For details about the concept, advantages, functions, and application scenarios of CSS, see Overview.
The following shows the process of searching for data using Elasticsearch in CSS.
A female clothes brand does business on the e-commerce website. It uses traditional databases to provide the commodity search function for users. However, with the increase in users and business, it suffers a lot from the slow response and low accuracy due to the traditional databases. To improve user experience and avoid user loss, the e-commerce website adopts Elasticsearch to provide the commodity search function for users. This method solves the issues caused by traditional databases and increases user quantity.
This section describes how to use Elasticsearch to provide the search function for users.
Assume that the e-commerce website provides the following data:
{ "products":[ {"productName":"Latest art shirts for women in 2017 autumn","size":"L"} {"productName":"Latest art shirts for women in 2017 autumn","size":"M"} {"productName":"Latest art shirts for women in 2017 autumn","size":"S"} {"productName":"Latest jeans for women in spring 2018","size":"M"} {"productName":"Latest jeans for women in spring 2018","size":"S"} {"productName":"Latest casual pants for women in spring 2017","size":"L"} {"productName":"Latest casual pants for women in spring 2017","size":"S"} ] }
Before searching for data, create a cluster using Elasticsearch. In this example, suppose that you create a cluster named Es-xfx. This cluster is used only for your getting started with Elasticsearch. For this cluster, you are advised to select css.medium.8 for Node Specifications, Common I/O for Node Storage Type, and 40 GB for Node Storage Capacity. For details, see Creating a Cluster.
After a cluster is created, you can switch to the cluster list to view the created cluster. See Figure 1.
CSS supports importing data to Elasticsearch by using Logstash, Kibana, or APIs. Kibana lets you visualize your Elasticsearch data. The following procedure illustrates how to import data to Elasticsearch using Kibana.
Enter the code as required in the left pane and view the result in the right pane.
PUT /my_store { "settings": { "number_of_shards": 1 }, "mappings": { "products": { "properties": { "productName": { "type": "text" }, "size": { "type": "keyword" } } } } }
The command output is similar to the following.
{ "acknowledged": true, "shards_acknowledged": true, "index": "my_store" }
POST /my_store/products/_bulk {"index":{}} {"productName":"Latest art shirts for women in 2017 autumn","size":"L"} {"index":{}} {"productName":"Latest art shirts for women in 2017 autumn","size":"M"} {"index":{}} {"productName":"Latest art shirts for women in 2017 autumn","size":"S"} {"index":{}} {"productName":"Latest jeans for women in spring 2018","size":"M"} {"index":{}} {"productName":"Latest jeans for women in spring 2018","size":"S"} {"index":{}} {"productName":"Latest casual pants for women in spring 2017","size":"L"} {"index":{}} {"productName":"Latest casual pants for women in spring 2017","size":"S"}
If the value of the errors field in the command output is false, the data is imported successfully.
If you access the e-commerce website and want to search for commodities whose names include "spring jeans", enter "spring jeans" to begin your search. The following provides the command to be executed on Kibana and the command output.
You need to run the following command on Kibana:
GET /my_store/products/_search { "query": {"match": { "productName": "spring jeans" }} }
The command output is similar to the following.
{ "took": 80, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 1.8069603, "hits": [ { "_index": "my_store", "_type": "products", "_id": "yTG1QWUBRuneTTG2KJSq", "_score": 1.8069603, "_source": { "productName": "Latest jeans for women in spring 2018", "size": "M" } }, { "_index": "my_store", "_type": "products", "_id": "yjG1QWUBRuneTTG2KJSq", "_score": 1.8069603, "_source": { "productName": "Latest jeans for women in spring 2018", "size": "S" } }, { "_index": "my_store", "_type": "products", "_id": "yzG1QWUBRuneTTG2KJSq", "_score": 0.56677663, "_source": { "productName": "Latest casual pants for women in spring 2017", "size": "L" } }, { "_index": "my_store", "_type": "products", "_id": "zDG1QWUBRuneTTG2KJSq", "_score": 0.56677663, "_source": { "productName": "Latest casual pants for women in spring 2017", "size": "S" } } ] } }
The e-commerce website provides the function of displaying aggregation results. For example, it classifies commodities corresponding to "spring" based on the size so that you can collect the number of commodities of different sizes. The following provides the command to be executed on Kibana and the command output.
You need to run the following command on Kibana:
GET /my_store/products/_search { "query": { "match": { "productName": "spring" } }, "size": 0, "aggs": { "sizes": { "terms": { "field": "size" } } } }
The command output is similar to the following.
{ "took": 66, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": 4, "max_score": 0, "hits": [] }, "aggregations": { "sizes": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "S", "doc_count": 2 }, { "key": "L", "doc_count": 1 }, { "key": "M", "doc_count": 1 } ] } } }
After you understand the process and method of using Elasticsearch, you can perform the following steps to delete the sample cluster and sample data to avoid resource waste.
After a cluster is deleted, its data cannot be recovered. Therefore, exercise caution when performing this operation.