Getting Started with Elasticsearch

This section describes how to use Elasticsearch for product search. You can use the Elasticsearch search engine of CSS to search for data based on the scenario example. The basic operation process is as follows:

Scenario Description

A women's clothing brand builds an e-commerce website. It uses traditional databases to provide a product search function for users. However, due to an increase in the number of users and business growth, the traditional databases have slow response and low accuracy. To improve user experience and user retention, the e-commerce website plans to use Elasticsearch to provide the product search function for users.

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

Step 1: Create a Cluster

Create a cluster using Elasticsearch as the search engine. In this example, suppose that you create a cluster named Es-xfx. This cluster is used only for 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 an Elasticsearch Cluster in Non-Security Mode.

Create a cluster using Elasticsearch as the search engine. In this example, suppose that you create a cluster named Sample-ESCluster. This cluster is used only for getting started with Elasticsearch. For this cluster, you are advised to select ess.spec-4u8g for Node Specifications, High I/O for Node Storage Type, and 40 GB for Node Storage Capacity. For details, see or .

After you create the cluster, switch to the cluster list to view the created cluster. If the Status of the cluster is Available, the cluster is created successfully.

Step 2: Import Data

CSS supports importing data to Elasticsearch using Logstash, Kibana, or APIs. Kibana lets you visualize your Elasticsearch data. The following procedure illustrates how to import data to Elasticsearch using Kibana.

  1. On the Clusters page, locate the target cluster and click More > Cerebro in the Operation column to go to the Cerebro login page.
    • Non-security cluster: Click the cluster name on the Cerebro login page to go to the Cerebro console.
    • Security cluster: Click the cluster name on the Cerebro login page, enter the username and password, and click Authenticate to go to the Cerebro console. The default username is admin and the password is the one specified during cluster creation.
  1. In the navigation pane of Kibana on the left, choose Dev Tools.

    The text box on the left is the input box. The triangle icon in the upper right corner of the input box is the command execution button. The text box on the right area is the result output box.

    Figure 1 Console page

    The Kibana UI varies depending on the Kibana version.

  2. On the Console page, run the following command to create index named my_store:
    (Versions earlier than 7.x)
    PUT /my_store
    {
      "settings": {
        "number_of_shards": 1
      },
      "mappings": {
        "products": {
          "properties": {
            "productName": {
              "type": "text",
              "analyzer": "ik_smart"
            },
            "size": {
              "type": "keyword"
            }
          }
        }
      }
    }

    (Versions later than 7.x)

    PUT /my_store
    {
      "settings": {
        "number_of_shards": 1
      },
      "mappings": {
              "properties": {
            "productName": {
              "type": "text",
              "analyzer": "ik_smart"
            },
            "size": {
              "type": "keyword"
            }
          }
        }
      }

    The command output is similar to the following:

    {
      "acknowledged" : true,
      "shards_acknowledged" : true,
      "index" : "my_store"
    }
  3. On the Console page, run the following command to import data to index named my_store:

    (Versions earlier than 7.x)

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

    (Versions later than 7.x)

    POST /my_store/_doc/_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.

Step 3: Search for Data

Step 4: Delete the Cluster

Once you understand the process and method of using Elasticsearch, you can perform the following steps to delete the cluster you created for the example and its data to avoid resource wastage.

After you delete a cluster, its data cannot be restored. Exercise caution when deleting a cluster.

  1. Log in to the CSS management console. In the navigation pane on the left, choose Clusters > Elasticsearch.
  2. Locate the row that contains cluster Es-xfx and click More > Delete in the Operation column.
  3. In the displayed dialog box, enter the name of the cluster to be deleted and click OK.