Last couple of days I was experimenting with ElasticSearch and different client libraries for .NET. In this post I will detail the implementation of Indexing and searching using ElasticSearch in .NET. For detailed documentation of Elasticsearch visit the official site or read this Joel Abrahamsson post.

I use PlainElastic.Net as my Elastic search client. PlainElastic is a very simple lightweight library for Elasticsearch. It uses plain json for indexing and querying, this gives me more freedom and tooling to create json from user inputs for indexing and query.

To make it more flexible, our system gets data from the database using views. The Datareader class converts this data to Dynamic objects. Then converts it to json and passes it to PlainElastic for indexing. Dynamic object makes life easier as we can reuse this class with different views without worrying about strong types. Below is the Dynamic class I created.

Our views will fetch the data and return as IDataReader. While indexing data, the index helper will iterate through the reader and from the reader the data get loaded to Dynamic ElasticEntity as shown below.

Before indexing the ElasticEntity, it will be serialized to json using the solution provided in Stackoverflow, it uses JavaScriptSerializer and is very fast. Same approach used while deserializing the json result from Elasticsearch while searching. For deserializing Elasticsearch result, I used json.net initially but deserializing is very slow compare to Javascript serializer.

Below is my Elasticsearch Indexer. It’s a very simple class that uses PlainElastic.Net.

This post gives a very simplistic and basic view of our ElasticSearch layer that I created. We have more functionality tailored for our needs. Hope this post helps some one to build a system using ElasticSearch.