Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# elastic search nest how to output the document score

I would like to output the score for each result from elastic search. But I'am unsure how I can get this.

Below is my current code for running a query:

  var searchResults = client.Search<Place>(s => s
             .From(0)
             .Size(5)
             .Explain(true)
             .TrackScores(true)
             .Query(q => q

                    .QueryString(fqqs1 => fqqs1
                        .OnFieldsWithBoost(d => d
                            .Add("name", 5.0)
                        )
                        .Query("west midlands birmingham")
                    )

             )
             .Sort(sort => sort.OnField("_score").Descending())
             .Sort(sort => sort.OnField(f => f.id).Ascending())
       );

// Output the results to console
        Console.WriteLine("\nTotal Hits: " + searchResults.HitsMetaData.Hits.Count + " out of " + searchResults.HitsMetaData.Total);

        List<Result> results = new List<Result>();
        foreach (Place result in searchResults.Documents)
        {
            results.Add(new Result
            {
                woeid = Convert.ToInt32(result.id),
                name = result.name,
                admin1 = result.admin1,
                admin2 = result.admin2,
                type = result.type
            });
            Console.WriteLine(result.id + " > " + result.name + " > " + result.admin1 + " > " + result.admin2 + " > " + result.type); 
        }
like image 762
Mannie Avatar asked Nov 14 '25 12:11

Mannie


1 Answers

use the .Hits property collection on the ISearchResponse<T> - The collection contains the score for each document in the .Score property, as well as the document in the .Source property.

like image 165
Russ Cam Avatar answered Nov 17 '25 08:11

Russ Cam



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!