Boosting boolean fields in Solr

后端 未结 2 1277
小蘑菇
小蘑菇 2021-02-08 21:42

Is it possible to boost boolean fields in Solr so that they receive a higher score?

We\'ve got an index which looks a bit like this:

  • document_id
  • t
相关标签:
2条回答
  • 2021-02-08 22:05

    Drupal

    Here is the solution for those who're using Drupal CMS.

    First, find your field name in Schema Browser at /solr/admin/schema.jsp

    Then, depending on the module which you use, try the following examples:

    Apachesolr module

    Code example:

    /**
     * Implements hook_apachesolr_query_alter().
     */
    function hook_apachesolr_query_alter(DrupalSolrQueryInterface $query) {
      $query->addParam('bq', array('is_review' =>
        '(is_review:true^100 OR is_review:false^0)'
      ));
    }
    

    Search Solr API module

    Code example:

    /**
     * Implements hook_search_api_solr_query_alter().
     */
    function hook_search_api_solr_query_alter(&$call_args, SearchApiQueryInterface $query) {
      $call_args['params']['bq'][] = '(is_review:true^100 OR is_review:false^0)';
    }
    
    0 讨论(0)
  • 2021-02-08 22:09

    Some query parsers have a feature dedicated to this kind of usage. For example, the dismax query parser has a boost query bq which allows you to boost documents which match a query by adding its clauses to the original query. There is also a boost function bf which allows you to multiply scores by the result of a function. For example, using is_review as this bf parameter,

    • the score of every document whose is_review field is undefined will be multiplied by 0.
    • the score of every document so that is_review=false will be multiplied by one.
    • the score of every documentso that is_review=true will be multiplied by two.

    is_review:true^100 shouldn't exclude non reviewed items unless you are using AND as the default query operator. In this case, you could try to replace is_review:true^100 by (is_review:true^100 OR is_review:false^0).

    If you are interested in the boost feature of the dismax query parser but would like to stick to the default query parser, you can use the boost query parser which will allow you to multiply the scores of any query with any function.

    0 讨论(0)
提交回复
热议问题