Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort result based on hit score in NEST elastic search

I am using NEST(c#) to communicate with Elasticsearch. However the results are not based on hit score, how can I sort my results based on hit score and also i am getting only 10 records. Below is my code sample

var result = client.Search(q => q
                               .Index(IndexName)
                               .From(0)
                               .Type("post")
                               .Fields("title","message")
                               .Size(10)
                               .Query(fq1 => fq1
                                  .QueryString(fqqs1 => fqqs1
                                    .OnFieldsWithBoost(d => d
                                      .Add("title", 7.0)
                                      .Add("message", 5.0))
                                      .Query(SearchQuery))));

my message field will contain large text , is it possible to get only few lines around the search keyword from the message just like google search result

like image 391
user2757044 Avatar asked Oct 20 '25 14:10

user2757044


1 Answers

Hello to sort by the hitscore just use

var result = client.Search(q => q
                 .Index(IndexName)
                 .From(0)
                 .Type("post")
                 .Fields("title","message")
                 .TrackScores(true)
                 .Size(10)
                 .Query(fq1 => fq1
                        .QueryString(fqqs1 => fqqs1
                            .OnFieldsWithBoost(d => d
                                .Add("title", 7.0)
                                .Add("message", 5.0)
                            )
                            .Query(SearchQuery)
                        )
                 .Sort(sort => sort.OnField("_score").Descending())
           );

To get more records just increase the number in the Size()

like image 114
danvasiloiu Avatar answered Oct 22 '25 03:10

danvasiloiu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!