Sorting solr search results using multiple fields (solrj)

12,039

If it looks like solrj is using just one of the sort fields, maybe you're using the SolrQuery#setSortField method instead of the addSortField like this:

solrQuery.addSortField("field1", ORDER.desc);
solrQuery.addSortField("field2", ORDER.desc);

In order to sort the results as you described, a cleaner solution would be adding a field to your index containing a kind of weight based on the entity type. Then you could directly sort using that field: weight desc, score desc. Before doing this, just make sure the weight has always to win against the solr score, otherwise you should work on influencing the solr score.

Share:
12,039
Admin
Author by

Admin

Updated on June 05, 2022

Comments

  • Admin
    Admin almost 2 years

    I need to sort the results I get back from apache solr based on two factors:

    There are three entities in our system that are indexed by solr (groups, projects and datasets) and in the results I want datasets to be displayed first, followed by projects and then groups; but I still want it to respect to score values for each of the types.

    So, for example: results would be

    1. Dataset with score of 0.325
    2. Dataset with score of 0.282
    3. Dataset with score of 0.200
    4. Project with score of 0.298
    5. Project with score of 0.186
    6. Group with score of 0.360
    7. Group with score of 0.270

    I'm doing this in java and using solrj to construct the solr queries. The problem is that when I try to add 2 sort fields to the SolrQuery object it only seems to use one of them. Also in the solr documents there isn't anything to state the entity type but the document's ID is prefixed with the entity name so I was planning to use that.

    If anyone has got any ideas on how I could achieve this it would be greatly appreciated as I've been stuck on this for a while now!

    Thanks in advance, -Jake.