ElasticSearch REST - insert JSON string without using class

1.4k views Asked by At

I am looking for an example where we can push below sample JSON string to ElasticSearch without using classes in REST api.

{
   "UserID":1,
   "Username": "Test",
   "EmailID": "[email protected]"
}

We get the input as xml and we convert it to JSON string using NewtonSoft.JSON dll.

I know REST api is strongly typed. But is there any way to insert JSON string to Elastic without uses classes in REST api?

1

There are 1 answers

2
Rob On BEST ANSWER

You can use low level client to pass raw json.

var elasticsearchClient = new Elasticsearch.Net.ElasticsearchClient(settings);
var elasticsearchResponse = elasticsearchClient.Index("index", "type", "{\"UserID\":1,\"Username\": \"Test\",\"EmailID\": \"[email protected]\"}");

UPDATE

Based on documentation, try this one:

var sb = new StringBuilder();

sb.AppendLine("{ \"index\":  { \"_index\": \"indexname\", \"_type\": \"type\" }}");
sb.AppendLine("{ \"UserID\":1, \"Username\": \"Test\", \"EmailID\": \"[email protected]\" }");

sb.AppendLine("{ \"index\":  { \"_index\": \"indexname\", \"_type\": \"type\" }}");
sb.AppendLine("{ \"UserID\":2, \"Username\": \"Test\", \"EmailID\": \"[email protected]\" }");

var response = elasticsearchClient.Bulk(sb.ToString());