使用NEST Field Boosting进行弹性搜索 [英] Elastic Search using NEST Field Boosting

查看:103
本文介绍了使用NEST Field Boosting进行弹性搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NEST强类型客户端在C#中使用弹性搜索。
我有一个包含条目的索引:

  [ElasticType(Name =Entry,IdProperty =Id) ] 
public class Entry
{
public string Id {get;组; }
public string标题{get;组; }
public string描述{get;组; }
public string Award {get;组; }
public int Year {get;组; }
}

哪里年份是入学年份,例如2012年,奖是获奖的类型获得,可以为null。



然后,我想使用boosting搜索这些条目,以获取不同的属性。在以下代码中,我希望结果在标题上的匹配排名高于描述符合的结果。

 私有IQueryResponse<条目> 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))));
}

我现在被要求提升获奖者,并且还会提升新的条目(即按年度)。



我该怎么做?是否需要作为索引服务的一部分或作为搜索的一部分?

解决方案

您可以实现这通过提升查询和 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< 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
。过滤(nfq => nfq
.Query(qq => qq.MatchAll() )
.Filter(f => f.Missing(p => p.Award))


.NegativeBoost(0.2)


);


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; }
}

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))));
}

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

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)
        )
    )
);

这篇关于使用NEST Field Boosting进行弹性搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆