Esta página explica como criar um índice, inserir documentos, executar buscas e excluir um índice no Alibaba Cloud Elasticsearch. Todos os exemplos usam um conjunto de dados de amostra de produtos de investimento e incluem sintaxe específica para versões do ES anteriores à 7,0, 7.x e 8.x.
Dados de amostra
Os exemplos usam os seis produtos de investimento a seguir como documentos de amostra. Carregue esses dados após criar o índice.
{
"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."
}
]
}
Criar um índice
Crie um índice chamado product_info com 5 shards, 1 réplica e mapeamentos explícitos de campos. Os campos productName e describe usam o analisador ik_smart para texto em chinês. O campo annual_rate é armazenado como keyword para permitir consultas de correspondência exata e de intervalo.
A sintaxe de mapeamento varia entre as versões porque o Elasticsearch 7,0 removeu o conceito de tipo de mapeamento.
Versões 7,0 e posteriores
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"
}
}
}
}
Versões anteriores à 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"
}
}
}
}
}
O Elasticsearch 7,0 e versões posteriores removem o tipo de mapeamento do corpo de mappings. Se você incluir um nome de tipo na versão 7,0 ou posterior, o cluster retornará um erro mapper_parsing_exception. Para obter detalhes, consulte Remoção de tipos de mapeamento.
Uma solicitação bem-sucedida retorna:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "product_info"
}
Inserir documentos
Use a Bulk API para inserir todos os seis documentos de amostra em uma única solicitação. O caminho do endpoint varia conforme a versão.
|
Versão |
Endpoint |
|
8.x |
|
|
7.x |
|
|
Anterior à 7,0 |
|
A versão 7,10 aceita a sintaxe de inserção em massa tanto para ES 7.x quanto para ES 8.x.
Versão 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."}
Versão 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."}
Versões anteriores à 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."}
Uma inserção em massa bem-sucedida retorna uma resposta com "errors": false.
Buscar dados
O caminho do endpoint de busca varia conforme a versão, seguindo o mesmo padrão dos endpoints de inserção.
|
Versão |
Endpoint de busca |
|
8.x |
|
|
7.x |
|
|
Anterior à 7,0 |
|
A versão 7,10 aceita a sintaxe de busca tanto para ES 7.x quanto para ES 8.x.
Os exemplos a seguir demonstram dois tipos de consulta: busca de texto completo com match e filtragem por intervalo com range.
A consulta match analisa o texto de entrada e retorna documentos em que o campo especificado contém termos correspondentes, classificados por pontuação de relevância.
A consulta range em um campo keyword retorna documentos cujo valor do campo está dentro dos limites especificados. O exemplo a seguir encontra produtos com annual_rate entre 3.0000% e 3.1300%.
Versão 8.x
-
Busca de texto completo
GET /product_info/_search { "query": { "match": { "describe": "A message is pushed every day when the earnings are credited to your account." } } }<details> <summary>Exemplo de resposta</summary>
{ "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." } } ] } }</details>
Principais campos da resposta:
took— tempo de execução da consulta em milissegundos.timed_out—truese a consulta exceder o tempo limite; nesse caso, resultados parciais são retornados._shards— quantidade de shards consultados e quantos tiveram sucesso.hits.total.value— número total de documentos correspondentes (6aqui, pois os termos da consulta aparecem nas descrições de todos os seis produtos após a análise).max_score— maior pontuação de relevância entre todos os documentos correspondentes._score— pontuação de relevância de cada documento; valores mais altos indicam correspondência mais forte._source— campos originais do documento armazenados no índice.
-
Busca por intervalo
GET /product_info/_search { "query": { "range": { "annual_rate": { "gte": "3.0000%", "lte": "3.1300%" } } } }<details> <summary>Exemplo de resposta</summary>
{ "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." } } ] } }</details>
Para obter mais informações sobre métodos de busca, consulte Query DSL.
Versão 7.x
-
Busca de texto completo
GET /product_info/_doc/_search { "query": { "match": { "describe": "A message is pushed every day when the earnings are credited to your account." } } }<details> <summary>Exemplo de resposta</summary>
{ "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." } } ] } }</details>
-
Busca por intervalo
GET /product_info/_doc/_search { "query": { "range": { "annual_rate": { "gte": "3.0000%", "lte": "3.1300%" } } } }<details> <summary>Exemplo de resposta</summary>
{ "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." } } ] } }</details>
Para obter mais informações sobre métodos de busca, consulte Query DSL.
Versões anteriores à 7,0
-
Busca de texto completo
GET /product_info/products/_search { "query": { "match": { "describe": "A message is pushed every day when the earnings are credited to your account." } } }<details> <summary>Exemplo de resposta</summary>
{ "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." } } ] } }</details>
-
Busca por intervalo
GET /product_info/products/_search { "query": { "range": { "annual_rate": { "gte": "3.0000%", "lte": "3.1300%" } } } }<details> <summary>Exemplo de resposta</summary>
{ "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." } } ] } }</details>
Para consultar a referência completa da Query DSL, consulte Query DSL.
Para obter mais informações sobre métodos de busca, consulte Query DSL.
Excluir o índice
Após concluir os exemplos, exclua o índice para liberar recursos.
A exclusão de um índice remove permanentemente todos os documentos e mapeamentos. Essa operação não pode ser desfeita.
DELETE /product_info
Uma exclusão bem-sucedida retorna:
{
"acknowledged" : true
}