-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
This is more of a question rather than an issue.
How Can I index json data using NEST?
To elaborate more , lets take an example
Lets say we have a class Test and it has 2 properties Name & Id.
I want to store an object of this Test class in ElasticSearch.
Using the api, I can do this by making the following call:
var setting = new ConnectionSettings(new Uri("http://localhost:9200"));
var client = new ElasticClient(setting);
client.Index(row, "testindex", "test", row.Id);
where row is an Object is test class.
All this works great. Now lets say, instead of the row object, I have the object in Json format(i.e a string type).
If I call the same method again, it breaks giving the following error :
MapperParsingException[Malformed content, must start with an object]
var setting = new ConnectionSettings(new Uri("http://localhost:9200"));
var client = new ElasticClient(setting);
client.Index(jsonData, "testindex", "test", 2);
where jsonData is a string type object and is a json representation of a Test Object having the value - {"Name":"abc","Id":2}
Is there a way this json data can be handled or I can only index Objects?
I saw we can do this using PlainElastic.Net this way :
// 1. It provides ES HTTP connection
var connection = new ElasticConnection("localhost", 9200);
// 2. And sophisticated ES command builders:
string command = Commands.Index(index: "twitter", type: "user", id: test)
// 3. And gives you the ability to serialize your objects to JSON:
var serializer = new JsonNetSerializer();
var tweet = new Tweet { Name = "Some Name" };
string jsonData = serializer.ToJson(tweet);
// 4. Then you can use appropriate HTTP verb to execute ES command:
string response = connection.Put(command, jsonData);