ElasticSearch和Nest:为什么我错过了查询中的id字段?

Mat*_*att 4 elasticsearch nest

我创建了一个简单的对象,它代表一个会议,其中包含时间,位置,名称,主题等元素,并通过Nest在ElasticSearch中对其进行索引.它有一个Id字段,我留空,以便ES可以生成它们.

后来我检索了所有缺少GEO坐标的文档,以便我可以更新它们.我的所有返回元素对于id字段仍然为null,当我将它们更新回ES时,它会为它们创建新文档.

我在这里错过了什么让我的所有id都为空?

谢谢

这是Meeting类(id prop是多余的,但无论如何我都试过了)

[ElasticType(IdProperty = "Id")]
    public class Meeting
    {
        public string Id { get; set; }
        public string Code { get; set; }
        public string Day { get; set; }
        public string Town { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
        public string OriginalTime { get; set; }
        public string OriginalTimeCleaned { get; set; }
        public string Handicap { get; set; }
        public string FormattedAddress { get; set; }
        public Coordinates Coordinates { get; set; }
        public List<MeetingTime> Times = new List<MeetingTime>();
        public bool IsProcessed { get; set; }    
    }
Run Code Online (Sandbox Code Playgroud)

这是我如何检索会议

 public static List<Meeting> GetAddressesWithMissingCoordinates()
        {

            var result = Client.Search<Meeting>(s => s
                .Index("meetings")
                .AllTypes()
                .Query(p => p.Filtered(f => f.Filter(x => x.Missing(c => c.Coordinates)))));


            return result.Documents.ToList();
        }
Run Code Online (Sandbox Code Playgroud)

这是我的更新语句,Id为null

 public static void UpdateMeetingCoordinates(Meeting meeting, Coordinates coordinates)
        {
            meeting.Coordinates = coordinates;

            var response = Client.Index(meeting, u => u
                .Index("meetings")
                .Type("meeting")
                //.Id(meeting.Id.ToString())
                .Refresh()
                );

            Console.WriteLine(response);
        }
Run Code Online (Sandbox Code Playgroud)

我也试过了部分更新,没有运气.

Hol*_*olf 5

有一种获取内部ID的方法,如本问题中所述,请求此功能.

而不是使用response.Documents,而是这样做:

var results = response.Hits.Select(hit =>
    {
        var result = hit.Source;
        result.Id = hit.Id;
        return result;
    });
Run Code Online (Sandbox Code Playgroud)

  • 截至2015年1月26日,弹性团队在[同一期](https://github.com/elastic/elasticsearch-net/issues/1293#issuecomment-175410209)中表示,这将是未来的实施.我最终使用Hits而不是Documents来检索Id. (3认同)

Slo*_*ens 4

Elasticsearch 设置一个"_id"元数据参数(如果您未指定,它会为其选择一个值),但它不会文档源中设置该值。

为了说明这一点,如果我创建一个简单的索引:

PUT /test_index
Run Code Online (Sandbox Code Playgroud)

然后给它几个文件,而不指定"_id"

POST /test_index/doc/_bulk
{"index":{}}
{"id":null,"name":"doc1"}
{"index":{}}
{"id":null,"name":"doc2"}
Run Code Online (Sandbox Code Playgroud)

然后搜索:

POST /test_index/_search
Run Code Online (Sandbox Code Playgroud)

这就是我得到的结果:

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "AVEmuVlmj_RE0PsHCpza",
            "_score": 1,
            "_source": {
               "id": null,
               "name": "doc2"
            }
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "AVEmuVlmj_RE0PsHCpzZ",
            "_score": 1,
            "_source": {
               "id": null,
               "name": "doc1"
            }
         }
      ]
   }
}
Run Code Online (Sandbox Code Playgroud)

请注意,"_id"两个文档都设置了元数据参数,但"id"我传递的字段没有更改。这是因为,就 Elasticsearch 而言,"id"它只是另一个文档字段。

(这是我使用的代码:http://sense.qbox.io/gist/777dafae88311c4105453482050c64d69ccd09db