searchsolrlucenesolrjlucidworks

Add variation to the Search Results based on a solr field


I have a solr field which has a set of values. Is it possible in solr to return results that are varied based on that field. Eg: My field contains "ValueA","ValueB" and "ValueC". So if rows is set to 3 then instead of returning all results from "ValueA" it should give me one from each field value (Considering they have the same scores)


Solution

  • You might want to use the Result Grouping / Field Collapsing or the CollapsingQParserPlugin.

    The CollapsingQParserPlugin is newer (since Solr 4.6), faster and more appropriate for your problem, I guess, as it does not effect the structure of the results.

    Just add this to your solrconfig.xml:

    <queryParser name="collapse" class="org.apache.solr.search.CollapsingQParserPlugin"/>
    

    You can then collapse your result by adding the following parameter to your query:

    fq={!collapse field=my_field}
    

    or in Solrj:

    solrQuery.addFilterQuery("{!collapse field=my_field}");
    

    Collapsing means: For each value in my_field it only retains the document with the highest score in the result set.