DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Doris vs Elasticsearch: A Comparison and Practical Cost Case Study
  • .NET Performance Optimization Techniques for Expert Developers
  • Advanced Maintenance of a Multi-Database Citus Cluster With Flyway
  • Getting Started With C# DataFrame and XPlot.Ploty

Trending

  • Optimize Deployment Pipelines for Speed, Security and Seamless Automation
  • Problems With Angular Migration
  • My LLM Journey as a Software Engineer Exploring a New Domain
  • Failure Handling Mechanisms in Microservices and Their Importance
  1. DZone
  2. Data Engineering
  3. Big Data
  4. ElasticSearch With .NET Core

ElasticSearch With .NET Core

I will show how to store JSON documents to ElasticSearch and then later leverage its REST API and DSL to perform various searches/queries,

By 
Jawad Hasan Shani user avatar
Jawad Hasan Shani
DZone Core CORE ·
Oct. 13, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
18.4K Views

Join the DZone community and get the full member experience.

Join For Free

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 them 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 a few posts about Elasticsearch and/or document databases. You can find the links in the 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 the reference section.

What Is a Document

Documents are essentially a set of key-value pairs.

naming conventions

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 on the 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.

elasticdemo

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

public class post

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.

class program

Persisting Data

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

var client

Here are 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. 

static void

Querying Data

ElasticSearch provides a very powerful API for these purposes. There are various types of queries you can perform. We will implement a 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 a few more records in elasticsearch using the 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 the official elastic-search docs link.

Here is the code for this query:

static void

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

studio debug console

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:

static void

and here is the output:

debug console

Term-Range Query

Returns documents that contain terms within a provided range.

Code:

code

Output:

Summary

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

References

  • https://dzone.com/articles/working-with-documents-using-elastic-search-and-no
  • https://dzone.com/articles/net-core-postgresql-and-document-database
Database Elasticsearch .NET POST (HTTP)

Published at DZone with permission of Jawad Hasan Shani. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Doris vs Elasticsearch: A Comparison and Practical Cost Case Study
  • .NET Performance Optimization Techniques for Expert Developers
  • Advanced Maintenance of a Multi-Database Citus Cluster With Flyway
  • Getting Started With C# DataFrame and XPlot.Ploty

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!