juni 08, 2016
Sitecore’s content search provider is a very powerful part of the product. With built in support for Lucene and Solr and extensions from commercial vendors such as Coveo; text search, filtering and faceting has become a real strength of the Sitecore product. One glaring omission is Sitecore’s lack of support for Elastic Search.
Your first question is probably “why would I need another search/indexing option in Sitecore?” Your scepticism is justified. I am not suggesting Elastic is better– but it does have some unique strengths. Personally the schema-less nature of Elastic indices is a big draw and I have found the REST-ful JSON API to be more intuitive to the uninitiated. If you couple that with some of the other products in the Elastic ecosystem some interesting capabilities emerge - logstash (data processing) and kibana (data visualization) would be top of mind.
I was asked to evaluate Elastic Search as a possible means to drive a search UI on the published site. It was clear that custom indexing was required and the organization has made some previous investments in Elastic Search. The driving requirements were the need to:
- 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
The following post provides the steps required to get Elastic running and indexing Sitecore content.
Install Elastic Search and ElasticHQ
One of the quickest ways to get Elastic up and running is the Bitnami installers. Bitnami will setup the needed software and register Windows services.
Elastic’s native bits do not include any UI. I would recommend the ElasticHQ plugin. To install ElasticHQ open a command prompt and execute the install command (JAVA_HOME environment variables need to be set to execute this command).
- Change directory to elasticsearch\bin
- Execute plugin –install royrusso/elasticsearch-HQ
Once ElasticHQ is installed you will have a UI available at http://localhost:9200/_plugin/hq/.
Loading some Sample Data
Before we start integrating with Sitecore I like to confirm that the Elastic product is working. Luckily the fine folks at Elastic have covered the loading of some sample data in this tutorial.
You will need to:
- Install Curl (http://curl.haxx.se/download.html) and put in in your Windows path
- Download the sample JSON data - which happen to be works of Shakespeare
- Execute the import command using CURL
curl -XPUT localhost:9200/_bulk --data-binary @shakespeare.json
If this worked you should be able to execute a query in the ElasticHQ interface.
Tip: you will often encounter examples in the Elastic documentation that rely on CURL to execute the REST API. The examples all presume a Linux/Unix environment where a command can span multiple lines. For example:
In a Windows environment this is not really possible. Instead take the parameters and put them in a file. You can then execute the above command as:
curl –XPUT –http://localhost:9200/sharespeare/ -d @paramfile.json
Getting .NET / Sitecore Code to talk to Elastic
There are a number of .NET clients for Elastic. Our preference is NEST. To install NEST in your solution, execute the following command from the Nuget console.
With NEST installed connecting to Elastic and indexing is very straight forward.
1. Add the following using statement:
2. Establish a connection, in this case to an index called sitecore_es_index
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node, "sitecore_es_index");
var client = new ElasticClient(settings);
3. Index an object (the object is a simple C# POCO)
A few quick notes:
- If the class of your POCO has a property “Id”, Elastic will interpret this as the unique identifier of the document. If that ID is already in use an update in the index is performed.
- The class of your POCO will be used as the document type. This is an important concept in Elastic – multiple document types in a single index. More details can be found here.
For more on the NEST library check out http://nest.azurewebsites.net/.
How you decide to build your index is really up to you. Scheduled tasks and publish:end events are both popular choices. If immediacy is important publish:end is the way to go.
Please do not add Elastic into your solution stack just for the fun of it. In a scaled Sitecore 8 environment Solr is the recommended path for xDB indices. If configured to do so, Solr will manage all native Sitecore indices. If your custom indexing needs can be met with new indices and some simple computed fields, the added complexity of Elastic is probably not worth the investment.
If you want to explore Elastic Search further some attempts have been made to build an entire search provider for Elastic Search.
The decision to add in Elastic as well must be worth the added complexity and infrastructure.