使用NEST现场推进弹性搜索 [英] Elastic Search using NEST Field Boosting
问题描述
我使用弹性搜索在C#中使用强类型的客户端窝。
我有一个包含条目索引:
I am using Elastic Search in C# using the NEST strongly typed client. 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; }
}
其中年是进入的年份,如2012年,和奖是奖项的类型赢了,它可以为null。
Where Year is the year of the entry, eg 2012, and Award is the type of Award the Entry won, which can be null.
然后我要搜索使用提升不同属性的这些条目。在下面的代码,我想结果进行排列的标题较高的那场比赛,比匹配的说明这一点。
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.
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))));
}
我现在已经被要求,以提高那些已经赢得大奖的结果,并且也助推新条目(由年度IE)。
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?
推荐答案
您可以实现这通过提高
查询和 custom_score
查询
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.
看到:$。 b $ b http://www.elasticsearch.org/guide/reference/query-dsl /boosting-query.html
http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html
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)
)
)
);
这篇关于使用NEST现场推进弹性搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!