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": "[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 Answer 1

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\": \"[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());
Sign up to request clarification or add additional context in comments.

2 Comments

Is there bulk insert method for doing this? Thanks.
Also the observation is - If we use NEST bulk insert then sometimes we get 'operation has timed out' error but with ElasticSearch.Net we dont get such error. Thanks again.

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.