
AWS OpenSearchServerlessの使い方
https://aws.amazon.com/jp/blogs/news/elasticsearch-tutorial-a-quick-start-guide/
を参照してメモります。
インデックス・・・テーブル
PUT、POST・・・UPSERT
数値の比較:gte(<=)、gt(<)、lte(>=)、lt(>)

▼IDを指定する場合は、PUT を使ってインサート、アップデートする。
PUT /vegetables/_doc/1
{
"name":"carrot",
"color":"orange"
}

↑最初1発目のPUTでは”result”が”created”、”_version”が”1″。2回目のPUTでは”result”が”updated”、”_version”が”2″になります。
▼IDを指定しない場合は、POST を使ってインサート、アップデートする。
POST /vegetables/_doc
{
"name":"キュウリ",
"color":"緑"
}
※POST /vegetables/_doc/ だと「Request failed to get to the server (status code: 404)」のエラーになる。

▼GET /vegetables/ を全て取得する。
GET /vegetables/_search
{
"query": {
"match_all": {}
}
}

▼取得するサイズをしていする。ようするにLIMIT。
GET /vegetables/_search
{
"query": {
"match_all": {}
},
"size":3
}
▼GET /vegetables/_doc/1 を取得する。
GET /vegetables/_doc/1

▼GET /vegetables/_doc/RDdLpJMBQzhePOqbMB0c を取得する。
GET /vegetables/_doc/RDdLpJMBQzhePOqbMB0c

▼項目名で値が一致するものを検索する。
GET /vegetables/_search
{
"query" : {
"term": { "name": "carrot" }
}
}

▼インデックスを一覧表示する。
GET /_cat/indices
▼インデックスを削除する。
DELETE /vegetables

▼ソートするにはインデックスを作成する。
型を定義している。
DELETE /vegetables
PUT /vegetables
{
"mappings":{
"properties":{
"name":{
"type":"text"
},
"color":{
"type":"keyword"
},
"classification":{
"type":"keyword"
}
}
}
}
POST /_bulk
{ "create" : { "_index" : "vegetables", "_id" : "7" } }
{ "name":"kale", "color":"green", "classification":"leafy-green" }
{ "create" : { "_index" : "vegetables", "_id" : "8" } }
{ "name":"spinach", "color":"green", "classification":"leafy-green" }
{ "create" : { "_index" : "vegetables", "_id" : "9" } }
{ "name":"arugula", "color":"green", "classification":"leafy-green" }
{ "create" : { "_index" : "vegetables", "_id" : "10" } }
{ "name":"endive", "color":"green", "classification":"leafy-green" }
{ "create" : { "_index" : "vegetables", "_id" : "11" } }
{ "name":"lettuce", "color":"green", "classification":"leafy-green" }
{ "create" : { "_index" : "vegetables", "_id" : "12" } }
{ "name":"fruit", "color":"green", "classification":"leafy-green" }
{ "create" : { "_index" : "vegetables", "_id" : "13" } }
{ "name":"kiwi fruit", "color":"green", "classification":"leafy-green" }
ソート
GET /vegetables/_search
{
"query" : {
"term": { "color": "green" }
},
"sort" : [{
"classification": {
"order": "asc"
}}
]
}
スコア順にソート
GET /vegetables/_search
{
"query" : {
"term": { "name": "fruit" }
},
"sort" : "_score"
}
テキスト(文字列を単語分けしない)を検索する。
GET /vegetables/_search
{
"query" : {
"match": { "name": "fruit" }
},
"sort" : "_score"
}
キーワード(文字列を単語分けしない)を検索する。
GET /vegetables/_search
{
"query" : {
"wildcard": { "name": "*end*" }
},
"sort" : "_score"
}
▼数値の比較
型を定義している。
DELETE /vegetables
PUT /vegetables
{
"mappings":{
"properties":{
"name":{
"type":"text"
},
"color":{
"type":"keyword"
},
"classification":{
"type":"keyword"
},
"price":{
"type":"integer"
}
}
}
}
POST /_bulk
{ "create" : { "_index" : "vegetables", "_id" : "7" } }
{ "name":"kale", "color":"green", "classification":"leafy-green","price":"100" }
{ "create" : { "_index" : "vegetables", "_id" : "8" } }
{ "name":"spinach", "color":"green", "classification":"leafy-green","price":"200" }
{ "create" : { "_index" : "vegetables", "_id" : "9" } }
{ "name":"arugula", "color":"green", "classification":"leafy-green","price":"300" }
{ "create" : { "_index" : "vegetables", "_id" : "10" } }
{ "name":"endive", "color":"green", "classification":"leafy-green","price":"400" }
{ "create" : { "_index" : "vegetables", "_id" : "11" } }
{ "name":"lettuce", "color":"green", "classification":"leafy-green","price":"500" }
{ "create" : { "_index" : "vegetables", "_id" : "12" } }
{ "name":"fruit", "color":"green", "classification":"leafy-green","price":"600" }
{ "create" : { "_index" : "vegetables", "_id" : "13" } }
{ "name":"kiwi fruit", "color":"green", "classification":"leafy-green","price":"700" }
GET /vegetables/_search
{
"query": {
"range":{
"price":{
"gte": 500
}
}
},
"sort" : "price"
}

以上