witchlightning
witchlightning

Reputation: 271

NOT LIKE query in NEST

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

Upvotes: 1

Views: 1863

Answers (2)

Russ Cam
Russ Cam

Reputation: 125488

In addition to Richa's answer, you can also use the overloaded operators to write the same query as Richa proposes more succinctly

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

Upvotes: 1

Richa
Richa

Reputation: 7649

you can try with following query using must_not

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

Hope this helps you

Upvotes: 2

Related Questions