Elastic Search using NEST Field Boosting

旧巷老猫 提交于 2019-11-30 05:10:32

You can achieve this through a combination of a boosting query and custom_score query

instead of boosting year we alter the score based on the year because:

(_score + 2013) > (_score + 1999)

Newer results will float to the top.

By using a boosting query we can effectively demote results that are missing the award field.

see: http://www.elasticsearch.org/guide/reference/query-dsl/boosting-query.html http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html

_client.Search<Entry>(s=>s
    .Query(q =>q
        .Boosting(bq=>bq
            .Positive(pq=>pq
                .CustomScore(cbf=>cbf
                    .Query(cbfq=>cbfq
                        .QueryString(qs => qs
                            .OnFieldsWithBoost(d =>
                                d.Add(entry => entry.Title, 5.0)
                                .Add(entry => entry.Description, 2.0)
                            )
                            .Query(searchText)
                        )
                    )
                    .Script("_score + doc['year'].value")
                )
            )
            .Negative(nq=>nq
                .Filtered(nfq=>nfq
                    .Query(qq=>qq.MatchAll())
                    .Filter(f=>f.Missing(p=>p.Award))
                )
            )
            .NegativeBoost(0.2)
        )
    )
);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!