3

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"" } ????

1 Answer 1

11

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"
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Many thanks for the solution. I've used Newtonsoft to deserialize the json var stuff = JsonConvert.DeserializeObject(json);
Could you please tell me why you check for index existence (formid) if we already define it as default index?
formid is the name of the default index to use if no index is specified or can be inferred for the call, but this doesn't check that an index with the name passed to default index exists.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.