问题描述
这是我的文档:
[ElasticsearchType(Name = "MyDoc")]
public class MyDoc: Dictionary<string, object>
{
[String(Store = false, Index = FieldIndexOption.NotAnalyzed)]
public string text { get; set; }
}
正如你可以看到,它从继承字典&LT;字符串对象&gt;
这样我就可以dinamically字段添加到它(这是为了让聚集工作要求)
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)
下面我存储的映射:
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");
纪录得到实际存储,但文本字段不会保留。在这里,从JSON 2.0 ElasticSearch回
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"
}
}
如果我从 MyDoc删除基类
的文本
字段是正确保存,但显然字典内容不(我还需要删除。新增()
位作为文件没有从词典继承
)。
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?
推荐答案
对不起,我写错建议在我的previous后,所以我删除了。
我认为,问题实际上是系列化因为你的基类是字典
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
我会做两件事情首先尝试序列化对象,以输出字符串我是pretty确保文本被忽略。
I would do two things first try to serialize your object to see output string i am pretty sure that text is ignored.
其次,我会改变类以下
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。因为我认为问题是C#的一面,因为你从字典中继承,
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"}
这篇关于NEST 2.0不坚持某些字段为ElasticSearch 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!