/Elasticsearch_Example

Set up Elasticsearch By NEST Library with ASP.NET Core and Docker

Primary LanguageC#

Elasticsearch_Example

Set up Elasticsearch By NEST Library version 7.10.1 with ASP.NET Core 3.1 and Docker.

to run and test project,you must do the following steps first.

firstyou have to create network to watch Elasticsearch and Kibana on Docker by below command:

sudo docker create network esnetwork

Elasticsearch:

docker run --rm -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" --name elasticsearch --network esnetwork elasticsearch:7.9.3

Kibana:

docker run --rm -p 5601:5601 --name kibana --network esnetwork kibana:7.9.3

after run containers,now We can run and test project.

before runing and testing the project, it is better to talk about Key Concepts of the Elasticsearch

Key Concepts

The key concepts of Elasticsearch are as follows:

Node

It refers to a single running instance of Elasticsearch. Single physical and virtual server accommodates multiple nodes depending upon the capabilities of their physical resources like RAM, storage and processing power.

Cluster

It is a collection of one or more nodes. Cluster provides collective indexing and search capabilities across all the nodes for entire data.

Index

It is a collection of different type of documents and their properties. Index also uses the concept of shards to improve the performance. For example, a set of document contains data of a social networking application.

Document

It is a collection of fields in a specific manner defined in JSON format. Every document belongs to a type and resides inside an index. Every document is associated with a unique identifier called the UID.

Shard

Indexes are horizontally subdivided into shards. This means each shard contains all the properties of document but contains less number of JSON objects than index. The horizontal separation makes shard an independent node, which can be store in any node. Primary shard is the original horizontal part of an index and then these primary shards are replicated into replica shards.

Replicas

Elasticsearch allows a user to create replicas of their indexes and shards. Replication not only helps in increasing the availability of data in case of failure, but also improves the performance of searching by carrying out a parallel search operation in these replicas.

Comparison between Elasticsearch and RDBMS

In Elasticsearch, index is similar to tables in RDBMS (Relation Database Management System). Every table is a collection of rows just as every index is a collection of documents in Elasticsearch.

The following table gives a direct comparison between these terms.

Elasticsearch RDBMS
Cluster Database
Shard Shard
Index Table
Field Column
Document Row

to run CreateIndex api, call below url:

HttpGet --> http://localhost:5000/post/createIndex

to run reIndex api, call below url:

reIndex means :remove current index then create new index and bind data to new index.

HttpGet --> http://localhost:5000/post/reIndex

to run deleteDocument api, call below url:

HttpDelete --> http://localhost:5000 /post/deleteDocument/0f8fad5b-d9cb-469f-a165-70867728950e

to run searchDocumnet api, call bellow url:

HttpPost --> http://localhost:5000/post/searchDocument

body:{"query":"0c885dd3-7dd9-484b-9b20-3e6552bca144"}

to run addDocument api, call below url:

HttpPost --> http://localhost:5000/post/addDocument

body:

{
"Title": "title00222",
"Slug":"slug00222",
"Excerpt":"excerpt2222",
"Content":"content2222",
"IsPublished":true, 
"Categories":[
    "Game",
    "Clothing",
    "Food",
    "Art"
],
"Comments":[
{
"Id":1,
"Author":"javad barber",
"Email":"j.barber@yahoo.com",
"Content":"Description about barber Book"
},
{
"Id":3,
"Author":"mehdi shabani",
"Email":"mehdi@outlook.com",
"Content":"Description about Fitness Book"
}
]
}

to run editDocument api, call below url:

HttpPut --> http://localhost:5000/post/editDocument/0f8fad5b-d9cb-469f-a165-70867728950e

body:

{
"Title": "title00222",
"Slug":"slug00222",
"Excerpt":"excerpt2222",
"Content":"content2222",
"IsPublished":true, 
"Categories":[
    "Game",
    "Clothing",
    "Food",
    "Art"
],
"Comments":[
{
"Id":1,
"Author":"javad barber",
"Email":"j.barber@yahoo.com",
"Content":"Description about barber Book"
},
{
"Id":3,
"Author":"mehdi shabani",
"Email":"mehdi@outlook.com",
"Content":"Description about Fitness Book"
}
]
}