# How do I do Case Sensitive Match Query in Elastic Search?

**URL:** <https://discuss.elastic.co/t/how-do-i-do-case-sensitive-match-query-in-elastic-search/309348>\
**Category:** Elasticsearch\
**Created:** [July 11, 2022, 5:44pm UTC](https://discuss.elastic.co/t/how-do-i-do-case-sensitive-match-query-in-elastic-search/309348 "2022-07-11T17:44:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DataStorageMuse](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@DataStorageMuse](https://discuss.elastic.co/u/DataStorageMuse)\
**Post date:** [July 11, 2022, 5:44pm UTC](https://discuss.elastic.co/t/how-do-i-do-case-sensitive-match-query-in-elastic-search/309348/1 "2022-07-11T17:44:52Z")

</div>

I'm trying to do a match query that is case sensitive such that it only returns words where the html field contains the string. Here is the query I have so far:

```auto
POST data/_search
{
  "track_total_hits": true, 
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "html": "test_html_query"
          }
        }
      ]
    }
}
  
}

```

I want to only return documents that strictly have the "test\_html\_query" string as a substring, not documents that are kind of like this string or have some relevance. When I do this query, I get more results than expected. How do I make this more case sensitive?

---

<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:** [July 11, 2022, 7:24pm UTC](https://discuss.elastic.co/t/how-do-i-do-case-sensitive-match-query-in-elastic-search/309348/2 "2022-07-11T19:24:29Z")

</div>

Hi @DataStorageMuse  
Could you give more information about your mapping? If the "html" field has any analyzer and what is its type?

---

<div class="post-metadata">

**Author:** ![DataStorageMuse](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@DataStorageMuse](https://discuss.elastic.co/u/DataStorageMuse)\
**Post date:** [July 11, 2022, 7:42pm UTC](https://discuss.elastic.co/t/how-do-i-do-case-sensitive-match-query-in-elastic-search/309348/3 "2022-07-11T19:42:11Z")

</div>

What do you mean by mapping? I didn't put any analyzer on the html field.

---

<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:** [July 11, 2022, 10:52pm UTC](https://discuss.elastic.co/t/how-do-i-do-case-sensitive-match-query-in-elastic-search/309348/4 "2022-07-11T22:52:11Z")

</div>

Since there is no parser in the field field, one option is to create a new field where the parser is of type whitespace just to separate the terms.  
The example below represents what I said.

```auto

PUT idx_test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "raw_analyzer": {
          "type": "whitespace"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "html": {
        "type": "text",
        "fields": {
          "raw": {
            "type":"text",
            "analyzer": "raw_analyzer"
          }
        }
      }
    }
  }
}

POST idx_test/_bulk
{"index":{}}
{"html": "test html query test_html_query"}
{"index":{}}
{"html": "test html query Test_html_query"}

GET idx_test/_analyze
{
  "field": "html",  
  "text": ["test html query Test_html_query"]
}

GET idx_test/_search
{
  "query": {
    "match": {
      "html.raw": {
        "query": "test_html_query"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [July 12, 2022, 5:24am UTC](https://discuss.elastic.co/t/how-do-i-do-case-sensitive-match-query-in-elastic-search/309348/5 "2022-07-12T05:24:41Z")

</div>

> [@DataStorageMuse](#):
>
> What do you mean by mapping? I didn't put any analyzer on the html field.

The [Mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html) is the Schema for the index i.e. the data types and other specifications about your data, it may be worth taking a look at the docs as it is a key concept.

[Text Analysis](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html) is also a key concept in this case above @RabBit_BR showed how to use a simple `whitespace` analyzer to tokenize your data and then your search will be case sensitive but you will not get other features like stemming etc.

---

<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:** [July 12, 2022, 5:53am UTC](https://discuss.elastic.co/t/how-do-i-do-case-sensitive-match-query-in-elastic-search/309348/6 "2022-07-12T05:53:47Z")

</div>

> [@DataStorageMuse](#):
>
> What do you mean by mapping? I didn't put any analyzer on the html field.

It is worth noting that all fields have a mapping, which defines how they are indexed. If you do not specify any mapping through an index template or when the index is created the field will be mapped using [dynamic mapping rules](https://www.elastic.co/guide/en/elasticsearch/reference/8.3/dynamic-mapping.html).

---

<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:** [August 9, 2022, 5:54am UTC](https://discuss.elastic.co/t/how-do-i-do-case-sensitive-match-query-in-elastic-search/309348/7 "2022-08-09T05:54:46Z")

</div>

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