• Cloud Search Service

css
  1. Help Center
  2. Cloud Search Service
  3. User Guide
  4. Getting Started
  5. Getting Started with Elasticsearch

Getting Started with Elasticsearch

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.

  1. Scenario Description
  2. Creating a Cluster
  3. Importing Data
  4. Searching for Data
  5. Deleting the Cluster

Scenario Description

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

Creating a Cluster

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.

Figure 1 Creating a cluster

Importing Data

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.

  1. On the Cluster Management page of the CSS management console, locate the row where the target cluster resides and click Kibana in the Operation column.
  2. In the left navigation pane of Kibana, click Dev Tools. Click Get to work to switch to the Console page. See Figure 2.

    Enter the code as required in the left pane and view the result in the right pane.

    Figure 2 Console page
  3. On the Console page, run the following command to create index my_store:
    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"
    }
  4. On the Console page, run the following command to import data to 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.

Searching for Data

  • Full-text search

    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"
            }
          }
        ]
      }
    }
    • Elasticsearch supports full-text search. The preceding command searches for the information about all commodities whose names include "spring" or "jeans".
    • Unlike traditional databases, Elasticsearch can return results in milliseconds by using inverted indices.
    • Elasticsearch supports sorting by score. In the command output, information about the first two commodities contains both "spring" and "jeans", while that about the last two commodities contains only "spring". Therefore, the first two commodities rank prior to the last two due to high keyword match.
  • Aggregation result display

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

Deleting a Cluster

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.

  1. Log in to the CSS management console. In the left navigation pane, click Cluster Management to switch to the Cluster Management page.
  2. Locate the row where the Es-xfx cluster resides and click More > Delete in the Operation column.
  3. In the displayed dialog box, click OK.