# How can i use elasticsearch query for exact match?

**URL:** <https://discuss.elastic.co/t/how-can-i-use-elasticsearch-query-for-exact-match/140799>\
**Category:** Elasticsearch\
**Created:** [July 19, 2018, 7:36pm UTC](https://discuss.elastic.co/t/how-can-i-use-elasticsearch-query-for-exact-match/140799 "2018-07-19T19:36:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shrimp-Fried-Rice](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shrimp-fried-rice/32/33360_2.png) [@Shrimp-Fried-Rice](https://discuss.elastic.co/u/Shrimp-Fried-Rice)\
**Post date:** [July 19, 2018, 7:36pm UTC](https://discuss.elastic.co/t/how-can-i-use-elasticsearch-query-for-exact-match/140799/1 "2018-07-19T19:36:17Z")

</div>

i have web app, where iam trying to move more and more queries from mysql to elasticsearch, even the ones which are not Full text search, to lower the system load.

there are one type of queries which i need to match exactly with uploader username.

lets say, usernames are

tom  
tom.bom  
tom\_bom  
tom-bom

when i search for tom

i dont want other username matches to show up.

how can i do that ?

right now i am doing it like this way

```
Array
(
    [index] => datasets
    [type] => content
    [body] => Array
        (
            [sort] => Array
                (
                    [upload_date] => Array
                        (
                            [order] => desc
                        )
                )
            [query] => Array
                (
                    [bool] => Array
                        (
                            [must] => Array
                                (
                                    [match] => Array
                                        (
                                            [uploader] => tom
                                        )
                                )
                        )
                )
            [from] => 0
            [size] => 30
        )
)

```

i just want to make this future proof, i dont have those users right now.

Thanks for your time,

here is my current mapping

```
{
	"mappings": {
    "content": { 
      "properties": { 
        "title":{ 
			"type": "text",
			"fields": {
				"raw": { 
					"type": "keyword"
				}
			}
		},
        "tags": { "type": "text" },
        "category": { "type": "keyword" },
        "sub_category":	{ "type": "keyword" },
        "size": { "type": "long" },
        "uploaders": { "type": "integer" },
        "downloaders": { "type": "integer" },
        "upload_date":	{
			"type": "date",
			"format": "yyyy-MM-dd HH:mm:ss"
        },
		"uploader":{
					"type": "text",
					"fields":	{
								"raw": { 
									"type": "keyword"
								}
					}
		}
      }
	}
  }
}
```

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [July 20, 2018, 2:13am UTC](https://discuss.elastic.co/t/how-can-i-use-elasticsearch-query-for-exact-match/140799/2 "2018-07-20T02:13:45Z")

</div>

Hi,

What about using term, according to the documentation:

> The term query finds documents that contain the exact term specified in the inverted index. For instance:

> **[Term Query | Elasticsearch Guide \[6.3\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/query-dsl-term-query.html)**

Just replace match by term and according to your mapping add ".raw" to your field

```
Array
(
    [index] => datasets
    [type] => content
    [body] => Array
        (
            [sort] => Array
                (
                    [upload_date] => Array
                        (
                            [order] => desc
                        )
                )
            [query] => Array
                (
                    [bool] => Array
                        (
                            [must] => Array
                                (
                                    [term] => Array
                                        (
                                            [uploader.raw] => tom
                                        )
                                )
                        )
                )
            [from] => 0
            [size] => 30
        )
)

```

---

<div class="post-metadata">

**Author:** ![Shrimp-Fried-Rice](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shrimp-fried-rice/32/33360_2.png) [@Shrimp-Fried-Rice](https://discuss.elastic.co/u/Shrimp-Fried-Rice)\
**Post date:** [July 20, 2018, 3:12am UTC](https://discuss.elastic.co/t/how-can-i-use-elasticsearch-query-for-exact-match/140799/3 "2018-07-20T03:12:26Z")

</div>

Thank you, looks like i need to complete the documentation.

---

<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 17, 2018, 3:12am UTC](https://discuss.elastic.co/t/how-can-i-use-elasticsearch-query-for-exact-match/140799/4 "2018-08-17T03:12:30Z")

</div>

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