Use Elasticsearch on Sitecore - Part I

Always wanted to add full-text search with highlighted search snippets, and search-as-you-type and did-you-mean suggestions to your Sitecore site? Elasticsearch is the right search engine for you and it is free!

Sitecore does not provide native support for Elasticsearch. Having said that there is some code that needs to be added in order to configure Elasticsearch on your site. Once it is done, you can enjoy all the features provided by Elasticsearch.

I am starting a series of articles talking about how to add Elasticsearch to your Sitecore.

What is Elasticsearch?

Elasticsearch is an open-source search engine based on Lucene. It is a distributed, scalable, real-time and analytics engine. It enables you to search, analyse, and explore your data. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents. It lets you perform and combine many types of searches — structured, unstructured, geo, metric.

Why use Elasticsearch?

You might be wondering why you should use Elasticsearch, and not simply Lucene or Solr? Even though those search engines cover mostly of the user needs, Elasticsearch provides a simple and a transparent way to do things.

Elasticsearch has intuitive APIs for monitoring and management that give you complete visibility and control. It is Resilient and highly available, you can run it on your laptop or hundreds of servers with petabytes of data.

Also, Elasticsearch is extensible, having a pack of powerful features such as X-Pack that gives you an enhanced experience with security, monitoring, alerting, reporting, graph exploration, and machine learning features.

Some of the usages include:

  • Combine data from Sitecore (product descriptions) and several web services (pricing)
  • Provide search ahead, search suggestions on the search page
  • Deliver a search experience that allowed for a high degree of facet/filter driven refinement
  • Add a synonym dictionary to conflate words that have pretty much the same meaning, such as jump, leap, and hop, or pamphlet, leaflet, and brochure and increase the accuracy of your search

Who is using Elasticsearch?

Wikipedia uses Elasticsearch to provide a full-text search with highlighted search snippets, and search-as-you-type and did-you-mean suggestions.

The Guardian uses Elasticsearch to combine visitor logs with social network data to provide real-time feedback to its editors about the public's response to new articles.

Stack Overflow combines full-text search with geolocation queries and uses more-like-this to find related questions and answers.

GitHub uses Elasticsearch to query 130 billion lines of code.

Prerequisites

Elasticsearch requires at least Java 8. It is recommended that you use the Oracle JDK version 1.8.0_73 (or superior). Before you install Elasticsearch, please check your Java version first by running the following in the Command Prompt:

java -version echo %JAVA_HOME%

You should see something like the below in the Command Prompt:

C:\>java -version 
java version "1.8.0_131" 
Java(TM) SE Runtime Environment (build 1.8.0_131-b11) 
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode) 
C:\>echo %JAVA_HOME% 
C:\Program Files\Java\jdk1.8.0_101

If anything goes wrong, please check your Java installation.

Setting up a local environment

Firstly, download the latest version of Elasticsearch (zip file) from elastic.co . By the time I wrote this article, version 5.4.0 is the latest version available.

Now, unzip the installation file and copy it to a place of your preference. I’ll be using the C:\ElasticSearch as my installation folder (Note: I renamed elasticsearch-5.4.0 to ElasticSearch).

To test the Elasticsearch, just run the C:\ElasticSearch\bin\elasticsearch.bat file and open localhost:9200 in a browser. You should get a JSON response like this:

{
    "name": "Cp8oag6",
    "cluster_name": "elasticsearch",
    "cluster_uuid": "AT69_T_DTp-1qgIJlatQqA",
    "version": {
        "number": "5.4.0",
        "build_hash": "f27399d",
        "build_date": "2016-03-30T09:51:41.449Z",
        "build_snapshot": false,
        "lucene_version": "6.5.0"
    },
    "tagline": "You Know, for Search"
}

If you are getting a “Could not find any executable java binary. Please install java in your PATH or set JAVA_HOME” error, please check if you have installed the Java JDK (just the JRE is not enough) and if the Environment Variables PATH and JAVA_HOME are properly configured.

Next, we are going to configure Elasticsearch to run as a Windows service and configure it to use a custom endpoint (e.g. we’ll use elasticserver.local:9200 instead of localhost:9200).

Firstly, add the following entry to the hosts file located in C:\Windows\System32\drivers\etc\hosts:

127.0.0.1 elastic.local

Create the folders Data and Logs on the root of the Elasticsearch installation. You should have something like the below:

C:\ElasticSearch\data 
C:\ElasticSearch\logs

Now open the configuration file {installation path}\config\elasticsearch.yml and update the following entries:

path.data: C:\ElasticSearch\data 
path.logs: C:\ElasticSearch\logs 
network.host: elastic.local 
http.port: 9200

Finally, let’s install Elasticsearch service.

Open the Command Prompt and navigate to the installation folder. Execute the command below:

bin\service.bat install

Open Services management console (services.msc) and find “Elasticsearch 2.3.4 (elasticsearch-service-x64)” service. Change Startup Type to Automatic and Start the service.

To test the Elasticsearch, open elastic.local:9200 in a browser. You should get the same JSON response you’ve got before.

Last, but not least, we’ll install kopf , a nice web administration tool for Elasticsearch. This tool offers an easy way of performing common tasks on an Elasticsearch cluster.

The rest menu is a really nice resource. You will find several HTTP request examples in the Elasticsearch documentation, and kopf presents you a nice way to write HTTP requests to test and use on Elasticsearch server.

In order to install kopf, run the following in the Command Prompt:

C:\ElasticSearch\bin\plugin.bat install lmenezes/elasticsearch-kopf

Don’t forget to replace C:\ElasticSearch with your ElasticSearch installation path.

Now, open elastic.local:9200/_plugin/kopf in a browser. You should see something like the following:

In this article, you learned how to setup a local environment with ElasticSearch and how to install kopt, a nice web tool for administering ElasticSearch server. In the next articles, I'll be talking about adding Elasticsearch capabilities to Sitecore.

comments powered by Disqus