使用NEST Field Boosting进行弹性搜索
我使用NEST强类型客户端在C#中使用弹性搜索。 我有一个包含条目的索引:
[ElasticType(Name = "Entry", IdProperty = "Id")] public class Entry { public string Id { get; set; } public string Title { get; set; } public string Description { get; set; } public string Award { get; set; } public int Year { get; set; } }
如果年份是参赛年份,例如2012年,奖励是参赛作品获奖的类型,可以为空。
然后我想使用boost来搜索这些条目以获得不同的属性。 在下面的代码中,我希望结果在Title上的排名高于在Description上匹配的结果。
private IQueryResponse GetMatchedEntries(string searchText) { return _elasticClient.Search( body => body.Query(q => q.QueryString(qs => qs.OnFieldsWithBoost(d => d.Add(entry => entry.Title, 5.0) .Add(entry => entry.Description, 2.0)) .Query(searchText)))); }
我现在被要求提高获奖者的成绩,并提升新的参赛作品(即年度)。
我该怎么做呢? 是作为索引服务的一部分还是作为搜索的一部分需要完成的事情?
您可以通过boosting
查询和custom_score
查询的组合来实现此custom_score
而不是提高年份我们根据年份改变分数,因为:
(_score + 2013) > (_score + 1999)
较新的结果将浮出水面。
通过使用提升查询,我们可以有效地降低遗漏奖励字段的结果。
请参阅: 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(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) ) ) );