[英]Elastic Search using NEST Field Boosting
I am using Elastic Search in C# using the NEST strongly typed client. 我正在使用NEST强类型客户端在C#中使用弹性搜索。 I have an index containing Entries:
我有一个包含条目的索引:
[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; }
}
Where Year is the year of the entry, eg 2012, and Award is the type of Award the Entry won, which can be null. 如果年份是参赛年份,例如2012年,奖励是参赛作品获奖的类型,可以为空。
I then want to search these Entries using boosting for different properties. 然后,我想使用针对不同属性的提升来搜索这些条目。 In the following code, I want results to be ranked higher that match on the Title, than those that match on the Description.
在下面的代码中,我希望结果在Title上的排名高于在Description上匹配的结果。
private IQueryResponse<Entry> GetMatchedEntries(string searchText)
{
return _elasticClient.Search<Entry>(
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))));
}
I have now been asked to Boost the results by those which have won Awards, and also Boost newer Entries (ie by the Year). 我现在被要求提高获奖者的成绩,并提升新的参赛作品(即年度)。
How do I do this? 我该怎么做呢? Is it something that needs to be done as part of the indexing service, or as part of the search?
它是否需要作为索引服务的一部分或作为搜索的一部分来完成?
You can achieve this through a combination of a boosting
query and custom_score
query 您可以通过
boosting
查询和custom_score
查询的组合来实现此custom_score
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 请参阅: 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)
)
)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.