doc-exports/docs/css/umn/css_01_0007.html
Zheng, Xiu 0c90df93b1 CSS UMN 20230404 Version
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Co-authored-by: Zheng, Xiu <zhengxiu@huawei.com>
Co-committed-by: Zheng, Xiu <zhengxiu@huawei.com>
2023-04-05 08:45:09 +00:00

303 lines
18 KiB
HTML

<a name="css_01_0007"></a><a name="css_01_0007"></a>
<h1 class="topictitle1">Getting Started with Elasticsearch</h1>
<div id="body0000001282179454"><p id="css_01_0007__p173715321378">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:</p>
<ul id="css_01_0007__ul13335102293410"><li id="css_01_0007__li1867161816352"><a href="#css_01_0007__section96881833619">Step 1: Create a Cluster</a></li><li id="css_01_0007__li13335192283412"><a href="#css_01_0007__section398512163445">Step 2: Import Data</a></li><li id="css_01_0007__li1733518226347"><a href="#css_01_0007__section167624221443">Step 3: Search for Data</a></li><li id="css_01_0007__li203359221346"><a href="#css_01_0007__section75027114374">Step 4: Delete the Cluster</a></li></ul>
<div class="section" id="css_01_0007__section15177859183319"><h4 class="sectiontitle">Scenario Description</h4><p id="css_01_0007__p98074268319">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.</p>
<p id="css_01_0007__p94791144716">This section describes how to use Elasticsearch to provide the search function for users.</p>
<p id="css_01_0007__p16542124819143">Assume that the e-commerce website provides the following data:</p>
<pre class="screen" id="css_01_0007__screen118951327115918">{
"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"}
]
}</pre>
</div>
<div class="section" id="css_01_0007__section96881833619"><a name="css_01_0007__section96881833619"></a><a name="section96881833619"></a><h4 class="sectiontitle">Step 1: Create a Cluster</h4><p id="css_01_0007__p898232016369">Create a cluster using Elasticsearch as the search engine. In this example, suppose that you create a cluster named <span class="parmname" id="css_01_0007__parmname93661543523"><b>Es-xfx</b></span>. This cluster is used only for getting started with Elasticsearch. For this cluster, you are advised to select <span class="parmvalue" id="css_01_0007__parmvalue63521310203014"><b>css.medium.8</b></span> for <span class="parmname" id="css_01_0007__parmname11961113171811"><b>Node Specifications</b></span>, <span class="parmvalue" id="css_01_0007__parmvalue612116254209"><b>Common I/O</b></span> for <span class="parmname" id="css_01_0007__parmname08622023155111"><b>Node Storage Type</b></span>, and <span class="parmvalue" id="css_01_0007__parmvalue13449183511816"><b>40 GB</b></span> for <span class="parmname" id="css_01_0007__parmname1299602881818"><b>Node Storage Capacity</b></span>. For details, see <a href="css_01_0011.html">Creating an Elasticsearch Cluster in Non-Security Mode</a>.</p>
<p id="css_01_0007__p9981114851314">Create a cluster using Elasticsearch as the search engine. In this example, suppose that you create a cluster named <span class="parmvalue" id="css_01_0007__parmvalue4187134718420"><b>Sample-ESCluster</b></span>. This cluster is used only for getting started with Elasticsearch. For this cluster, you are advised to select <strong id="css_01_0007__b191855817415">ess.spec-4u8g</strong> for <strong id="css_01_0007__b168451802614">Node Specifications</strong>, <span class="parmvalue" id="css_01_0007__parmvalue121817581546"><b>High I/O</b></span> for <span class="parmname" id="css_01_0007__parmname10193580410"><b>Node Storage Type</b></span>, and <span class="parmvalue" id="css_01_0007__parmvalue141955811416"><b>40 GB</b></span> for <span class="parmname" id="css_01_0007__parmname201911582041"><b>Node Storage Capacity</b></span>. For details, see or .</p>
<p id="css_01_0007__p1358232011214">After you create the cluster, switch to the cluster list to view the created cluster. If the <strong id="css_01_0007__b3483163615715">Status</strong> of the cluster is <strong id="css_01_0007__b5485173611579">Available</strong>, the cluster is created successfully. </p>
</div>
<div class="section" id="css_01_0007__section398512163445"><a name="css_01_0007__section398512163445"></a><a name="section398512163445"></a><h4 class="sectiontitle">Step 2: Import Data</h4><p id="css_01_0007__p109714181508"><span id="css_01_0007__text7335958205618">CSS</span> 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.</p>
<ol id="css_01_0007__ol8733172515314"><li id="css_01_0007__li7655153392911">On the <span class="uicontrol" id="css_01_0007__uicontrol2655733162910"><b>Clusters</b></span> page, locate the target cluster and click <strong id="css_01_0007__b820219462171">More</strong> &gt; <strong id="css_01_0007__b07204415178">Cerebro</strong> in the <span class="uicontrol" id="css_01_0007__uicontrol18655533122910"><b>Operation</b></span> column to go to the Cerebro login page.<ul id="css_01_0007__ul75232028195712"><li id="css_01_0007__li20523122816570">Non-security cluster: Click the cluster name on the Cerebro login page to go to the Cerebro console.</li><li id="css_01_0007__li134787055820">Security cluster: Click the cluster name on the Cerebro login page, enter the username and password, and click <span class="uicontrol" id="css_01_0007__uicontrol15959165212110"><b>Authenticate</b></span> to go to the Cerebro console. The default username is <strong id="css_01_0007__b39827080224321">admin</strong> and the password is the one specified during cluster creation.</li></ul>
</li></ol><ol start="2" id="css_01_0007__ol721112814243"><li id="css_01_0007__li221115281242">In the navigation pane of Kibana on the left, choose <strong id="css_01_0007__b830264193410">Dev Tools</strong>.<p id="css_01_0007__p821152882417">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.</p>
<div class="fignone" id="css_01_0007__fig1830133281516"><span class="figcap"><b>Figure 1 </b>Console page</span><br><span><img id="css_01_0007__image357642471516" src="en-us_image_0000001554697245.png"></span></div>
<div class="note" id="css_01_0007__note681964235017"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="css_01_0007__p188211742195017">The Kibana UI varies depending on the Kibana version.</p>
</div></div>
</li><li id="css_01_0007__li11211328172416">On the <strong id="css_01_0007__b64617712383">Console</strong> page, run the following command to create index named <span class="parmname" id="css_01_0007__parmname23541417123812"><b>my_store</b></span>:<div class="p" id="css_01_0007__p1583315530414">(Versions earlier than 7.<em id="css_01_0007__i1860645555612">x</em>)<pre class="screen" id="css_01_0007__screen1112385214416">PUT /my_store
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"products": {
"properties": {
"productName": {
"type": "text",
"analyzer": "ik_smart"
},
"size": {
"type": "keyword"
}
}
}
}
}</pre>
</div>
<p id="css_01_0007__p14123195294112">(Versions later than 7.<em id="css_01_0007__i1295314811464">x</em>)</p>
<pre class="screen" id="css_01_0007__screen13127155234115">PUT /my_store
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties": {
"productName": {
"type": "text",
"analyzer": "ik_smart"
},
"size": {
"type": "keyword"
}
}
}
}</pre>
<p id="css_01_0007__p81281952134119">The command output is similar to the following:</p>
<pre class="screen" id="css_01_0007__screen1584472084814">{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "my_store"
}</pre>
</li><li id="css_01_0007__li10211102892411">On the <strong id="css_01_0007__b842352706152128">Console</strong> page, run the following command to import data to index named <span class="parmname" id="css_01_0007__parmname1507511166152141"><b>my_store</b></span>:<p id="css_01_0007__p182011209264">(Versions earlier than 7.<em id="css_01_0007__i374519764720">x</em>)</p>
<pre class="screen" id="css_01_0007__screen980815170266">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"}
</pre>
<p id="css_01_0007__p143178175282">(Versions later than 7.<em id="css_01_0007__i829344664711">x</em>)</p>
<pre class="screen" id="css_01_0007__screen215755817493">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"}</pre>
<p id="css_01_0007__p1280919176267">If the value of the <span class="parmname" id="css_01_0007__parmname1301102314392"><b>errors</b></span> field in the command output is <span class="parmvalue" id="css_01_0007__parmvalue5302122313392"><b>false</b></span>, the data is imported successfully.</p>
</li></ol>
</div>
<div class="section" id="css_01_0007__section167624221443"><a name="css_01_0007__section167624221443"></a><a name="section167624221443"></a><h4 class="sectiontitle">Step 3: Search for Data</h4><ul id="css_01_0007__ul1826814131366"><li id="css_01_0007__li826816134618"><strong id="css_01_0007__b83672071469">Full-text search</strong><p id="css_01_0007__p1123422620167">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 example shows the command to be executed on Kibana and the command output.</p>
<p id="css_01_0007__p13713546141016">Command to be executed on Kibana:</p>
<p id="css_01_0007__p1965195514348">(Versions earlier than 7.<em id="css_01_0007__i4903102310480">x</em>)</p>
<pre class="screen" id="css_01_0007__screen18763165016431">GET /my_store/products/_search
{
"query": {"match": {
"productName": "spring jeans"
}}
}</pre>
<p id="css_01_0007__p5107155916342">(Versions later than 7.<em id="css_01_0007__i1356582619484">x</em>)</p>
<pre class="screen" id="css_01_0007__screen16560124212525">GET /my_store/_search
{
"query": {"match": {
"productName": "spring jeans"
}}
}</pre>
<p id="css_01_0007__p264111561320">The command output is similar to the following:</p>
<pre class="screen" id="css_01_0007__screen9867132115210">{
"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"
}
}
]
}
}</pre>
<ul id="css_01_0007__ul1515122217247"><li id="css_01_0007__li61511122152415">Elasticsearch supports full-text search. The preceding command searches for the information about all commodities whose names include "spring" or "jeans".</li><li id="css_01_0007__li14281247258">Unlike traditional databases, Elasticsearch can return results in milliseconds by using inverted indexes.</li><li id="css_01_0007__li1345681018563">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 products contain only "spring". Therefore, the first two commodities rank prior to the last two due to high keyword match.</li></ul>
</li></ul>
<ul id="css_01_0007__ul4149154594416"><li id="css_01_0007__li1093164683813"><strong id="css_01_0007__b129772018038">Aggregation result display</strong><p id="css_01_0007__p19910171720276">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 products of different sizes. The following example shows the command to be executed on Kibana and the command output.</p>
<p id="css_01_0007__p207753616243">Command to be executed on Kibana:</p>
<p id="css_01_0007__p6929112817554">(Versions earlier than 7.<em id="css_01_0007__i1811735635315">x</em>)</p>
<pre class="screen" id="css_01_0007__screen1875916312110">GET /my_store/products/_search
{
"query": {
"match": { "productName": "spring" }
},
"size": 0,
"aggs": {
"sizes": {
"terms": { "field": "size" }
}
}
}</pre>
<p id="css_01_0007__p1623161075615">(Versions later than 7.<em id="css_01_0007__i1202165818539">x</em>)</p>
<pre class="screen" id="css_01_0007__screen48531811195511">GET /my_store/_search
{
"query": {
"match": { "productName": "spring" }
},
"size": 0,
"aggs": {
"sizes": {
"terms": { "field": "size" }
}
}
}</pre>
<p id="css_01_0007__p2102146133116">The command output is similar to the following:</p>
<p id="css_01_0007__p143635237016">(Versions earlier than 7.<em id="css_01_0007__i14912205412">x</em>)</p>
<pre class="screen" id="css_01_0007__screen4199113611531">{
"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
}
]
}
}
}</pre>
<p id="css_01_0007__p1649513220116">(Versions later than 7.<em id="css_01_0007__i6512431544">x</em>)</p>
<pre class="screen" id="css_01_0007__screen1240668181413">{
"took" : 27,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"sizes" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "L",
"doc_count" : 1
},
{
"key" : "M",
"doc_count" : 1
},
{
"key" : "S",
"doc_count" : 1
}
]
}
}
}</pre>
</li></ul>
</div>
<div class="section" id="css_01_0007__section75027114374"><a name="css_01_0007__section75027114374"></a><a name="section75027114374"></a><h4 class="sectiontitle">Step 4: Delete the Cluster</h4><p id="css_01_0007__p1972792353814">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.</p>
<div class="note" id="css_01_0007__note462220010358"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="css_01_0007__p13627600357">After you delete a cluster, its data cannot be restored. Exercise caution when deleting a cluster.</p>
</div></div>
<ol id="css_01_0007__ol197114498387"><li id="css_01_0007__li271949183818">Log in to the <span id="css_01_0007__text9242163412576">CSS</span> management console. In the navigation pane on the left, choose <span class="uicontrol" id="css_01_0007__uicontrol131976736917344"><b>Clusters</b></span> &gt; <strong id="css_01_0007__b19839183919612">Elasticsearch</strong>.</li><li id="css_01_0007__li671154953820">Locate the row that contains cluster <span class="parmname" id="css_01_0007__parmname47927612562"><b>Es-xfx</b></span> and click <span class="uicontrol" id="css_01_0007__uicontrol711114465463"><b>More</b></span> &gt; <strong id="css_01_0007__b025713403818">Delete</strong> in the <strong id="css_01_0007__b7112104617469">Operation</strong> column.</li><li id="css_01_0007__li13711149113813">In the displayed dialog box, enter the name of the cluster to be deleted and click <strong id="css_01_0007__b134517496378">OK</strong>.</li></ol>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="css_01_0006.html">Getting Started</a></div>
</div>
</div>