如何在带有高亮的NEST弹性搜索中基于命中分数排序结果

本文关键字:搜索 于命中 结果 排序 高亮 NEST | 更新日期: 2023-09-27 17:55:04

我正在使用NEST(c#)与Elasticsearch进行通信,版本1.7.3

我正在传递一个字符串并尝试在该字符串中多匹配字段。我得到的亮点,以确定有多少字段匹配的字符串。返回Hits.Select.

的结果

但问题是,有时在highlight中最匹配的字段不会出现在Hits.Select列表的顶部。有什么可以纠正的吗?

            var result = this.client.Search<PInfo>(s => s
                .Take(20)
                .TrackScores(true)
                .Query(q => q
                .MultiMatch(m => m
                .Type(TextQueryType.CrossFields)
                .OnFieldsWithBoost(b => b
                .Add(f => f.A, 1.0)
                .Add(f => f.B, 1.0)
                .Add(f => f.C, 1.0)
                )
                .Operator(Operator.Or)
                .Query(text)
                ))
                .Highlight( h => h
                //.PreTags("<b>")
                //.PostTags("</b>")
                .OnFields(
                fk => fk.OnField( a => a.A),
                fk => fk.OnField( a => a.B),
                fk => fk.OnField( a => a.C)
                )
                )
                .Sort(sort => sort.OnField("_score").Descending())
                );
            string value = result.ConnectionStatus.ToString();

            return result
                .Hits
                .Select(c => new PInfo
                {
                    Id = c.Source.Id,
                    A = c.Source.A,
                    B = c.Source.B,
                    C = c.Source.C,
                    IndexedOn = c.Source.IndexedOn,
                    Highlights = c.Highlights // returning the highlights too from here
                })
                .ToList();

如何在带有高亮的NEST弹性搜索中基于命中分数排序结果

这应该会得到期望的结果。

            return result
                .Hits
                .Select(c => new PatientInfo
                {
                    Id = c.Source.Id,
                    Patient_Num = c.Source.Patient_Num,
                    First_Name = c.Source.First_Name,
                    Last_Name = c.Source.Last_Name,
                    Full_Name = c.Source.Full_Name,
                    Address = c.Source.Address,
                    City = c.Source.City,
                    State = c.Source.State,
                    Zip = c.Source.Zip,
                    Relation_Code = c.Source.Relation_Code,
                    DOB = c.Source.DOB,
                    Sex = c.Source.Sex,
                    IndexedOn = c.Source.IndexedOn,
                    Highlights = c.Highlights
                })
                .OrderByDescending(t => t.Highlights.Count).ToList();