NOT LIKE Query in NEST


(James) #1

I've written a NEST query that matches documents that have words in a string ("queryString" below). I want to filter out documents that contain the word "not". I've tried the boolean query but it doesn't filter out the documents. What am I doing wrong here?

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

(Russ Cam) #2

The most succinct way to write your query would be using the overloaded operators for queries

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

This is synonymous with the longer form

var searchResponse = client.Search<MyDocument>(s => s
    .Size(50)
    .Query(q => q
        .Bool(b => b
            .Must(mu => mu
                .Match(m => m
                    .Field(f => f.SceneText)
                    .Query(queryString)
                )
            )
            .MustNot(mn => mn
                .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)
            )
        )
    );

(system) #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.