简体   繁体   English

Elasticsearch / Nest - 使用MatchPhrase和OnFieldsWithBoost

[英]Elasticsearch/Nest - using MatchPhrase with OnFieldsWithBoost

In my code today I am doing a search like this: 在我今天的代码中,我正在进行如下搜索:

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

My problem is this gives me a very wide search if I search on a phrase like. 我的问题是,如果我搜索一个类似的短语,这给了我一个非常广泛的搜索。 "The sun is shining". “艳阳高照”。 I have tried using MatchPhrase on RawText, instead of QueryString, and that kinda works. 我尝试在RawText上使用MatchPhrase而不是QueryString,这种方法有用。

The problem is I still want to search in both MetaTitle and RawText and with the boosting I am using now. 问题是我仍然想要在MetaTitle和RawText中搜索并使用我正在使用的提升。

I don't know Nest, but what you want to do is to use a multi-match query of phrase type , with fields boost . 我不知道Nest,但你想要做的是使用短语类型多匹配查询 ,并增加字段

A quick search on g**gle gave me a syntax like this for the boost part: 对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")
    )
)

The API must have some sort of type parameter to add the phrase type to this. API必须具有某种type参数才能将phrase类型添加到此类型。

Edit : after a quick look in the sources , I found a Type method, added above. 编辑 :在快速查看源代码后 ,我找到了一个Type方法,上面添加了。

Since I have not found an example of a multimatch query search without defining a given type. 由于我没有找到多匹配查询搜索的示例而没有定义给定类型。 I spent a few days trying to work it out and i came up with the solution. 我花了几天时间试图解决这个问题,然后我想出了解决方案。

I´m using NEST Library in C#. 我在C#中使用NEST库。 Here I leave the same method as above but using Generics, and passing a dictionary with the fields and boosts, since you will not have the intellisence writing with the fluent expression. 在这里,我使用与上面相同的方法,但使用泛型,并传递带字段和增强的字典,因为您不会使用流畅的表达式进行智能编写。 I also added the skip and take (or from and size) methods, the Type of Search and an array of the indexes you want to perform the search. 我还添加了skip和take(或from和size)方法,Search Type和要执行搜索的索引数组。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM