0

How to sort data using multiple filters in elastic search using NEST queries.

  1. I need to apply two fields lets say price and kilometer for cars in results set. I want the results sorted based on these fields by ASC or DESC.

  2. How to get top five results based on conditions or some filters.

Please provide some links if available.

1 Answer 1

1

This should answer your questions:

 IEnumerable<string> searchableFields = new List<string>() { "price", "kilometer" };

 SearchDescriptor<T> descriptor = new SearchDescriptor<T>();
 descriptor = descriptor.Size(5).OnFields(searchableFields)
                .Sort(s => s.OnField("price").Descending().OnField("kilometer").Ascending());

 var result = client.Search<T>(body => descriptor);
Sign up to request clarification or add additional context in comments.

2 Comments

Thats good. but Lets say i have a document set of 100 results. I want top five to be sorted using price and KM AND remaining 95 i want to apply other sort(other filter) but all 100 results should be in one object.
.OnFields() is not allowed with SearchDescriptor type object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.