Elasticsearch For .NET Developer : How to work with Elasticsearch using c#

In this post, we are going to take a look at how to integrate Elasticsearch with .NET so that it can be useful in your .NET based projects.
This post is part 3 of the series “elasticsearch for .net developer”. Before proceeding, I strongly recommend to check out previous parts listed below:

We will be building a very basic console search application called “SearchCars”  demonstrating connection to Elasticsearch, creating an index, adding and deleting documents, and querying data. So, Let’s get started with it.

1. Create a Console .NET Application

Create A Console App

 

2. Install NEST – A .NET Client for Elasticsearch

There are a couple of libraries out there to work with Elasticsearch with .NET.  The most popular and most used one is NEST library. It’s a C# implementation of the Elasticsearch API.  In fact, the NEST library is so good that it has been incorporated as the official .NET client library for Elasticsearch.

Navigate to the NuGet Package Manager and add the NEST library to the console app.

NEST Library

3. Connecting to the Elasticsearch using NEST library

Connecting to the Elasticsearch using NEST is quite easy. By-default NEST uses sensible configuration options for connecting to and interacting with an Elasticsearch cluster and provides configurable options to change according to the developer needs.

See the below ElasticConfig.cs class that has a method GetClient() that returns a connection object to the elasticsearch cluster(node1,node2..).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using Elasticsearch.Net;
using Nest;
using System;
 
namespace SearchApp
{
    public static class ElasticConfig
    {
        private static IElasticClient _client;
        static ElasticConfig()
        {
            var node1 = new Uri("http://localhost:9200/");
            var node2 = new Uri("http://152.30.11.192:9200/");
            var nodes = new Uri[]
            {
                node1
               //,node2
            };
            var connectionPool = new SniffingConnectionPool(nodes);
            var connectionSettings = new ConnectionSettings(connectionPool)
                                .SniffOnConnectionFault(false)
                                .SniffOnStartup(false)
                                .SniffLifeSpan(TimeSpan.FromMinutes(1));
            _client = new ElasticClient(connectionSettings);
        }
        public static IElasticClient GetClient()
        {
            return _client;
        }
    }
}

You can add many elasticsearch node according to your needs. For demo,here we are connecting to a single node only.

4. Define Data Model

Now, Lets create a C# model class to represent the cars data we want to index into the elasticsearch cluster.  See the below code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
 
namespace SearchApp.Model
{
    public class Car
    {
        public int? CarID { get; set; }
        public string Maker { get; set; }
        public string Color { get; set; }
        public Double? Milage { get; set; }
        public Double? Price { get; set; }
        public DateTime? CreatedDate { get; set; }
    }
}

 

5. Create Index

Index is analogous to a database in RDBMS which holds the documents. The NEST Create Index API is used to create an index. See the below code:

1
2
3
4
5
6
7
public void CreateIndex(string indexName)
        {
            if (!_client.IndexExists(indexName).Exists)
            {
                _client.CreateIndex(indexName);
            }
        }
6. Insert a Document

Inserting a document into a index in elasticsearch cluster is known as indexing. The NEST index API adds or updates a document. See the below code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Car car = new Car() { CarID = 1, Color = "Black", CreatedDate = DateTime.Now, 
                      Maker = "TOYOTA", Milage = 27.39, Price = 7170000 };
string indexName = "vehicles";
string indexType = "car";
 
 
public void Insert(Car car, string indexName, string indexType)
{
     _client.Index(car, i => i
           .Id(car.CarID)
           .Index(indexName)
           .Type(indexType)
           );
}

The above code insert a car object into the index “vehicles” with type info of “car”. Elasticsearch assigns a system id to the document if not specified while indexing the document.

7. Search a document

Now, we have data indexed within Elasticsearch and want to be able to search it.  The NEST Search API allows us to execute a search query and get back search hits that match the query. See the below code:

1
2
3
4
5
6
7
8
9
10
public Car getCarByID(string Id, string indexName, string typeName)
{
     var result = _client.Search(q => q
                       .Index(indexName)
                       .Type(typeName)
                       .Query(qq => qq.Match(m => m.Field(f => f.CarID).Query(Id))
                       ).Size(1));
 
    return result.Documents.FirstOrDefault();
}

The above method returns a car document by running a Match query on field “CarID”.

8. Update a document

The NEST Update API allows us to update a document. The Update operation gets the document from the index ,updates document and re-index back the document with an incremental version number. Updating an indexed document can be done in two way:

  • Update By Update API
  • Update By Index API
Update By Update API:

The Update API requires the document path and the updated document to update the document. This can be done in two ways :

Method – 1(Complete Document Replacement)

1
2
3
4
5
6
public void UpdateByScript(Car car, string indexName, string indexType)
{
      _client.Update(
             new DocumentPath(car), u =>
                  u.Index(indexName).Type(indexType).Doc(car));
}

By-default it uses the Id property to identify the document to be updated. If Id property is not available in the object, in that case no document will be updated.

You can directly pass the document ID to be updated as shown below:

1
2
3
4
5
6
7
public void UpdateByScript(Car car, string indexName, string indexType)
{
      _client.Update(
             new DocumentPath(car.CarID), u =>
                  u.Index(indexName).Type(indexType).Doc(car));
 
}

Method – 2(Partial Document Update)
The update API also has a Update variant, where T is the document type to update, and K is the partial document with fields to be updated. See the below code:

1
2
3
4
5
6
7
8
9
10
11
12
public class PartialCarObject
{
      public string CarID { get; set; }
      public string Color { get; set; }
}
public void UpdateByPartialDocument(PartialCarObject car, string indexName, string indexType)
{
      _client.Update<Car, PartialCarObject>(new DocumentPath(car.CarID),u => u
                 .Doc(car)
                 .RetryOnConflict(3)
                 );
}

 

Update By Index API :

Index API can be used to update data in document. Index API checks that if there is any document with given id. If the id is available then it update it with data passed and increment the version number. See the below code:

1
2
3
4
5
_client.Index(car, i => i
                    .Id(car.CarID)
                    .Index(indexName)
                    .Type(indexType)
                    );
9. Delete a Document

The delete API allows to delete a document from a specific index based on its id. See the below code:

1
2
3
4
5
6
public object DeleteDocument(string id, string indexName, string typeName)
{
     var response = _client.Delete(DocumentPath
            .Id(id), d => d.Index(indexName).Type(typeName).Refresh(Elasticsearch.Net.Refresh.True));
     return response;
}

 

10. Delete an Index

The delete index API allows you to delete an existing index.See the below code:

1
2
3
4
public void DeleteIndex(string indexName)
{
      _client.DeleteIndex(indexName);
}

So, it was all about How to work with Elasticsearch using c#, if you have any query then please comment below and let us know. If you liked this article, then please follow us on Facebook to get notification for new posts.

Rahul is a Data Geek, technology enthusiast, a passionate writer, thinker with passion for computer programming.  He loves to explore technology and finds ultimate joy when writing about trending technology, geek stuff and web development.