在NEST中不喜欢查询

时间:2017-05-16 00:25:17

标签: c# elasticsearch nest

我编写了一个NEST查询,该查询匹配字符串中包含单词的文档(下面的“queryString”)。我想过滤掉包含“not”一词的文档。我已经尝试过布尔查询,但它没有过滤掉文档。我在这里做错了什么?

var searchResponse2 = _EsClientDAL.Current.Search<DTO.riSmall>(s => s
                        .Size(50)
                        .Query(q => q
                            .Bool(b => b
                                .Must(mu => mu
                                    .Match(m => m
                                        .Field(f => f.SceneText)
                                        .Query(queryString)
                                    )
                                  ,
                                    mu => !mu
                                    .Term(p => p.SceneText, "not")
                                    )
                                )
                             )
                        .Highlight(h => h
                            .PreTags("|")
                            .PostTags("|")
                            .Fields(
                                fs => fs
                                    .Field(p => p.SceneText)
                                    .Type("plain")
                                    .ForceSource()
                                    .FragmentSize(150)
                                    .NumberOfFragments(3)
                                    .NoMatchSize(150)
                                )
                            )
                        );

2 个答案:

答案 0 :(得分:2)

您可以使用as.Date

尝试以下查询
must_not

希望这有助于你

答案 1 :(得分:1)

除了Richa's answer之外,您还可以使用overloaded operators来编写与Richa更简洁的提议相同的查询

var searchResponse = client.Search<MyDocument>(s => s
    .Size(50)
    .Query(q => q
        .Match(m => m
            .Field(f => f.SceneText)
            .Query(queryString)
        ) && !q
        .Term(p => p.SceneText, "not") 
    )
    .Highlight(h => h
        .PreTags("|")
        .PostTags("|")
        .Fields(
            fs => fs
                .Field(p => p.SceneText)
                .Type("plain")
                .ForceSource()
                .FragmentSize(150)
                .NumberOfFragments(3)
                .NoMatchSize(150)
            )
        )
    );