9

I'm very new to Elasticsearch and Want to know How to create index and index following json document to 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..."
        }
    ]
}
1

1 Answer 1

14

To create an index with NEST is as simple as

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

This will create an index with the default number of shards and replicas defined for the node.

To index a document represented as json into the 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 the low level client to index json, available in NEST through the .LowLevel property on ElasticClient.

To search the indexed document would be

// 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. The generic type parameter dynamic used in Search<T>() here means that the resulting documents will be deserialized to Json.Net JObject types.

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I tried with NEST version 7.10.1. It has syntax error at var indexResponse = _elasticClient.LowLevel.Index<string>("index-name", "type-name", json); . Error is The type 'string' cannot be used as type parameter 'TResponse' in the generic type or method 'IElasticLowLevelClient.Index<TResponse>(string, string, PostData, IndexRequestParameters)'. There is no implicit reference conversion from 'string'
Hey @Steve, the client has changed a little bit since this answer. Take a look at elastic.co/guide/en/elasticsearch/client/net-api/current/… and the note in elastic.co/guide/en/elasticsearch/client/net-api/current/…. Specifically, the response type is now StringResponse and no _type parameter for document type is supported

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.