2

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": "Test@TestElastic.com"
}

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?

Rob
  • 9,664
  • 3
  • 41
  • 43
Sameer Deshmukh
  • 1,339
  • 4
  • 13
  • 20

1 Answers1

2

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\": \"Test@TestElastic.com\"}");

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\": \"Test@TestElastic.com\" }");

sb.AppendLine("{ \"index\":  { \"_index\": \"indexname\", \"_type\": \"type\" }}");
sb.AppendLine("{ \"UserID\":2, \"Username\": \"Test\", \"EmailID\": \"Test@TestElastic.com\" }");

var response = elasticsearchClient.Bulk(sb.ToString());
Rob
  • 9,664
  • 3
  • 41
  • 43