ElasticSearch with .NET Core

Introduction

Working with NoSQL persistence simplifies a lot of things when building applications. You can build persistence functionalities from scratch or you can use an already available solution like elastic-search or combine the both.

In this post, I will show how to store JSON documents to ElasticSearch and then later leverage its REST API and DSL to perform various searches/queries easily using .NET Core.

I have previously written few posts about elasticsearch and/or document databases. You can find the links in reference section, if you want to refresh some background information about those topics.

What is ElasticSearch

Elasticsearch is a search engine based on the Lucene library. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents. Elasticsearch is developed in Java.

There are client libraries available for different languages e.g. Node.js, .NET Core etc. I’ve written some posts about ElasticSearch and NoSQL databases you can find in reference section.

What is a document

Documents are essentially a set of key-value pair.

NEST High Level Client

The high level client, ElasticClient, provides a strongly typed query DSL that maps one-to-one with the Elasticsearch query DSL. It can be installed from the Package Manager Console inside Visual Studio.

You can read more about NEST on this URL.

Demo Project Setup

You shall be running elasticsearch service on your computer. There is an installer available from official website and you can download and install it on your computer. I’ve explained this process in my previous post and you can check that one for details.

The code for the demo project is available on this git repository. To start with I created a .NET Core console application and added the NEST package via nuget.

In this project, we will be working with Post (blog post) records. The following picture shows how the Post model looks like:

Connecting to ElasticSearch

The following code block shows a typical way to connect to elastic-search. The code is self explanatory. Feel free to ask if something is not clear.

Persisting Data

Ok, now our basic setup in place, we can start with persisting data in elastic-search. Its actually very simple. NEST client library provides us a very simple to use method which we can leverage for our purposes.

Here is the method implementation details. We are writing to console , creating a post object and then calling the .IndexDocument method for persistence. You can adjust this method code as needed.

Querying Data

ElasticSearch provides a very powerful API for this purposes. There are various type of queries you can perform. We will implement few of those to understand the workflow. For more information about the API and query types, please check the official web-site of elasticsearch.

I also have inserted few more records in elasticsearch using previously mentioned method.

Match Query

This type of query returns documents that match a provided text, number, date or boolean value. The provided text is analyzed before matching.

The match query is the standard query for performing a full-text search, including options for fuzzy matching. You can find detailed information about this query on official elastic-search docs link.

Here is the code for this query:

Now, when I run the application, the following output shows the match-query result:

Match-Phrase Query

The match_phrase query analyzes the text and creates a phrase query out of the analyzed text. For more details, please visit this link.

Here is the method call and implementation code:

and here is the output:

Term-Range Query

Returns documents that contain terms within a provided range.

Code:

Output:

Summary

There is a lot other type of queries and functionalities you can use using NEST client. I have implemented few more in the source code, however, the code is very similar to the previously described code, feel free to check it.

References


Discover more from Hex Quote

Subscribe to get the latest posts sent to your email.

Discover more from Hex Quote

Subscribe now to keep reading and get access to the full archive.

Continue reading