Search code examples
phpluceneelasticsearchelastica

Boosting an elasticsearch result based on a boolean field value


I'm getting a lot of "static" when searching for the correct way to boost a result when a "Boolean" field type is TRUE, most results are talking about boolean searches.

N.B. We're using the php elastica library but if you can only provide json that's fine, I can manage from that.

I have an index with 5 fields where we have some built in boosting going on as you can see here:

array(
    'article_id' => array('type' => 'string', 'include_in_all' => FALSE),
    'subject' => array('type' => 'string', 'include_in_all' => TRUE, 'boost' => 8),
    'summary' => array('type' => 'string', 'include_in_all' => TRUE, 'boost' => 5),
    'content' => array('type' => 'string', 'include_in_all' => TRUE, 'boost' => 3),
    'media'  => array('type' => 'boolean', 'include_in_all' => FALSE),
    'pub_date'  => array('type' => 'date', 'include_in_all' => FALSE)
)

I'm also successfully boosting the results at a variety of levels via the pub_date field based on age using the addShould() method on a \Elastica\Query\Bool() query object.

What I would like to do now is boost results further where media is TRUE.

Can someone tell me how to add the appropriate boost for just the media field?


Solution

  • There are different ways to boost your results, have a look at this question to know more. Your usecase seems easy, you could do it at index time but I would rather do it at query time, just adding a should clause to your bool query and giving a proper boost to it if needed:

    {
        "bool" : {
            "must" : {
                ...
            },
            "should" : [
                {
                    .....
                },
                {
                    "term" : { "media" : "TRUE" }
                }
            ],
        }
    }
    

    The new clause would be optional, but it makes the score of the matching documents higher. If necessary you can add a specific boost to it, its value mainly depends on the other clauses.