# How to search for exact matches in either one of two fields?

**URL:** <https://discuss.elastic.co/t/how-to-search-for-exact-matches-in-either-one-of-two-fields/137338>\
**Category:** Elasticsearch\
**Created:** [June 26, 2018, 3:29am UTC](https://discuss.elastic.co/t/how-to-search-for-exact-matches-in-either-one-of-two-fields/137338 "2018-06-26T03:29:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajeevrmenon97](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rajeevrmenon97/32/32681_2.png) [@rajeevrmenon97](https://discuss.elastic.co/u/rajeevrmenon97)\
**Post date:** [June 26, 2018, 3:29am UTC](https://discuss.elastic.co/t/how-to-search-for-exact-matches-in-either-one-of-two-fields/137338/1 "2018-06-26T03:29:17Z")

</div>

I have two fields `field_1` and `field_2`. I have a to find exact match for my query in either `field_1` or `field_2`. `multi_match` is not giving me exact matches. And `constant_score` does not support `multi_match`.

The elasticsearch version I'm using:

`Version: 6.3.0, Build: default/rpm/424e937/2018-06-11T23:38:03.357887Z, JVM: 1.8.0_171`

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 26, 2018, 5:50am UTC](https://discuss.elastic.co/t/how-to-search-for-exact-matches-in-either-one-of-two-fields/137338/2 "2018-06-26T05:50:04Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![rajeevrmenon97](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rajeevrmenon97/32/32681_2.png) [@rajeevrmenon97](https://discuss.elastic.co/u/rajeevrmenon97)\
**Post date:** [June 26, 2018, 5:56am UTC](https://discuss.elastic.co/t/how-to-search-for-exact-matches-in-either-one-of-two-fields/137338/3 "2018-06-26T05:56:04Z")

</div>

I'm new to elasticsearch. What is a recreation script?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 26, 2018, 6:39am UTC](https://discuss.elastic.co/t/how-to-search-for-exact-matches-in-either-one-of-two-fields/137338/4 "2018-06-26T06:39:18Z")

</div>

I guess you did not click on the link in my response. Otherwise you'd have seen a typical script that can be used to reproduce a problem:

```auto
DELETE index
PUT index/_doc/1
{
  "foo": "bar"
}
GET index/_search
{
  "query": {
    "match": {
      "foo": "bar"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [June 26, 2018, 6:55am UTC](https://discuss.elastic.co/t/how-to-search-for-exact-matches-in-either-one-of-two-fields/137338/5 "2018-06-26T06:55:07Z")

</div>

What is the mapping of your fields?

---

<div class="post-metadata">

**Author:** ![rajeevrmenon97](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rajeevrmenon97/32/32681_2.png) [@rajeevrmenon97](https://discuss.elastic.co/u/rajeevrmenon97)\
**Post date:** [June 26, 2018, 7:14am UTC](https://discuss.elastic.co/t/how-to-search-for-exact-matches-in-either-one-of-two-fields/137338/6 "2018-06-26T07:14:55Z")

</div>

This is the mapping of my fields. Say my search term is "foo", I want results in which either "field\_1" is "foo" or "field\_2" is "foo". Also I want exact matches

```
{
  "test-index": {
    "mappings": {
      "names": {
        "properties": {
          "field_1": {
            "type": "keyword"
          },
          "field_2": {
            "type": "keyword"
          },
          "title": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [June 26, 2018, 7:19am UTC](https://discuss.elastic.co/t/how-to-search-for-exact-matches-in-either-one-of-two-fields/137338/7 "2018-06-26T07:19:35Z")

</div>

Have you tried using term queries for the two fields within a bool clause?

---

<div class="post-metadata">

**Author:** ![rajeevrmenon97](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rajeevrmenon97/32/32681_2.png) [@rajeevrmenon97](https://discuss.elastic.co/u/rajeevrmenon97)\
**Post date:** [June 27, 2018, 4:14am UTC](https://discuss.elastic.co/t/how-to-search-for-exact-matches-in-either-one-of-two-fields/137338/8 "2018-06-27T04:14:48Z")

</div>

Yes. It's not working. I used

```
{
   "query" : {
      "constant_score" : { 
         "filter" : {
            "bool" : {
              "should" : [
                 { "term" : {"field_1" : "<search query>"}},
                 { "term" : {"field_2" : "<search query>"}}
              ]
           }
         }
      }
   }
}
```

---

<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:** [July 25, 2018, 4:14am UTC](https://discuss.elastic.co/t/how-to-search-for-exact-matches-in-either-one-of-two-fields/137338/9 "2018-07-25T04:14:50Z")

</div>

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