Search code examples
solrsolrnet

In Solr, how can we use terms external to the search query to bias result ordering?


We're working on a plan to identify content tags our users are interested in. So, for instance, we may determine that User X consumes content tagged with "kermit" and "piggy" more often than other tags. These are their "favored tags."

When the users search, we'd like to favor/bias documents that contain these terms.

This means we can't boost the documents at index time, because every user will have different favored tags. Additionally, they may not be searching for the favored tags themselves. They may search for "gonzo," and so we absolutely want to give them documents with "gonzo," but we want to boost documents that also contain "kermit" or "piggy."

These favored tags are not used to actually query the index, but rather are used to bias the result ordering. The favored tags become something of a tie-breaker -- all else being equal, documents containing these terms will rank higher.

This is new/planned development, so we can use whatever version and parser stack is optimal to solve this problem.

Solution in SolrNet

The question was correctly answered below, but here's the code for SolrNet just in case someone else is using it.

var localParams = new LocalParams();
localParams.Add("bq", "kermit^10000); //numeric value is the degree of boost

var solr = ServiceLocator.Current.GetInstance<ISolrOperations<MySolrDocumentClass>>();
solr.Query(new SolrQuery("whatever") + localParams);

Solution

  • You didn't specify which query parser you're using, but if you are using the Dismax or Extended Dismax query parser, the bq argument should do exactly what you're looking for. bq adds search criteria to a search solely for the purpose of affecting the relevancy, but not to limit the result set.

    From the Dismax documentation:

    The bq (Boost Query) Parameter

    The bq parameter specifies an additional, optional, query clause that will be added to the user's main query to influence the score. For example, if you wanted to add a relevancy boost for recent documents:

    q=cheese 
    bq=date:[NOW/DAY-1YEAR TO NOW/DAY]
    

    You can specify multiple bq parameters. If you want your query to be parsed as separate clauses with separate boosts, use multiple bq parameters.

    In this case, you may want to add &bq=kermit&bq=piggy to the end of your Solr query. If you aren't using one of these query parsers, this need may be exactly the motivation you need to switch.