Search code examples
c#jsonelasticsearchkibananest

ElasticSearch Index/Insert with NEST fail


I'm trying to insert some JSON data into elastic search for testing.

here is the code:

    var node = new Uri("http://localhost:9200");
    var settings = new ConnectionSettings(node);        
    settings.DefaultIndex("FormId");

    var client = new ElasticClient(settings);

    var myJson = @"{ ""hello"" : ""world"" }";
    var response = client.Index(myJson, i => i.Index("FormId")
                        .Type("resp")
                        .Id((int)r.id)
                        .Refresh()
                        );

Nothing is inserted, and I get the following error from ES: {Invalid NEST response built from a unsuccesful low level call on PUT: /FormId/resp/1?refresh=true}

I've tried to find some example on that but all use a predefined structure of data, instead I want to use JSON data, with unstructured data.

The above error messsage is from NEST. Elastic replies (and write in the log) the following message: MapperParsingException[failed to parse]; nested- NotXContentException[Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes];

Failed to parse { ""hello"" : ""world"" } ????


Solution

  • A few observations:

    • the index name needs to be lowercase
    • the index will be automatically created when you index a document into it, although this feature can be turned off in configuration. If you'd like to also control the mapping for the document, it's best to create the index first.
    • use an anonymous type to represent the json you wish to send (you can send a json string with the low level client i.e. client.LowLevel if you want to, but using an anonymous type is probably easier).
    • The .DebugInformation on the response should have all of the details for why the request failed

    Here's an example to demonstrate how to get started

    void Main()
    {
        var node = new Uri("http://localhost:9200");
        var settings = new ConnectionSettings(node)
        // lower case index name
        .DefaultIndex("formid");
    
        var client = new ElasticClient(settings);
    
        // use an anonymous type
        var myJson = new { hello = "world" };
    
        // create the index if it doesn't exist
        if (!client.IndexExists("formid").Exists)
        {
            client.CreateIndex("formid");
        }
    
        var indexResponse = client.Index(myJson, i => i
            .Index("formid")
            .Type("resp")
            .Id(1)
            .Refresh()
        );
    }
    

    Now if we make a GET request to http://localhost:9200/formid/resp/1 we get back the document

    {
       "_index": "formid",
       "_type": "resp",
       "_id": "1",
       "_version": 1,
       "found": true,
       "_source": {
          "hello": "world"
       }
    }