# Query String Query exact word search

**URL:** <https://discuss.elastic.co/t/query-string-query-exact-word-search/211865>\
**Category:** Elasticsearch\
**Created:** [December 14, 2019, 8:41am UTC](https://discuss.elastic.co/t/query-string-query-exact-word-search/211865 "2019-12-14T08:41:28Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Juliano](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kevin_juliano/32/48845_2.png) [@Kevin\_Juliano](https://discuss.elastic.co/u/Kevin_Juliano)\
**Post date:** [December 14, 2019, 8:41am UTC](https://discuss.elastic.co/t/query-string-query-exact-word-search/211865/1 "2019-12-14T08:41:28Z")

</div>

Hi Guys! I want to return result that only has the exact word searched without appended characters like if I search "toy", it SHOULD NOT return "toys" or "toyed".

Right now, when I do some searching, it is returning results that doesn't have the word searched.

here's how I use it;

```
{

"size": 10,

"query": {

        "query_string" : {

                 "query" : "lawyer:(John Doe)",

                 "fuzziness":0

          }

 },

  "collapse" : {

       "field" : "id"

  }
}

```

Any help would be appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![Opster\_Community1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/opster_community1/32/59062_2.png) [@Opster\_Community1](https://discuss.elastic.co/u/Opster_Community1)\
**Post date:** [December 14, 2019, 9:06am UTC](https://discuss.elastic.co/t/query-string-query-exact-word-search/211865/2 "2019-12-14T09:06:37Z")

</div>

We can help you better if you can add the mapping of the index.

---

<div class="post-metadata">

**Author:** ![Kevin\_Juliano](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kevin_juliano/32/48845_2.png) [@Kevin\_Juliano](https://discuss.elastic.co/u/Kevin_Juliano)\
**Post date:** [December 14, 2019, 1:50pm UTC](https://discuss.elastic.co/t/query-string-query-exact-word-search/211865/3 "2019-12-14T13:50:27Z")

</div>

Hi @Opster_Community1, Thanks for your response,

I actually put:

```
 "query_string" : {

             "query" : "lawyer.atty_name:(John Doe)",

             "fuzziness":0

      } 

```

here's the example result set:

```
hits": [
{
    "_index": "my_index",
    "_type": "_doc",
    "_id": "Uz3iPm0BznItGBZve8Yp",
    "_score": 9.699111,
    "_source": {
        "applicant":"Jerry Smith",
        "@timestamp": "2019-09-17T11:01:00.857Z",
        "lawyer": [
            {
                "atty_name": "John Doe",
                "atty_country": "US",
                "atty_org": null
            }
        ],
        
        "examiner": [
            {
                "name": "Alex Smith",
                "role": "primary"
            }
        ],
        "id": 6692053,
        "reldoc": [
            {
                "related_doc": "06336679"
            },
            {
                "related_doc": "06692053"
            }
        ]
   }
]
```

---

<div class="post-metadata">

**Author:** ![Kevin\_Juliano](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kevin_juliano/32/48845_2.png) [@Kevin\_Juliano](https://discuss.elastic.co/u/Kevin_Juliano)\
**Post date:** [December 16, 2019, 2:42am UTC](https://discuss.elastic.co/t/query-string-query-exact-word-search/211865/4 "2019-12-16T02:42:03Z")

</div>

Anyone? ☹

---

<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:** [December 16, 2019, 2:56am UTC](https://discuss.elastic.co/t/query-string-query-exact-word-search/211865/5 "2019-12-16T02:56:24Z")

</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:** ![Kevin\_Juliano](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kevin_juliano/32/48845_2.png) [@Kevin\_Juliano](https://discuss.elastic.co/u/Kevin_Juliano)\
**Post date:** [December 16, 2019, 3:20am UTC](https://discuss.elastic.co/t/query-string-query-exact-word-search/211865/6 "2019-12-16T03:20:31Z")

</div>

> [@dadoonet](#):
>
> 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.

Hi @dadoonet, Thanks for your response, I just want to know if it is possible to search using `query_string` and have an exact word searched as a result. In this case I am searching for a name.

Here's what I did:

```
POST records/_search

  {
     "size": 1,
     "query": {
	    "query_string" : {
		"query" : "lawyer.atty_name:(John Doe)",
		"fuzziness":0
	  }
   },
      "collapse" : {
	    "field" : "id" 
      }
   }

```

Returned Result:

```
hits": [
{
   "_index": "records",
   "_type": "_doc",
   "_id": "Uz3iPm0BznItGBZve8Yp",
   "_score": 9.699111,
   "_source": {
      "applicant":"Jerry Smith",
      "id": 6692053
      "@timestamp": "2019-09-17T11:01:00.857Z",
      "lawyer": [
          {
               "atty_name": "Matthew Doe",
               "atty_country": "US",
               "atty_org": null
          }
    ],
    
    "examiner": [
        {
            "name": "Alex Smith",
            "role": "primary"
        }
    ],
    "reldoc": [
        {
            "related_doc": "06336679"
        }
    ]
   }
]

```

I wanted to get all result with `"atty_name": John doe` like this but instead I am getting a bunch of different names.

```
"lawyer": [
              {
                   "atty_name": "John Doe",
                   "atty_country": "US",
                   "atty_org": null
              }
        ]

```

Please inform me if there is still something you don't understand on my question. Thank you very much.

---

<div class="post-metadata">

**Author:** ![Opster\_Community1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/opster_community1/32/59062_2.png) [@Opster\_Community1](https://discuss.elastic.co/u/Opster_Community1)\
**Post date:** [December 16, 2019, 3:46am UTC](https://discuss.elastic.co/t/query-string-query-exact-word-search/211865/7 "2019-12-16T03:46:05Z")

</div>

Please add mapping of the index. To get the mapping you can use:

```
GET <index>/_mapping
```

---

<div class="post-metadata">

**Author:** ![Kevin\_Juliano](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kevin_juliano/32/48845_2.png) [@Kevin\_Juliano](https://discuss.elastic.co/u/Kevin_Juliano)\
**Post date:** [December 16, 2019, 3:53am UTC](https://discuss.elastic.co/t/query-string-query-exact-word-search/211865/8 "2019-12-16T03:53:18Z")

</div>

Thanks @Opster_Community1, Sorry, here's the mapping

```
{
"records": {
    "mappings": {
        "properties": {
            "@timestamp": {
                "type": "date"
            },
            "@version": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "id": {
                "type": "long"
            },
            "applicant": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "examiner": {
                "properties": {
                    "exm_name": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "role": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            },
            "lawyer": {
                "properties": {
                    "atty_country": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "atty_name": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "atty_org": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            },
            "reldoc": {
                "properties": {
                    "related_doc": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            },
            "tags": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            }
        }
    }
}

```

}

---

<div class="post-metadata">

**Author:** ![Opster\_Community1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/opster_community1/32/59062_2.png) [@Opster\_Community1](https://discuss.elastic.co/u/Opster_Community1)\
**Post date:** [December 16, 2019, 4:13am UTC](https://discuss.elastic.co/t/query-string-query-exact-word-search/211865/9 "2019-12-16T04:13:15Z")

</div>

For making an exact search I would suggest you to use `term` query instead of `query _string` as term query is meant for the same. This can be done as below:

```
GET records/_search
{
  "query": {
    "term": {
      "lawyer.atty_name.keyword": "John Doe"
    }
  }
}

```

Still if you want to perform the search using `query_string` then you have to wrap the search string (John Doe) in double quotes as below:

```
GET records/_search
{
  "query": {
    "query_string": {
      "query": "lawyer.atty_name.keyword:\"John Doe\"",
      "fuzziness": 0
    }
  }
}

```

Note that I'm using `keyword` sub field since it is of type `keyword` which index the string as is without applying any analyzers on it.

---

<div class="post-metadata">

**Author:** ![Kevin\_Juliano](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kevin_juliano/32/48845_2.png) [@Kevin\_Juliano](https://discuss.elastic.co/u/Kevin_Juliano)\
**Post date:** [December 16, 2019, 4:20am UTC](https://discuss.elastic.co/t/query-string-query-exact-word-search/211865/10 "2019-12-16T04:20:42Z")

</div>

GREAT!! Thank you very much! I will use `query_string` because I have a very complex search combinations 🙂 Thanks Again!

---

<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:** [January 13, 2020, 4:20am UTC](https://discuss.elastic.co/t/query-string-query-exact-word-search/211865/11 "2020-01-13T04:20:50Z")

</div>

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