How to pass 2 values or More to Search Using **Nest** in .Net


(Saineshwar Bageri) #1

How to pass 2 values or More to Search Using Nest in .Net

I have Tried below Code Snippet. But it wont show Accurate Result ,Please Guide Me in this.

string query1 = "fullname=" + applicantname + "AND district=" + "AKOLA";

var response = ConnectionToEs.EsClient().Search<Bankpayoutsummary>(s => s
    .From(0)
    .Size(50)
    .Index("bankpayoutsummary")
    .Type("data")
    .DefaultOperator(DefaultOperator.And)
    .MatchAll()
        .Query(q => q
            .QueryString(qs => qs
                .Query(query1)
            )
        )
);

(David Pilato) #2

Can't tell what NEST code would look like but you need to use a bool query with 2 filter clauses (or must clauses if relevancy matters).


(Saineshwar Bageri) #3

Query to Return Value passing 2 Values in Search.

     string applicantname = "Saineshwar";
     string district = "Mumbai City";
     var searchResponse = ConnectionToEs.EsClient().Search<Bankpayoutsummary>(s => s
            .Index("bankpayoutsummary")
            .Type("data")
            .Size(50)
            .Query(q => q
                            .Match(m => m
                                .Field(f => f.Fullname)
                                .Query(applicantname)
                            ) && q
                            .Match(m => m
                                .Field(f => f.District)
                                .Query(district)
                            )
            )
        );

(David Pilato) #4

Does it automatically generate a Bool Query behind the scene?
I'm surprised by this code and I don't think it's correct TBH.

You can't tell here if it goes to a filter or must clause.


(Saineshwar Bageri) #5

Valid Example and its Working thanks to @dadoonet sir for giving some hits on it.

string applicantname = "Saineshwar";
string district = "Mumbai City";  

var searchResponse = ConnectionToEs.EsClient().Search<Bankpayoutsummary>(s => s
              .Index("bankpayoutsummary")
              .Type("data")
              .Size(50)

              .Query(q => q.Bool(b => b
                  .Must(mu => mu
                             .Match(m => m
                                  .Field(f => f.Fullname)
                                  .Query(applicantname)
                              ) && q
                              .Match(m => m
                                  .Field(f => f.District)
                                  .Query(district)
                              )
              )
              ))
          );

(David Pilato) #6

Note that if the District is coming from a list box or something like that, you should use a keyword type instead of text and use then a term query and move this term query in a filter clause instead of must clause. That will be more efficient (speed wise).

If you do full text search on district (like it's something entered manually by the user), then keep your solution, it's good as is.


(Saineshwar Bageri) #7

Any reference @dadoonet sir


(David Pilato) #8

Doc here: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html


(system) #9

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