Help Center> Cloud Search Service> User Guide> Getting Started> Getting Started with Elasticsearch

Getting Started with Elasticsearch

For details about the concept, advantages, functions, and application scenarios of Cloud Search Service (CSS), see the CSS Service Overview.

This section provides a simple example. For details, see Scenario Description. 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 commodity search function for users. However, with an increase in users and business, it suffers from the slow response and low accuracy of 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 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 autumn 2017","size":"L"}
{"productName":"Latest art shirts for women in autumn 2017","size":"M"}
{"productName":"Latest art shirts for women in autumn 2017","size":"S"}
{"productName":"Latest jeans for women in spring 2018","size":"M"}
{"productName":"Latest jeans for women in spring 2018","size":"S"}
{"productName":"Latest jeans for women in spring 2017","size":"L"}
{"productName":"Latest casual pants for women in spring 2017","size":"S"}
]
}

Step 1: Create a Cluster

Before searching for data, create a cluster using Elasticsearch. 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-2u16g for Node Specifications, High I/O for Node Storage Type, and 40 GB for Node Storage Capacity. For details, see Creating a Cluster.

After a cluster is created, switch to the cluster list to view the created cluster. If the Status of the cluster is Available, the cluster is created successfully. See Figure 1.

Figure 1 Creating a cluster

Step 2: Import Data

CSS supports importing data to Elasticsearch using Cloud Data Migration (CDM), Data Ingestion Service (DIS), 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 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 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"
    }
  4. 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 autumn 2017","size":"L"}
    {"index":{}}
    {"productName":"Latest art shirts for women in autumn 2017","size":"M"}
    {"index":{}}
    {"productName":"Latest art shirts for women in autumn 2017","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 autumn 2017","size":"L"}
    {"index":{}}
    {"productName":"Latest art shirts for women in autumn 2017","size":"M"}
    {"index":{}}
    {"productName":"Latest art shirts for women in autumn 2017","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

  • 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 text provides the command to be executed on Kibana and the command output.

    Command to be executed on Kibana:

    (Versions earlier than 7.x)

    GET /my_store/products/_search
    {
      "query": {"match": {
        "productName": "spring jeans"
      }}
    }

    (Versions later than 7.x)

    GET /my_store/_search
    {
      "query": {"match": {
        "productName": "spring jeans"
      }}
    }

    The command output is similar to the following:

    {
      "took" : 3,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 4,
          "relation" : "eq"
        },
        "max_score" : 1.7965372,
        "hits" : [
          {
            "_index" : "my_store",
            "_type" : "_doc",
            "_id" : "9xf6VHIBfClt6SDjw7H5",
            "_score" : 1.7965372,
            "_source" : {
              "productName": "Latest jeans for women in spring 2018",
              "size" : "M"
            }
          },
          {
            "_index" : "my_store",
            "_type" : "_doc",
            "_id" : "-Bf6VHIBfClt6SDjw7H5",
            "_score" : 1.7965372,
            "_source" : {
              "productName": "Latest jeans for women in spring 2018",
              "size" : "S"
            }
          },
          {
            "_index" : "my_store",
            "_type" : "_doc",
            "_id" : "-Rf6VHIBfClt6SDjw7H5",
            "_score" : 0.5945667,
            "_source" : {
              "productName": "Latest casual pants for women in spring 2017",
              "size" : "L"
            }
          },
          {
            "_index" : "my_store",
            "_type" : "_doc",
            "_id" : "-hf6VHIBfClt6SDjw7H5",
            "_score" : 0.5945667,
            "_source" : {
              "productName": "Latest casual pants for women in spring 2017",
              "size" : "S"
            }
          }
        ]
      }
    }
    
    • Elasticsearch supports word segmentation. The preceding command segments "spring jeans" into "spring" and "jeans".
    • 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.

    Command to be executed on Kibana:

    (Versions earlier than 7.x)

    GET /my_store/products/_search
    {
    "query": {
    "match": { "productName": "spring" }
    },
    "size": 0,
    "aggs": {
    "sizes": {
    "terms": { "field": "size" }
    }
    }
    }

    (Versions later than 7.x)

    GET /my_store/_search
    {
    "query": {
    "match": { "productName": "spring" }
    },
    "size": 0,
    "aggs": {
    "sizes": {
    "terms": { "field": "size" }
    }
    }
    }

    The command output is similar to the following:

    (Versions earlier than 7.x)

    {
      "took" : 31,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : 4,
        "max_score" : 0.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
            }
          ]
        }
      }
    }

    (Versions later than 7.x)

    {
      "took" : 3,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 4,
          "relation" : "eq"
        },
        "max_score" : null,
        "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
            }
          ]
        }
      }
    }

Step 4: Delete the Cluster

Once 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. Exercise caution when deleting a cluster.

  1. Log in to the CSS management console. In the left navigation pane, click Clusters to switch to the Clusters page.
  2. Locate the row where the Sample-ESCluster cluster resides and click More > Delete in the Operation column.
  3. In the displayed dialog box, click Yes.