Examples of index operations for common ES versions

更新时间:
复制 MD 格式

This page walks you through creating an index, inserting documents, running searches, and deleting an index on Alibaba Cloud Elasticsearch. All examples use a sample wealth management dataset and include version-specific syntax for ES versions earlier than 7.0, 7.x, and 8.x.

Sample data

The examples use the following six wealth management products as sample documents. Load this data after you create the index.

{
    "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."
        }
    ]
}

Create an index

Create an index named product_info with 5 shards, 1 replica, and explicit field mappings. The productName and describe fields use the ik_smart analyzer for Chinese text; annual_rate is stored as a keyword for exact-match and range queries.

The mapping syntax differs between versions because Elasticsearch 7.0 removed the mapping type concept.

Versions 7.0 and later

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"
        }
    }
  }
}

Versions earlier than 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"
        }
      }
    }
  }
}
Important

Elasticsearch 7.0 and later removes the mapping type from the mappings body. If you include a type name in version 7.0 or later, the cluster returns a mapper_parsing_exception error. For details, see Removal of mapping types.

A successful request returns:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "product_info"
}

Insert documents

Use the Bulk API to insert all six sample documents in a single request. The endpoint path varies by version.

Version Endpoint
8.x POST /product_info/_bulk
7.x POST /product_info/_doc/_bulk
Earlier than 7.0 POST /product_info/products/_bulk
Version 7.10 accepts the bulk insert syntax for both ES 7.x and ES 8.x.

Version 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."}

Version 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."}

Versions earlier than 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."}

A successful bulk insert returns a response with "errors": false.

Search for data

The search endpoint path varies by version, following the same pattern as the insert endpoints.

Version Search endpoint
8.x GET /product_info/_search
7.x GET /product_info/_doc/_search
Earlier than 7.0 GET /product_info/products/_search
Version 7.10 accepts the search syntax for both ES 7.x and ES 8.x.

The examples below show two query types: full-text search using a match query and range filtering using a range query.

A match query analyzes the input text and returns documents where the specified field contains matching terms, ranked by relevance score.

A range query on a keyword field returns documents where the field value falls within the specified bounds. The following example finds products with an annual_rate between 3.0000% and 3.1300%.

Version 8.x

  • Full-text search

    GET /product_info/_search
    {
      "query": {
        "match": {
          "describe": "A message is pushed every day when the earnings are credited to your account."
        }
      }
    }

    <details> <summary>Example response</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>

    Key response fields:

    • took — query execution time in milliseconds.

    • timed_outtrue if the query exceeded its timeout; partial results are returned in that case.

    • _shards — how many shards were queried and how many succeeded.

    • hits.total.value — total number of matching documents (6 here, because the query terms appear in all six product descriptions after analysis).

    • max_score — the highest relevance score among all matching documents.

    • _score — each document's relevance score; higher means a stronger match.

    • _source — the original document fields stored in the index.

  • Range search

    GET /product_info/_search
    {
      "query": {
        "range": {
          "annual_rate": {
            "gte": "3.0000%",
            "lte": "3.1300%"
          }
        }
      }
    }

    <details> <summary>Example response</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>

For more information about search methods, see Query DSL.

Version 7.x

  • Full-text search

    GET /product_info/_doc/_search
    {
      "query": {
        "match": {
          "describe": "A message is pushed every day when the earnings are credited to your account."
        }
      }
    }

    <details> <summary>Example response</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>

  • Range search

    GET /product_info/_doc/_search
    {
      "query": {
        "range": {
          "annual_rate": {
            "gte": "3.0000%",
            "lte": "3.1300%"
          }
        }
      }
    }

    <details> <summary>Example response</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>

For more information about search methods, see Query DSL.

Versions earlier than 7.0

  • Full-text search

    GET /product_info/products/_search
    {
      "query": {
        "match": {
          "describe": "A message is pushed every day when the earnings are credited to your account."
        }
      }
    }

    <details> <summary>Example response</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>

  • Range search

    GET /product_info/products/_search
    {
      "query": {
        "range": {
          "annual_rate": {
            "gte": "3.0000%",
            "lte": "3.1300%"
          }
        }
      }
    }

    <details> <summary>Example response</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>

    For the full Query DSL reference, see Query DSL.

For more information about search methods, see Query DSL.

Delete the index

After you finish with the examples, delete the index to release resources.

Warning

Deleting an index permanently removes all documents and mappings. This operation cannot be undone.

DELETE /product_info

A successful deletion returns:

{
  "acknowledged" : true
}