# Elasticsaerch Query Exact match

**URL:** <https://discuss.elastic.co/t/elasticsaerch-query-exact-match/353696>\
**Category:** Elasticsearch\
**Created:** [February 20, 2024, 1:34pm UTC](https://discuss.elastic.co/t/elasticsaerch-query-exact-match/353696 "2024-02-20T13:34:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohan\_T](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Mohan\_T](https://discuss.elastic.co/u/Mohan_T)\
**Post date:** [February 20, 2024, 1:34pm UTC](https://discuss.elastic.co/t/elasticsaerch-query-exact-match/353696/1 "2024-02-20T13:34:16Z")

</div>

to find the exact match

**mappings**

```auto
"keywords": {
    "type": "object",
    "enabled": True,
    "properties": {
        "keyword_values": {
            "type": "text",
            "analyzer": "synonym_stemmer_bad_words_analyzer",
            "fields": {
                "keyword": {
                    "type": "keyword"
                }
            }
        }
    }
}

```

Doc 1

```auto
"keywords": [
    {
        "keyword_values": "marbles"
    },
    {
        "keyword_values": "flooring"
    }
}

```

Doc 2

```auto
"keywords": [
    {
        "keyword_values": "marble"
    },
    {
        "keyword_values": "floor"
    }
}

```

Doc 3

```auto
"keywords": [
    {
        "keyword_values": "marble floor"
    },
    {
        "keyword_values": "floor marble"
    }
}

```

If am searching for marble with keywords

using the below query

**Query:**  **"keywords.keyword\_values"**

```auto
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "keywords.keyword_values": {
                            "query": "marble",
                            "operator": "AND",
                            "prefix_length": 0,
                            "max_expansions": 50,
                            "fuzzy_transpositions": "true",
                            "lenient": "false",
                            "zero_terms_query": "NONE",
                            "auto_generate_synonyms_phrase_query": "true",
                            "boost": 7
                        }
                    }
                }
            ],
            "minimum_should_match": "1"
        }
    }
}

```

I will be getting all three doc results, but I need to fetch only Doc 1 & Doc 2

so I have used the query with the **keyword**

**Query:****"keywords.keyword\_values.keyword"**

```auto
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "keywords.keyword_values.keyword": {
                            "query": "marble",
                            "operator": "AND",
                            "prefix_length": 0,
                            "max_expansions": 50,
                            "fuzzy_transpositions": "true",
                            "lenient": "false",
                            "zero_terms_query": "NONE",
                            "auto_generate_synonyms_phrase_query": "true",
                            "boost": 7
                        }
                    }
                }
            ],
            "minimum_should_match": "1"
        }
    }
}

```

in this am getting only **Doc 2** , here i **need to fetch Doc 1 & Doc 2**

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [February 20, 2024, 4:40pm UTC](https://discuss.elastic.co/t/elasticsaerch-query-exact-match/353696/2 "2024-02-20T16:40:45Z")

</div>

Hi @Mohan_T

Please provide the analyzer below:

> [@Mohan\_T](#):
>
> `synonym_stemmer_bad_words_analyzer`

---

<div class="post-metadata">

**Author:** ![Mohan\_T](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Mohan\_T](https://discuss.elastic.co/u/Mohan_T)\
**Post date:** [February 20, 2024, 5:24pm UTC](https://discuss.elastic.co/t/elasticsaerch-query-exact-match/353696/3 "2024-02-20T17:24:15Z")

</div>

```auto
{
    "tokens": [
        {
            "token": "marbl",
            "start_offset": 0,
            "end_offset": 6,
            "type": "<ALPHANUM>",
            "position": 0
        }
    ]
}

```

---

<div class="post-metadata">

**Author:** ![Mohan\_T](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Mohan\_T](https://discuss.elastic.co/u/Mohan_T)\
**Post date:** [February 21, 2024, 7:02am UTC](https://discuss.elastic.co/t/elasticsaerch-query-exact-match/353696/4 "2024-02-21T07:02:01Z")

</div>

@RabBit_BR I have updated the analyzer, and the above one am getting the results when am using analyzer

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [February 21, 2024, 11:52am UTC](https://discuss.elastic.co/t/elasticsaerch-query-exact-match/353696/5 "2024-02-21T11:52:55Z")

</div>

I don't know if you can create a new field and new analyzer but in the example below it was possible to retrieve the docs using the keyword tokenizer.

```auto
PUT idx_test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "keyword",
          "filter": [
            "my_filter"
          ]
        }
      },
      "filter": {
        "my_filter": {
          "type": "stemmer"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "keywords": {
        "properties": {
          "keyword_values": {
            "type": "text",
            "analyzer": "my_analyzer",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
  }
}

```

Query

```auto
GET idx_test/_search
{
  "query": {
    "match": {
      "keywords.keyword_values": "marbles"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Mohan\_T](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Mohan\_T](https://discuss.elastic.co/u/Mohan_T)\
**Post date:** [February 21, 2024, 12:52pm UTC](https://discuss.elastic.co/t/elasticsaerch-query-exact-match/353696/6 "2024-02-21T12:52:23Z")

</div>

@RabBit_BR Thank you, it's working as expected, but I have one more use case, for example, Docs 4 & 5 has the below data.

Doc 4

```auto
"keywords": [
    {
        "keyword_values": "marble floor"
    }
}

```

Doc 5

```auto
"keywords": [
    {
        "keyword_values": "floor marble"
    }
}

```

**Search Query**

```auto
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "keywords.keyword_values": {
                            "query": "marble floor",
                            "operator": "AND",
                            "prefix_length": 0,
                            "max_expansions": 50,
                            "fuzzy_transpositions": "true",
                            "lenient": "false",
                            "zero_terms_query": "NONE",
                            "auto_generate_synonyms_phrase_query": "true",
                            "boost": 7
                        }
                    }
                }
            ],
            "minimum_should_match": "1"
        }
    }
}

```

I want to get Docs 4 & 5 but am getting only Doc 4 using the above query

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [February 21, 2024, 2:38pm UTC](https://discuss.elastic.co/t/elasticsaerch-query-exact-match/353696/7 "2024-02-21T14:38:25Z")

</div>

I saw and other post similiar question and the orientation was using [span fist query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-first-query.html), maybe work for you.

---

<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:** [March 23, 2024, 11:09am UTC](https://discuss.elastic.co/t/elasticsaerch-query-exact-match/353696/9 "2024-03-23T11:09:04Z")

</div>

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