Getting started with Elasticsearch and .NET

At present there are 2 main players in the full text searches for applications. Solr and Elasticsearch. Both are built using Lucene.

There’s a lot of dicussion about which is the best, but for me, Elasticsearch provides some better features and seems to be growing faster. I decided to choose Elasticsearch because you require no schema for the documents and can you can have nested documents. Here’s a set of links showing/explaining the pros and cons:

SOLR or Elasticsearch

Installing Elasticsearch

Download the required zip.

Unpack it and save it to your file system.

Set your JAME_HOME variable if not already set. This should be set to the latest installed jdk.

Now start your service:


This just starts it in the command line. You can also install it as a service if required.

Now check if it’s ok. Send a GET http://localhost:9200/_nodes/process?pretty in fiddler.

You should recieve a json answer:

Install Elastic HQ

Download the required zip and unpack:

Open the index.html and connect to http://localhost:9200

Create a index:


We’ll use the log4net.ElasticSearch json.

Or we can create a second index:
PUT http://localhost:9200/myfirstindex/demotype/1

"name": "damienbod",
"reason": "To say hello",
"place": "Switzerland"


And now the indexes can be viewed in the HQ:

Create a .NET client:
In the first example, a log4net is used and logged to the elasticsearch search engine. We mainly follow the instructions from Log4net.elasticsearch. Thanks for this.

Here’s the log4net config:

    <appender name="ElasticSearchAppender" type="log4net.ElasticSearch.ElasticSearchAppender, log4net.ElasticSearch">
      <!-- for .NET 40 <appender name="ElasticSearchAppender" type="log4net.ElasticSearch.ElasticSearchAppender, log4net.ElasticSearch.Net40">-->
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
      <connectionString value="Server=localhost;Index=log;Port=9200"/>
      <level value="ALL"/>
      <appender-ref ref="ElasticSearchAppender" />
using log4net;

namespace ConsoleElasticSearch
    class Program
        private static readonly ILog _log = LogManager.GetLogger(typeof(Program));

        static void Main(string[] args)
            for (int i = 1; i < 100; i++)
                _log.Info("console test log " + i);  

Here’s the sample application:

Here’s the results in HQ:

So now we’re ready to start learning how to use Elasticsearch. For how to setup the server properly in a production enviroment, refer to elasticsearch or buy a book.

MVC .NET Elasticsearch Tutorials:

Part 1: ElasticsearchCRUD introduction

Part 2: MVC application search with simple documents using autocomplete, jQuery and jTable

Part 3: MVC Elasticsearch CRUD with nested documents

Part 4: Data Transfer from MS SQL Server using Entity Framework to Elasticsearch

Part 5: MVC Elasticsearch with child, parent documents

Part 6: MVC application with Entity Framework and Elasticsearch


One comment

  1. Shyamsundar Naik · · Reply

    Can you help me in Facet Navigation in Web page in .dot net

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: