Boosting an elasticsearch result based on a boolean field value

允我心安 提交于 2019-12-24 10:55:47

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/17029463/boosting-an-elasticsearch-result-based-on-a-boolean-field-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!