This topic describes how to create an Alibaba Cloud Elasticsearch cluster and access the cluster. It also describes how to call Elasticsearch RESTful APIs to create an index, create a document and insert data into the document, search for data, and delete the index.
Prerequisites
- An Alibaba Cloud account is created.
To create an Alibaba Cloud account, visit the account registration page.
- A virtual private cloud (VPC) and a vSwitch are created.
For more information, see Create a VPC with an IPv4 CIDR block.
- The specifications and storage capacity of your cluster are evaluated.
For more information, see Evaluate specifications and storage capacity.
Scenarios
A finance service enterprise uses an online platform to manage its wealth management products and uses conventional databases to provide the search feature for customers. The wealth management products provided by the enterprise offer satisfactory returns, and the customer base of the enterprise grows rapidly. The expansion of its business system and the increase of customer information cause the inherent issues of conventional databases to become noticeable. These issues include slow search responses, low accuracy, and the low performance of data service devices. To resolve these issues and improve customer satisfaction, the enterprise purchases the Alibaba Cloud Elasticsearch service. This topic uses the preceding scenario to describe how to use Alibaba Cloud Elasticsearch to create a cluster and search for data.
{
"products":[
{"productName":"Wealth Management Product A","annual_rate":"3.2200%","describe":"180-day wealth management product that requires the minimum investment of USD 20,000, enables low-risk investment, and allows you to select whether to receive push messages for returns."}
{"productName":"Wealth Management Product B","annual_rate":"3.1100%","describe":"90-day wealth management product that requires the minimum investment of USD 10,000 and daily pushes messages for returns credited to your account."}
{"productName":"Wealth Management Product C","annual_rate":"3.3500%","describe":"270-day wealth management product that requires the minimum investment of USD 40,000 and daily pushes messages for returns immediately credited to your account."}
{"productName":"Wealth Management Product D","annual_rate":"3.1200%","describe":"90-day wealth management product that requires the minimum investment of USD 12,000 and daily pushes messages for returns credited to your account."}
{"productName":"Wealth Management Product E","annual_rate":"3.0100%","describe":"30-day wealth management product that requires the minimum investment of USD 8,000 and daily pushes messages for returns."}
{"productName":"Wealth Management Product F","annual_rate":"2.7500%","describe":"3-day popular wealth management product that does not require service fees, requires the minimum investment of USD 500, and pushes messages for returns."}
]
}
Procedure
Video tutorial
Step 1: Create a cluster
Step 2: Access the cluster
Step 3: Create an index
- Command for an Elasticsearch cluster of a version earlier than V7.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" } } } } }
- Command for an Elasticsearch cluster of V7.0 or 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" } } } }
Notice Mapping types are deprecated in open source Elasticsearch 7.0 and later. However, these mapping types are still supported in versions earlier than Elasticsearch 7.0. If mapping types are used in Alibaba Cloud Elasticsearch V7.0 and later, the system displays the error message"type": "mapper_parsing_exception"
. For more information about the error details and solution, see Removal of mapping types.
In the preceding example, an index named product_info is created. In a version earlier than Elasticsearch V7.0, the index is of the products type. In Elasticsearch V7.0 or later, the index is of the _doc type. The index contains the productName, annual_rate, and describe fields.
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "product_info"
}
Step 4: Create documents and insert data into the documents
POST /product_info/products/_bulk
{"index":{}}
{"productName":"Wealth Management Product A","annual_rate":"3.2200%","describe":"180-day wealth management product that requires the minimum investment of USD 20,000, enables low-risk investment, and allows you to select whether to receive push messages for returns."}
{"index":{}}
{"productName":"Wealth Management Product B","annual_rate":"3.1100%","describe":"90-day wealth management product that requires the minimum investment of USD 10,000 and daily pushes messages for returns credited to your account."}
{"index":{}}
{"productName":"Wealth Management Product C","annual_rate":"3.3500%","describe":"270-day wealth management product that requires the minimum investment of USD 40,000 and daily pushes messages for returns immediately credited to your account."}
{"index":{}}
{"productName":"Wealth Management Product D","annual_rate":"3.1200%","describe":"90-day wealth management product that requires the minimum investment of USD 12,000 and daily pushes messages for returns credited to your account."}
{"index":{}}
{"productName":"Wealth Management Product E","annual_rate":"3.0100%","describe":"30-day wealth management product that requires the minimum investment of USD 8,000 and daily pushes messages for returns."}
{"index":{}}
{"productName":"Wealth Management Product F","annual_rate":"2.7500%","describe":"3-day popular wealth management product that does not require service fees, requires the minimum investment of USD 500, and pushes messages for returns."}
If "errors" : false
is returned, data is inserted into the created documents.
Step 5: Search for data
- Full-text search
Run the following command to search for products whose descriptions contain
daily pushes messages for returns credited to your account
:GET /product_info/products/_search { "query": { "match": { "describe": "daily pushes messages for returns credited to your account" } } }
If the command is successfully run, the following result is returned:{ "took" : 21, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 6, "max_score" : 1.3968885, "hits" : [ { "_index" : "product_info", "_type" : "products", "_id" : "WLvWYXAB8Rql5AUxLqUU", "_score" : 1.3968885, "_source" : { "productName" : "Wealth Management Product B", "annual_rate" : "3.1100%", "describe" : "90-day wealth management product that requires the minimum investment of USD 10,000 and daily pushes messages for returns credited to your account." } }, { "_index" : "product_info", "_type" : "products", "_id" : "WrvWYXAB8Rql5AUxLqUU", "_score" : 1.3968885, "_source" : { "productName" : "Wealth Management Product D", "annual_rate" : "3.1200%", "describe" : "90-day wealth management product that requires the minimum investment of USD 12,000 and daily pushes messages for returns credited to your account." } }, { "_index" : "product_info", "_type" : "products", "_id" : "WbvWYXAB8Rql5AUxLqUU", "_score" : 1.3547361, "_source" : { "productName" : "Wealth Management Product C", "annual_rate" : "3.3500%", "describe" : "270-day wealth management product that requires the minimum investment of USD 40,000 and daily pushes messages for returns immediately credited to your account." } }, { "_index" : "product_info", "_type" : "products", "_id" : "W7vWYXAB8Rql5AUxLqUU", "_score" : 1.1507283, "_source" : { "productName" : "Wealth Management Product E", "annual_rate" : "3.0100%", "describe" : "30-day wealth management product that requires the minimum investment of USD 8,000 and daily pushes messages for returns." } }, { "_index" : "product_info", "_type" : "products", "_id" : "XLvWYXAB8Rql5AUxLqUU", "_score" : 0.5753642, "_source" : { "productName" : "Wealth Management Product F", "annual_rate" : "2.7500%", "describe" : "3-day popular wealth management product that does not require service fees, requires the minimum investment of USD 500, and pushes messages for returns." } }, { "_index" : "product_info", "_type" : "products", "_id" : "V7vWYXAB8Rql5AUxLqUU", "_score" : 0.31854028, "_source" : { "productName" : "Wealth Management Product A", "annual_rate" : "3.2200%", "describe" : "180-day wealth management product that requires the minimum investment of USD 20,000, enables low-risk investment, and allows you to select whether to receive push messages for returns." } } ] } }
Note Alibaba Cloud Elasticsearch allows you to use a tokenizer to search for data. It also allows you to sort the records in search results by score. In the preceding result, the descriptions of the first two products containdaily pushes messages for returns credited to your account
, and the descriptions of the last two products contain onlypushes messages
. The higher the ranking of a product in the search result, the higher the matching degree and score of the product. - Search by condition
Run the following command to search for products with an annualized rate of 3.0000% to 3.1300%:
GET /product_info/products/_search { "query": { "range": { "annual_rate": { "gte": "3.0000%", "lte": "3.1300%" } } } }
If the command is successfully run, the following result is returned:{ "took" : 10, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 1.0, "hits" : [ { "_index" : "product_info", "_type" : "products", "_id" : "WLvWYXAB8Rql5AUxLqUU", "_score" : 1.0, "_source" : { "productName" : "Wealth Management Product B", "annual_rate" : "3.1100%", "describe" : "90-day wealth management product that requires the minimum investment of USD 10,000 and daily pushes messages for returns credited to your account." } }, { "_index" : "product_info", "_type" : "products", "_id" : "WrvWYXAB8Rql5AUxLqUU", "_score" : 1.0, "_source" : { "productName" : "Wealth Management Product D", "annual_rate" : "3.1200%", "describe" : "90-day wealth management product that requires the minimum investment of USD 12,000 and daily pushes messages for returns credited to your account." } } ] } }
Note The system finds products that meet your requirements based on the search conditions and displays the products in descending order.For more information about search methods, see Query DSL.
Step 6: (Optional) Delete the index
DELETE /product_info
{
"acknowledged" : true
}
Step 7: (Optional) Release the cluster
References
- Learn how to access and configure an Alibaba Cloud Elasticsearch cluster: Access and configure an Elasticsearch cluster
- Learn how to synchronize data from an ApsaraDB RDS for MySQL database to an Alibaba Cloud Elasticsearch cluster: Select a synchronization method
- Learn FAQ about Alibaba Cloud Elasticsearch: FAQ