【问题标题】:Elasticsearch/Nest - using MatchPhrase with OnFieldsWithBoostElasticsearch/Nest - 将 MatchPhrase 与 OnFieldsWithBoost 一起使用
【发布时间】:2015-03-26 00:17:42
【问题描述】:

在我今天的代码中,我正在做这样的搜索:

.Query(q => q.QueryString(qs => qs.Query(searchQuery).OnFieldsWithBoost(f => f.Add(b => b.MetaTitle, 5).Add(b => b.RawText, 1))))

我的问题是,如果我搜索类似的短语,这会给我一个非常广泛的搜索。 “太阳普照”。我尝试在 RawText 上使用 MatchPhrase,而不是 QueryString,这有点用。

问题是我仍然想在 MetaTitle 和 RawText 中进行搜索,并使用我现在使用的增强功能。

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

    我不知道 Nest,但你想要做的是使用 multi-match query of phrase typefields boost

    在 g**gle 上的快速搜索给了我一个类似这样的提升部分语法:

    .Query(q => q
        .MultiMatch(m => m
            .OnFieldsWithBoost(b => b
                .Add(o => o.MyField, 2.0)
                .Add(o => o.AnotherField, 3.0)
            )
            .Type(TextQueryType.Phrase)
            .Query("my query text")
        )
    )
    

    API 必须有某种type 参数才能将phrase 类型添加到此。

    编辑:快速查看sources后,我发现了一个Type方法,添加在上面。

    【讨论】:

    • 嘿汤姆。谢谢您的意见。我尝试了以下方法。 .Query(q => q.MultiMatch(m => m.OnFieldsWithBoost(f => f.Add(b => b.MetaTitle, 5).Add(b => b.RawText, 1)).Query(searchQuery )) ) 它看起来也很宽。我得到 268 个结果。使用 MatchPhrase 我只得到 2 个结果。
    【解决方案2】:

    因为我没有找到不定义给定类型的多重匹配查询搜索示例。我花了几天时间试图解决这个问题,我想出了解决方案。

    我在 C# 中使用 NEST 库。在这里,我保留与上面相同的方法,但使用泛型,并传递带有字段和提升的字典,因为您将无法使用流利的表达方式编写智能。我还添加了 skip 和 take(或 from 和 size)方法、搜索类型和要执行搜索的索引数组。

       var dic = new FluentDictionary<string, double?>();
        dic.Add("Field1", 1.0);
        dic.Add("Field2", 1.0);
    
       var response = Client.Search<T>(s => s
                                        .Skip(0)
                                        .Take(5)
                                        .Indices(indexes)
                                        .Query(q => q
                                            .MultiMatch(m => m
                                                .OnFieldsWithBoost(b => 
                                                {
                                                    foreach (var entry in dic)
                                                        b.Add(entry.Key, entry.Value);     
                                                })
                                                .Type(TextQueryType.Phrase)
                                                .Query("your query text")
                                                )
                                             )
                                        );
    

    【讨论】:

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