ElasticSearch 和 Nest:为什么我在查询中缺少 id 字段?

2024-03-06

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

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

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

谢谢

这是 Meeting 类( id 属性是多余的,但我还是尝试了)

[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; }    
    }

这是我检索会议的方法

 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();
        }

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

 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);
        }

我也尝试过部分更新,但没有成功。


有一种方法可以获取内部 id,如中所述这个问题 https://github.com/elastic/elasticsearch-net/issues/1293#issuecomment-88151908请求这个功能。

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

var results = response.Hits.Select(hit =>
    {
        var result = hit.Source;
        result.Id = hit.Id;
        return result;
    });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ElasticSearch 和 Nest:为什么我在查询中缺少 id 字段? 的相关文章

随机推荐