# Exact Match

**URL:** <https://discuss.elastic.co/t/exact-match/155020>\
**Category:** Elasticsearch\
**Created:** [November 1, 2018, 12:52pm UTC](https://discuss.elastic.co/t/exact-match/155020 "2018-11-01T12:52:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Euan](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@Euan](https://discuss.elastic.co/u/Euan)\
**Post date:** [November 1, 2018, 12:52pm UTC](https://discuss.elastic.co/t/exact-match/155020/1 "2018-11-01T12:52:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SaskiaVola](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@SaskiaVola](https://discuss.elastic.co/u/SaskiaVola)\
**Post date:** [November 1, 2018, 1:18pm UTC](https://discuss.elastic.co/t/exact-match/155020/2 "2018-11-01T13:18:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Euan](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@Euan](https://discuss.elastic.co/u/Euan)\
**Post date:** [November 1, 2018, 1:25pm UTC](https://discuss.elastic.co/t/exact-match/155020/3 "2018-11-01T13:25:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SaskiaVola](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@SaskiaVola](https://discuss.elastic.co/u/SaskiaVola)\
**Post date:** [November 1, 2018, 1:40pm UTC](https://discuss.elastic.co/t/exact-match/155020/4 "2018-11-01T13:40:29Z")

</div>

Yes that looks good 🙂

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.

---

<div class="post-metadata">

**Author:** ![Euan](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@Euan](https://discuss.elastic.co/u/Euan)\
**Post date:** [November 1, 2018, 1:46pm UTC](https://discuss.elastic.co/t/exact-match/155020/5 "2018-11-01T13:46:03Z")

</div>

Sweet 🙂 Thank you

---

<div class="post-metadata">

**Author:** ![Euan](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@Euan](https://discuss.elastic.co/u/Euan)\
**Post date:** [November 2, 2018, 8:44am UTC](https://discuss.elastic.co/t/exact-match/155020/6 "2018-11-02T08:44:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Euan](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@Euan](https://discuss.elastic.co/u/Euan)\
**Post date:** [November 2, 2018, 10:39am UTC](https://discuss.elastic.co/t/exact-match/155020/7 "2018-11-02T10:39:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 30, 2018, 10:39am UTC](https://discuss.elastic.co/t/exact-match/155020/8 "2018-11-30T10:39:34Z")

</div>

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