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

http://solr-vs-elasticsearch.com/

http://www.elasticsearchtutorial.com/elasticsearch-vs-solr.html

http://stackoverflow.com/questions/10213009/solr-vs-elasticsearch

http://www.ymc.ch/en/why-we-chose-solr-4-0-instead-of-elasticsearch

Installing Elasticsearch

Download the required zip.

http://www.elasticsearch.org/download/

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.
EsEnvironmentVariables01

Now start your service:

\elasticsearch-0.90.10\bin\elasticsearch.bat

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:
EsFiddler02

Install Elastic HQ

Download the required zip and unpack:

http://www.elastichq.org/gettingstarted.html

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

Create a index:

http://localhost:9200/<index>/<type>/%5B<id>%5D

We’ll use the log4net.ElasticSearch json.
EsIndex04

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

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

EsIndex05

And now the indexes can be viewed in the HQ:
EsIndex06

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:

 <log4net>
    <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" />
      </layout>
      <connectionString value="Server=localhost;Index=log;Port=9200"/>
    </appender>
    <root>
      <level value="ALL"/>
      <appender-ref ref="ElasticSearchAppender" />
    </root>
  </log4net>
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:
https://github.com/damienbod/ElasticSearchLog4NetExample

Here’s the results in HQ:
EsIndex07

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

Links:

http://www.elasticsearch.org/

http://www.elastichq.org/

http://nest.azurewebsites.net/

https://github.com/jptoto/log4net.ElasticSearch

http://joelabrahamsson.com/elasticsearch-101/

http://exploringelasticsearch.com/book/an-overview/what-is-elasticsearch.html

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:

WordPress.com Logo

You are commenting using your WordPress.com 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: