NEST 2.0 doesn't persist some fields into ElasticSearch 2.0

落花浮王杯 提交于 2019-12-12 03:36:44

问题


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?


回答1:


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"}


来源:https://stackoverflow.com/questions/35745689/nest-2-0-doesnt-persist-some-fields-into-elasticsearch-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!