Exact Match

Hey all

How could I in elastic search do my search in such a way that only if both fields given are a match will a result be given? Like an AND statement.

Here is what I am trying to do (minified code):

var searchResult = searchClient.Search<Log>(s => 
 .Index(uriIndex)
 .Query(q =>
      q.ConstantScore(cs => 
           .Filter(f => f
                 .Term(t => t.Status, _status)
           )
      ) &&
      q.MultiMatch(mm => mm
            .Query(_phrase)
                  .Fields(fs => fs
                       .Field(f => f.Data)
                  )
             ) 
      )
 );

Regards

There are several ways to implement an "AND" statement.
You're using the multi_match query. If you want only hits where the search term appears in 2 fields, you can use the "minimum_should_match" parameter (set to 2).

You could also build a bool-query with 2 must clauses.

Thanks for the quick response.

The multi_match I am using as I want to search multiple fields. I just only included the one so as not to make the question to long.

Regarding the bool-query, is something like this on the right track?

q.Bool(b => b
       .Must(m => m
           .Term(t => t
                  .Field(f => f.Status)
                  .Value(_status)
            )
       )
       .Must(m => m
             .Term(t => t
                      .Field(f => f.data)
                       .Value(_term)
              )
       )

)

I will take a look into the minimum_should_match.

Yes that looks good :slight_smile:

A "multi_match" query over 2 fields with the "minimum_should_match" parameter set to 2 in your case is equivalent to a bool-query with 2 must-clauses.

Sweet :slight_smile: Thank you

Does the must-clause run concurrently? I am getting 3 results for the _status check, _term check and then _status && _term check, not just the _status && _term check. Or so it seems.

What I ended up doing was to have a single must-clause and in that I did my checks to make the following (X AND (Y OR X)) statement.

In ElasticSearch it looks like the following:

                .Bool(b => b
                    .Must(m =>
                        m.Match(ma => ma
                            .Query(_status)
                            .Field(f => f.Status)
                        ) &&
                        m.Bool(b2 => b2
                            .Should(sh => sh
                                .MultiMatch(mm => mm
                                    .Query(_phrase)
                                    .Fields(fs => fs
                                         .Field(f => f.Data)
                                       // Rest of my fields
                                    )
                                ) ||
                                q.Terms(ts => ts.Field(f => f.Data).Terms(_terms)) ||
                                // The rest of my fields
                                 )
                             )
                         )
                   )

This may not be the best way to do it, but it is what I have working at the moment. As I work with ElasticSearch more and the scope of the project changes, this will improve in the future.

Regards

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