Json Document Index using Elasticsearch NEST C #

I am very new to Elasticsearch and want to know How to create index and index after json document for Elasticsearch using NEST C #?

{ "BookName": "Book1", "ISBN": "978-3-16-148410-0", "chapter" : [ { "chapter_name": "Chapter1", "chapter_desc": "Before getting into computer programming, let us first understand computer programs and what they..." }, { "chapter_name": "Chapter2", "chapter_desc": "Today computer programs are being used in almost every field, household, agriculture, medical, entertainment, defense.." }, { "chapter_name": "Chapter3", "chapter_desc": "MS Word, MS Excel, Adobe Photoshop, Internet Explorer, Chrome, etc., are..." }, { "chapter_name": "Chapter4", "chapter_desc": "Computer programs are being used to develop graphics and special effects in movie..." } ] } 
+5
source share
1 answer

Creating an index using NEST is as easy as

 var client = new ElasticClient(); client.CreateIndex("index-name"); 

This will create an index with the skull number and default replicas set for node.

To index a document represented as json into an index would be

 var json = @"{ ""BookName"": ""Book1"", ""ISBN"": ""978-3-16-148410-0"", ""chapter"" : [ { ""chapter_name"": ""Chapter1"", ""chapter_desc"": ""Before getting into computer programming, let us first understand computer programs and what they..."" }, { ""chapter_name"": ""Chapter2"", ""chapter_desc"": ""Today computer programs are being used in almost every field, household, agriculture, medical, entertainment, defense.."" }, { ""chapter_name"": ""Chapter3"", ""chapter_desc"": ""MS Word, MS Excel, Adobe Photoshop, Internet Explorer, Chrome, etc., are..."" }, { ""chapter_name"": ""Chapter4"", ""chapter_desc"": ""Computer programs are being used to develop graphics and special effects in movie..."" } ] }"; var indexResponse = client.LowLevel.Index<string>("index-name", "type-name", json); if (!indexResponse.Success) Console.WriteLine(indexResponse.DebugInformation); 

Here we use a low-level client to index json, available in NEST, through the .LowLevel property on ElasticClient .

To search for an indexed document will

 // refresh the index so that newly indexed documents are available // for search without waiting for the refresh interval client.Refresh("index-name"); var searchResponse = client.Search<dynamic>(s => s .Index("index-name") .Type("type-name") .Query(q => q .Match(m => m .Query("Photoshop") .Field("chapter.chapter_desc") ) ) ); 

This returns the document indexed. A generic parameter of type dynamic used in Search<T>() means that the resulting documents will be deserialized for Json.Net JObject types.

When we created the index, we did not specify a mapping for our type-name , so Elasticsearch deduced the mapping from the structure of the json document. This is a dynamic mapping and can be useful for many situations, however, if you know the structure of the documents that you are going to send, and it will not be destructively changed, then you specify the mapping for the type . The advantage of this in this particular example is that the chapter array will be output as an object type , but if you want to search for both the name of the chapter and the description of the chapter of a particular chapter, then you probably want to map chapter as an nested type .

+5
source

All Articles