Query to get and Sort based on Most occurrence of a field value

Hi,

I am working on Recommendation engine where trying to retrieve the purchase data, I need to sort the data based on most occurrence means in order index productId is one of the field.
For ex :
o111, prod111,
o222, prod222,
o333, prod111,
o444, prod333,
o555, prod222,
o666, prod111

I want to get the data as below
o111, prod111,
o333, prod111,
o666, prod111,
o222, prod222,
o555, prod222,
o444, prod333,

I am using Java high level rest client, Please suggest me the elastic search query and java example if possible.

ThanksInAdvance.

You should be able to just use the query sorting capability to sord by productId ASC: https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-request-body.html#request-body-search-sort

In java, you can use a FieldSortBuilder: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search.html#_specifying_sorting

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.