Working With Elasticsearch

TABLE OF CONTENTS

Search functionality in the Znode WebStore is built on top of Elasticsearch. Each catalog in Znode is published to Elasticsearch as a single independent Elasticsearch index.

As a Znode developer, it's typically unnecessary to directly interact with Elasticsearch. However, it's helpful to understand a few simple Elasticsearch commands that can be useful during development.


In Znode the following layered structure is used to provide to implement Search in Znode. 

  1. Admin UI Layer - Znode Admin screens that expose Znode’s search functionality as configuration
  2. API Layer - Znode endpoints that expose Znode’s search functionality as a web service - Znode endpoints that expose Znode’s search functionality as a web service
  3. Service Layer - Znode logic that uses Nest to interact with Elasticsearch
  4. Elasticsearch Nest Client - Elasticsearch client
  5. Elasticsearch - A search engine that uses Lucene and exposes JSON-based API
  6. Apache Lucene -  Core search library that does all language processing/analysis

Elasticsearch Configuration

The Elasticsearch server to which Znode points is configured in the API's web.config. More detail about the ElasticsearchRootUri setting is here.

Elasticsearch index name configuration is stored in the SQL database. More detail about Elasticsearch index names is here.

Elasticsearch API

Elasticsearch itself provides access to all of its functionality via its built-in RESTful API that is automatically available on any installation. By default, this API is hosted on port 9200 of the machine it is installed on.

It's possible to verify that Elasticsearch is running properly, by simply sending a GET request to the root of the Elasticsearch API. Quite simply, navigating to http://localhost:9200/ in a browser will send this request and show the result. The result should be something like the following:

{
"name" : "System_Name",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "RoLayOlSTjOpJwg5nTJKgw",
"version" : {
"number" : "7.16.2",
"build_flavor" : "default",
"build_type" : "zip",
"build_hash" : "2b937c44140b6559905130a8650c64dbd0879cfb",
"build_date" : "2021-12-18T19:42:46.604893745Z",
"build_snapshot" : false,
"lucene_version" : "8.10.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}


Listing All Indices

A summary of all Elasticsearch indices can be viewed by loading the following URL in a browser: http://localhost:9200/_cat/indices?v. The result will look something like the following:

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

yellow open carsuppliesindex Yz2zOC-8R8yP3DadpBGfpQ 5 1 129 0 101.4kb 101.4kb

yellow open finefoodindex fXKyiPu3R6y1sYXCRajX0Q 5 1 448 0 3.8mb 3.8mb

This example shows that there are 2 indices, corresponding to 2 different catalogs in Znode. The docs.count and other properties will grow as more products are added to the catalog and published.

See List All Indices in Elasticsearch's official documentation for more info.

Deleting Indices

Under normal circumstances, it is not necessary to rebuild indices in Elasticsearch. However, it can be helpful during development, when diagnosing functional/performance issues or migrating data between environments.

Like many Elasticsearch operations, deleting an index is as simple as sending an HTTP request to the Elasticsearch REST API. An HTTP DELETE request must be sent to the URL http://localhost:9200/{index_name} where {index_name} must be replaced with the name of the index to delete. For example, to delete the index for the CarSupplies catalog, a DELETE request must be sent to http://localhost:9200/carsuppliesindex.

See Delete Index in Elasticsearch's official documentation for more info about deleting an index.

Kibana for Elasticsearch Data Visualization

An official, open-source, Elasticsearch visualization tool can be easily installed to make it easier to audit/understand the data held in Elasticsearch. The tool is called Kibana.

Here is an example of a Kibana dashboard built to visualize timing data:

kibana

Resolving a duplicate Elasticsearch index

If there is a publishing issue, the administrator can see the application logs to determine if there is a duplicate Elasticsearch index. The duplicate index needs to be deleted and a full store publish including the store, catalog, cms should be done. The following options can be run.

  1. Clear Publish Data
    1. Go into Admin > Dev Center > Diagnostic & Maintenance > Maintenance > Clear Data

    2. Do a full store publish

    3. Verify publish works, if it still errors, proceed to step 2.

  2. Manually delete the Elasticsearch index (Only do this if step 1 fails.
    1. Go into Admin >Marketing> Site Search (Search Settings > Manage PIM Indexes

    2. Take note of the index name

    3. Run the following PowerShell command

      1. Invoke-WebRequest -Uri

        Invoke-WebRequest -Uri http://localhost:9200/IndexToBeDeleted -Method delete


      2. Were the localhost is the Elasticsearch URL and the index name is the index to be deleted (in step 2a)

      3. Run command, you should see a successful return

      4. verifying the Index is deleted from http://localhost:9200/_cat/indices?v

      5. Do a full store publish.

Official Elasticsearch Documentation

Official full documentation for Znode's version of Elasticsearch, v7.16.2, is available on Elastic's website here.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.