【问题标题】:Elastic Search using NEST Field Boosting使用 NEST 字段提升的弹性搜索
【发布时间】:2012-12-30 18:50:21
【问题描述】:

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

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

其中 Year 是参赛作品的年份,例如 2012 年,Award 是参赛作品获得的奖项类型,可以为空。

然后我想使用提升来搜索这些条目以获得不同的属性。在下面的代码中,我希望与标题匹配的结果排名高于与描述匹配的结果。

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

我现在被要求提升获奖者的成绩,并提升新的参赛作品(即按年度)。

我该怎么做?它是作为索引服务的一部分还是作为搜索的一部分需要完成的?

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

    您可以通过boosting 查询和custom_score 查询的组合来实现此目的

    我们根据年份更改分数,而不是提高年份,因为:

    (_score + 2013) > (_score + 1999)
    

    较新的结果将浮动到顶部。

    通过使用提升查询,我们可以有效地将缺少奖励字段的结果降级。

    见: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-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)
            )
        )
    );
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多