簡體   English   中英

如何在NEST彈性搜尋中按熱門成績排序結果

[英]How to sort result based on hit score in NEST elastic search with Highlights

我正在使用NEST(c#)與Elasticsearch版本1.7.3進行通信

我正在傳遞一個字符串,並試圖對該字符串中的多個字段進行匹配。 我正在查看要點,以找出字符串中匹配的字段數。 從Hits.Select還原結果。

但是問題是,有時Highlights中最匹配的字段不會出現在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();

這應該獲得期望的結果。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM