Home > Back-end >  Sorting Innerhits results using Nest
Sorting Innerhits results using Nest

Time:12-07

I'm having trouble figuring out the correct usage of .Sort() for inner hits. If I have a document structure like:

{
  "id": "whatever",
  "sales": [
    {
      "startsOn": "2022-05-01T00:00:00"
      "endsOn": "2022-05-31T23:59:59"
      "discountPercent": 0.10
    },
    {
      "startsOn": "2022-06-01T00:00:00"
      "endsOn": "2022-06-31T23:59:59"
      "discountPercent": 0.15
    }
  ]
}

I'm trying to order the innerResults by startsOn descending and my InnerHits code looks something like:

  client.SearchAsync<Product>(x =>
    x.Query(q => 
      q.Nested(n => 
        n.Path(p => p.sales)
        .Query(q => q.DateRange( /* misc date range*/))
        .InnerHits(ih => ih.Name("sorted_Sales")))));     

I tried adding .Sort() after the .Name() but I get a

FieldSortDescriptor does not contain a definition for sales and no accessible exensiont method yada yada

CodePudding user response:

Below will sort inner hits with descending order using startsOn field.

.InnerHits(ih => ih
  .Name("sorted_Sales")
  .Sort(s => s
    .Descending(d => d.sales.FirstOrDefault()!.startsOn))))))
  • Related