Reputation: 11628
This is my document:
[ElasticsearchType(Name = "MyDoc")]
public class MyDoc: Dictionary<string, object>
{
[String(Store = false, Index = FieldIndexOption.NotAnalyzed)]
public string text { get; set; }
}
As you can see, it inherits from Dictionary<string, object>
so I can dinamically add fields to it (this is a requirement to make aggregation work)
Here I store the mapping:
client.Map<MyDoc>(m => m.Index("myindexname").AutoMap());
Now I create a new record and store it:
var rec= new MyDoc();
rec.Add("id", "mystuff");
rec.text = "mytext";
client.Index(rec, i => i.Index("myindexname"));
client.Refresh("myindexname");
The record get actually stored but the text field is not persisted. Here the JSON back from ElasticSearch 2.0
{
"_index": "myindexname",
"_type": "MyDoc",
"_id": "AVM3B2dlrjN2fcJKmw_z",
"_version": 1,
"_score": 1,
"_source": {
"id": "mystuff"
}
}
If I remove the base class from MyDoc
the text
field is stored correctly but obviously the dictionary content is not (I also need to remove the .Add()
bit as the document doesn't inherit from a Dictionary
).
How to store both the text
field and the dictionary content?
Upvotes: 0
Views: 334
Reputation: 19494
Sorry i wrote wrong suggestion in my previous post so i deleted it. I think issues is actually in serialization since your base class is Dictionary
I would do two things first try to serialize your object to see output string i am pretty sure that text is ignored.
Second i would change class to following
public class MyDoc : Dictionary<string, object>
{
public string text
{
get
{
object mytext;
return TryGetValue("text", out mytext) ? mytext.ToString() : null;
}
set { this.Add("text", value);}
}
}
PS. As i thought issue is on c# side since you inherit from dictionary,
var rec = new MyDoc();
rec.Add("id", "mystuff");
rec.text = "mytext";
//Text2 is property public string text2 { get; set; }
rec.text2 = "mytext2";
var test = JsonConvert.SerializeObject(rec); //{"id":"mystuff","text":"mytext"}
Upvotes: 1