このトピックでは、一般的な ES バージョンのインデックス操作の例を示します。
サンプルデータ
このトピックでは、サンプルデータを提供し、それを使用して関連する操作をデモンストレーションします。
{
"products": [
{
"productName": "Wealth Management Product A",
"annual_rate": "3.2200%",
"describe": "A 180-day fixed-term wealth management product. The minimum investment is 20,000. It provides stable returns and lets you choose whether to receive message pushes."
},
{
"productName": "Wealth Management Product B",
"annual_rate": "3.1100%",
"describe": "A 90-day regular investment product. The minimum investment is 10,000. A message is pushed every day when the earnings are credited to your account."
},
{
"productName": "Wealth Management Product C",
"annual_rate": "3.3500%",
"describe": "A 270-day regular investment product. The minimum investment is 40,000. A message is pushed every day when the earnings are immediately credited to your account."
},
{
"productName": "Wealth Management Product D",
"annual_rate": "3.1200%",
"describe": "A 90-day regular investment product. The minimum investment is 12,000. A message is pushed every day when the earnings are credited to your account."
},
{
"productName": "Wealth Management Product E",
"annual_rate": "3.0100%",
"describe": "A recommended 30-day regular investment product. The minimum investment is 8,000. A message about daily earnings is pushed."
},
{
"productName": "Wealth Management Product F",
"annual_rate": "2.7500%",
"describe": "A popular 3-day short-term product. No service fee is required. The minimum investment is 500. You can receive earnings messages by text message."
}
]
}インデックスの作成
product_info という名前のインデックスを作成します。
バージョン 7.0 以降
PUT /product_info
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"productName": {
"type": "text",
"analyzer": "ik_smart"
},
"annual_rate":{
"type":"keyword"
},
"describe": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
}バージョン 7.0 以前
PUT /product_info
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"products": {
"properties": {
"productName": {
"type": "text",
"analyzer": "ik_smart"
},
"annual_rate":{
"type":"keyword"
},
"describe": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
}Elasticsearch 7.0 以降では、マッピングから type 定義が削除されます。以前のバージョンでは、引き続き type 定義がサポートされます。バージョン 7.0 以降で type を使用すると、"type": "mapper_parsing_exception" エラーメッセージが返されます。詳細については、「マッピングタイプの削除」をご参照ください。
インデックスが作成されると、次の結果が返されます。
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "product_info"
}ドキュメントの作成とデータの挿入
バージョン 8.x
POST /product_info/_bulk
{"index":{}}
{"productName":"Wealth Management Product A","annual_rate":"3.2200%","describe":"A 180-day fixed-term wealth management product. The minimum investment is 20,000. It provides stable returns and lets you choose whether to receive message pushes."}
{"index":{}}
{"productName":"Wealth Management Product B","annual_rate":"3.1100%","describe":"A 90-day regular investment product. The minimum investment is 10,000. A message is pushed every day when the earnings are credited to your account."}
{"index":{}}
{"productName":"Wealth Management Product C","annual_rate":"3.3500%","describe":"A 270-day regular investment product. The minimum investment is 40,000. A message is pushed every day when the earnings are immediately credited to your account."}
{"index":{}}
{"productName":"Wealth Management Product D","annual_rate":"3.1200%","describe":"A 90-day regular investment product. The minimum investment is 12,000. A message is pushed every day when the earnings are credited to your account."}
{"index":{}}
{"productName":"Wealth Management Product E","annual_rate":"3.0100%","describe":"A recommended 30-day regular investment product. The minimum investment is 8,000. A message about daily earnings is pushed."}
{"index":{}}
{"productName":"Wealth Management Product F","annual_rate":"2.7500%","describe":"A popular 3-day short-term product. No service fee is required. The minimum investment is 500. You can receive earnings messages by text message."}バージョン 7.x
POST /product_info/_doc/_bulk
{"index":{}}
{"productName":"Wealth Management Product A","annual_rate":"3.2200%","describe":"A 180-day fixed-term wealth management product. The minimum investment is 20,000. It provides stable returns and lets you choose whether to receive message pushes."}
{"index":{}}
{"productName":"Wealth Management Product B","annual_rate":"3.1100%","describe":"A 90-day regular investment product. The minimum investment is 10,000. A message is pushed every day when the earnings are credited to your account."}
{"index":{}}
{"productName":"Wealth Management Product C","annual_rate":"3.3500%","describe":"A 270-day regular investment product. The minimum investment is 40,000. A message is pushed every day when the earnings are immediately credited to your account."}
{"index":{}}
{"productName":"Wealth Management Product D","annual_rate":"3.1200%","describe":"A 90-day regular investment product. The minimum investment is 12,000. A message is pushed every day when the earnings are credited to your account."}
{"index":{}}
{"productName":"Wealth Management Product E","annual_rate":"3.0100%","describe":"A recommended 30-day regular investment product. The minimum investment is 8,000. A message about daily earnings is pushed."}
{"index":{}}
{"productName":"Wealth Management Product F","annual_rate":"2.7500%","describe":"A popular 3-day short-term product. No service fee is required. The minimum investment is 500. You can receive earnings messages by text message."}バージョン 7.10 は、ES 7.x と ES 8.x の両方のデータ挿入構文をサポートしています。
7.0 より前のバージョン
POST /product_info/products/_bulk
{"index":{}}
{"productName":"Wealth Management Product A","annual_rate":"3.2200%","describe":"A 180-day fixed-term wealth management product. The minimum investment is 20,000. It provides stable returns and lets you choose whether to receive message pushes."}
{"index":{}}
{"productName":"Wealth Management Product B","annual_rate":"3.1100%","describe":"A 90-day regular investment product. The minimum investment is 10,000. A message is pushed every day when the earnings are credited to your account."}
{"index":{}}
{"productName":"Wealth Management Product C","annual_rate":"3.3500%","describe":"A 270-day regular investment product. The minimum investment is 40,000. A message is pushed every day when the earnings are immediately credited to your account."}
{"index":{}}
{"productName":"Wealth Management Product D","annual_rate":"3.1200%","describe":"A 90-day regular investment product. The minimum investment is 12,000. A message is pushed every day when the earnings are credited to your account."}
{"index":{}}
{"productName":"Wealth Management Product E","annual_rate":"3.0100%","describe":"A recommended 30-day regular investment product. The minimum investment is 8,000. A message about daily earnings is pushed."}
{"index":{}}
{"productName":"Wealth Management Product F","annual_rate":"2.7500%","describe":"A popular 3-day short-term product. No service fee is required. The minimum investment is 500. You can receive earnings messages by text message."}返された結果に "errors" : false が含まれている場合、データが正常に挿入されたことを示します。
データの検索
バージョン 8.x
全文検索
GET /product_info/_search { "query": { "match": { "describe": "A message is pushed every day when the earnings are credited to your account." } } }検索が成功すると、次の結果が返されます。
{ "took": 15, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 6, "relation": "eq" }, "max_score": 2.41246, "hits": [ { "_index": "product_info", "_id": "pgAGQZgB5J6iFDPOX2QN", "_score": 2.41246, "_source": { "productName": "Wealth Management Product B", "annual_rate": "3.1100%", "describe": "A 90-day regular investment product. The minimum investment is 10,000. A message is pushed every day when the earnings are credited to your account." } }, { "_index": "product_info", "_id": "pwAGQZgB5J6iFDPOX2QN", "_score": 2.343423, "_source": { "productName": "Wealth Management Product C", "annual_rate": "3.3500%", "describe": "A 270-day regular investment product. The minimum investment is 40,000. A message is pushed every day when the earnings are immediately credited to your account." } }, { "_index": "product_info", "_id": "qAAGQZgB5J6iFDPOX2QN", "_score": 1.7260926, "_source": { "productName": "Wealth Management Product D", "annual_rate": "3.1200%", "describe": "A 90-day regular investment product. The minimum investment is 12,000. A message is pushed every day when the earnings are credited to your account." } }, { "_index": "product_info", "_id": "qQAGQZgB5J6iFDPOX2QN", "_score": 0.9649055, "_source": { "productName": "Wealth Management Product E", "annual_rate": "3.0100%", "describe": "A recommended 30-day regular investment product. The minimum investment is 8,000. A message about daily earnings is pushed." } }, { "_index": "product_info", "_id": "pQAGQZgB5J6iFDPOX2QN", "_score": 0.8630463, "_source": { "productName": "Wealth Management Product A", "annual_rate": "3.2200%", "describe": "A 180-day fixed-term wealth management product. The minimum investment is 20,000. It provides stable returns and lets you choose whether to receive message pushes." } }, { "_index": "product_info", "_id": "qgAGQZgB5J6iFDPOX2QN", "_score": 0.19178316, "_source": { "productName": "Wealth Management Product F", "annual_rate": "2.7500%", "describe": "A popular 3-day short-term product. No service fee is required. The minimum investment is 500. You can receive earnings messages by text message." } } ] } }クエリ条件による検索
年率が 3.0000% から 3.1300% の間のプロダクトを検索します。
GET /product_info/_search { "query": { "range": { "annual_rate": { "gte": "3.0000%", "lte": "3.1300%" } } } }検索が成功すると、次の結果が返されます。
{ "took": 14, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 3, "relation": "eq" }, "max_score": 1, "hits": [ { "_index": "product_info", "_id": "qAAGQZgB5J6iFDPOX2QN", "_score": 1, "_source": { "productName": "Wealth Management Product D", "annual_rate": "3.1200%", "describe": "A 90-day regular investment product. The minimum investment is 12,000. A message is pushed every day when the earnings are credited to your account." } }, { "_index": "product_info", "_id": "pgAGQZgB5J6iFDPOX2QN", "_score": 1, "_source": { "productName": "Wealth Management Product B", "annual_rate": "3.1100%", "describe": "A 90-day regular investment product. The minimum investment is 10,000. A message is pushed every day when the earnings are credited to your account." } }, { "_index": "product_info", "_id": "qQAGQZgB5J6iFDPOX2QN", "_score": 1, "_source": { "productName": "Wealth Management Product E", "annual_rate": "3.0100%", "describe": "A recommended 30-day regular investment product. The minimum investment is 8,000. A message about daily earnings is pushed." } } ] } }
検索メソッドの詳細については、「Query DSL」をご参照ください。
バージョン 7.x
全文検索
GET /product_info/_doc/_search { "query": { "match": { "describe": "A message is pushed every day when the earnings are credited to your account." } } }検索が成功すると、次の結果が返されます。
{ "took" : 19, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "product_info", "_type" : "_doc", "_id" : "gC9RUJgBDC_Ir7OTV2QD", "_score" : 1.0, "_source" : { "productName" : "Wealth Management Product D", "annual_rate" : "3.1200%", "describe" : "A 90-day regular investment product. The minimum investment is 12,000. A message is pushed every day when the earnings are credited to your account." } }, { "_index" : "product_info", "_type" : "_doc", "_id" : "fi9RUJgBDC_Ir7OTV2QD", "_score" : 1.0, "_source" : { "productName" : "Wealth Management Product B", "annual_rate" : "3.1100%", "describe" : "A 90-day regular investment product. The minimum investment is 10,000. A message is pushed every day when the earnings are credited to your account." } }, { "_index" : "product_info", "_type" : "_doc", "_id" : "gS9RUJgBDC_Ir7OTV2QD", "_score" : 1.0, "_source" : { "productName" : "Wealth Management Product E", "annual_rate" : "3.0100%", "describe" : "A recommended 30-day regular investment product. The minimum investment is 8,000. A message about daily earnings is pushed." } } ] } }クエリ条件による検索
年率が 3.0000% から 3.1300% の間のプロダクトを検索します。
GET /product_info/_doc/_search { "query": { "range": { "annual_rate": { "gte": "3.0000%", "lte": "3.1300%" } } } }検索が成功すると、次の結果が返されます。
{ "took" : 5, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "product_info", "_type" : "_doc", "_id" : "gC9RUJgBDC_Ir7OTV2QD", "_score" : 1.0, "_source" : { "productName" : "Wealth Management Product D", "annual_rate" : "3.1200%", "describe" : "A 90-day regular investment product. The minimum investment is 12,000. A message is pushed every day when the earnings are credited to your account." } }, { "_index" : "product_info", "_type" : "_doc", "_id" : "fi9RUJgBDC_Ir7OTV2QD", "_score" : 1.0, "_source" : { "productName" : "Wealth Management Product B", "annual_rate" : "3.1100%", "describe" : "A 90-day regular investment product. The minimum investment is 10,000. A message is pushed every day when the earnings are credited to your account." } }, { "_index" : "product_info", "_type" : "_doc", "_id" : "gS9RUJgBDC_Ir7OTV2QD", "_score" : 1.0, "_source" : { "productName" : "Wealth Management Product E", "annual_rate" : "3.0100%", "describe" : "A recommended 30-day regular investment product. The minimum investment is 8,000. A message about daily earnings is pushed." } } ] } }
バージョン 7.10 は、ES 7.x と ES 8.x の両方のデータクエリ構文をサポートしています。
検索メソッドの詳細については、「Query DSL」をご参照ください。
7.0 より前のバージョン
全文検索
GET /product_info/products/_search { "query": { "match": { "describe": "A message is pushed every day when the earnings are credited to your account." } } }検索が成功すると、次の結果が返されます。
{ "took" : 21, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 6, "max_score" : 2.6264062, "hits" : [ { "_index" : "product_info", "_type" : "products", "_id" : "J8YIQZgBMhABkprCeWXb", "_score" : 2.6264062, "_source" : { "productName" : "Wealth Management Product B", "annual_rate" : "3.1100%", "describe" : "A 90-day regular investment product. The minimum investment is 10,000. A message is pushed every day when the earnings are credited to your account." } }, { "_index" : "product_info", "_type" : "products", "_id" : "KcYIQZgBMhABkprCeWXb", "_score" : 1.489365, "_source" : { "productName" : "Wealth Management Product D", "annual_rate" : "3.1200%", "describe" : "A 90-day regular investment product. The minimum investment is 12,000. A message is pushed every day when the earnings are credited to your account." } }, { "_index" : "product_info", "_type" : "products", "_id" : "KMYIQZgBMhABkprCeWXb", "_score" : 1.4445845, "_source" : { "productName" : "Wealth Management Product C", "annual_rate" : "3.3500%", "describe" : "A 270-day regular investment product. The minimum investment is 40,000. A message is pushed every day when the earnings are immediately credited to your account." } }, { "_index" : "product_info", "_type" : "products", "_id" : "K8YIQZgBMhABkprCeWXb", "_score" : 0.5753642, "_source" : { "productName" : "Wealth Management Product F", "annual_rate" : "2.7500%", "describe" : "A popular 3-day short-term product. No service fee is required. The minimum investment is 500. You can receive earnings messages by text message." } }, { "_index" : "product_info", "_type" : "products", "_id" : "JsYIQZgBMhABkprCeWXb", "_score" : 0.5469647, "_source" : { "productName" : "Wealth Management Product A", "annual_rate" : "3.2200%", "describe" : "A 180-day fixed-term wealth management product. The minimum investment is 20,000. It provides stable returns and lets you choose whether to receive message pushes." } }, { "_index" : "product_info", "_type" : "products", "_id" : "KsYIQZgBMhABkprCeWXb", "_score" : 0.53964466, "_source" : { "productName" : "Wealth Management Product E", "annual_rate" : "3.0100%", "describe" : "A recommended 30-day regular investment product. The minimum investment is 8,000. A message about daily earnings is pushed." } } ] } }クエリ条件による検索
年率が 3.0000% から 3.1300% の間のプロダクトを検索します。
GET /product_info/products/_search { "query": { "range": { "annual_rate": { "gte": "3.0000%", "lte": "3.1300%" } } } }検索が成功すると、次の結果が返されます。
{ "took" : 15, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 3, "max_score" : 1.0, "hits" : [ { "_index" : "product_info", "_type" : "products", "_id" : "J8YIQZgBMhABkprCeWXb", "_score" : 1.0, "_source" : { "productName" : "Wealth Management Product B", "annual_rate" : "3.1100%", "describe" : "A 90-day regular investment product. The minimum investment is 10,000. A message is pushed every day when the earnings are credited to your account." } }, { "_index" : "product_info", "_type" : "products", "_id" : "KcYIQZgBMhABkprCeWXb", "_score" : 1.0, "_source" : { "productName" : "Wealth Management Product D", "annual_rate" : "3.1200%", "describe" : "A 90-day regular investment product. The minimum investment is 12,000. A message is pushed every day when the earnings are credited to your account." } }, { "_index" : "product_info", "_type" : "products", "_id" : "KsYIQZgBMhABkprCeWXb", "_score" : 1.0, "_source" : { "productName" : "Wealth Management Product E", "annual_rate" : "3.0100%", "describe" : "A recommended 30-day regular investment product. The minimum investment is 8,000. A message about daily earnings is pushed." } } ] } }
検索メソッドの詳細については、「Query DSL」をご参照ください。
インデックスの削除
例を完了したら、次のコマンドを実行してインデックスを削除し、リソースの浪費を避けることができます。
インデックスが削除されると、回復することはできません。この操作は注意して実行してください。
DELETE /product_infoインデックスが削除されると、次の結果が返されます。
{
"acknowledged" : true
}