# How to exclude querystring reponse and keep only exact match if present?

**URL:** <https://discuss.elastic.co/t/how-to-exclude-querystring-reponse-and-keep-only-exact-match-if-present/253412>\
**Category:** Elasticsearch\
**Created:** [October 27, 2020, 9:28am UTC](https://discuss.elastic.co/t/how-to-exclude-querystring-reponse-and-keep-only-exact-match-if-present/253412 "2020-10-27T09:28:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![darshan\_ambre](https://avatars.discourse-cdn.com/v4/letter/d/85f322/32.png) [@darshan\_ambre](https://discuss.elastic.co/u/darshan_ambre)\
**Post date:** [October 27, 2020, 9:28am UTC](https://discuss.elastic.co/t/how-to-exclude-querystring-reponse-and-keep-only-exact-match-if-present/253412/1 "2020-10-27T09:28:39Z")

</div>

How to return only exact match if exists else perform wildcard search. for e.g. for the below given set of data

```auto
    [
      {
        "_index" : "priority_test",
        "_type" : "_doc",
        "_id" : "3IkeZHUBYJCjRlSv-EXV",
        "_score" : 1.0,
        "_source" : {
          "number" : "GI#000001",
          "name" : "GI for Cost Center",
          "comments" : "Cost Center",
          "id" : "000001",
          "PlantCode" : "ABC"
        }
      },
      {
        "_index" : "priority_test",
        "_type" : "_doc",
        "_id" : "3YkeZHUBYJCjRlSv-EXV",
        "_score" : 1.0,
        "_source" : {
          "number" : "GI#0000010",
          "name" : "GI for WBS Element",
          "comments" : "WBS Element",
          "id" : "0000010",
          "PlantCode" : "XYZ"
        }
      },
      {
        "_index" : "priority_test",
        "_type" : "_doc",
        "_id" : "3okeZHUBYJCjRlSv-EXV",
        "_score" : 1.0,
        "_source" : {
          "number" : "GI#0000011",
          "name" : "GI for Cost Center & WBS Element",
          "comments" : "Cost Center & WBS Element",
          "id" : "0000011",
          "PlantCode" : "LMN"
        }
      },
      {
        "_index" : "priority_test",
        "_type" : "_doc",
        "_id" : "4IkeZHUBYJCjRlSv-EXV",
        "_score" : 1.0,
        "_source" : {
          "number" : "GI#00000100",
          "name" : "Manage Pick List GI",
          "comments" : "Manage Pick List GI",
          "id" : "00000100",
          "PlantCode" : "QRS"
        }
      }
    ]

```

In the above data, If i search for "GI#000001" in document number, The result must return only that document, otherwise it should perform a contains search that starts with "GI#000001\*".

I've formed a query with looks something like the following:

```auto
    {
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "number.keyword": [
              "GI#000001"
            ]
          }
        },
        {
          "query_string": {
            "fields": [
              "number.keyword"
            ],
            "query": """GI\#000001*"""
          }
        }
      ]
    }
  }
}

```

But the above query always returns a result even if there is an exact match available for "GI#000001".

Please guide me in building this search

---

<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:** [October 28, 2020, 10:28am UTC](https://discuss.elastic.co/t/how-to-exclude-querystring-reponse-and-keep-only-exact-match-if-present/253412/2 "2020-10-28T10:28:44Z")

</div>

I'd probably use the multi search API (which means 2 searches) and on the client side, I'd deal with the use case.  
So you will get every time 2 responses.

If you know in advance that in most cases, the second query is not needed, I'd just call the first query and if empty call the second.

My 2 cents.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [October 28, 2020, 1:59pm UTC](https://discuss.elastic.co/t/how-to-exclude-querystring-reponse-and-keep-only-exact-match-if-present/253412/3 "2020-10-28T13:59:39Z")

</div>

Interesting, @dadoonet - we could be more efficient if we had a new [fail-if query](https://github.com/elastic/elasticsearch/issues/51840#issuecomment-717950165) on the second backup search to cut it short if it looks like the primary search is working out.

---

<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:** [November 25, 2020, 2:00pm UTC](https://discuss.elastic.co/t/how-to-exclude-querystring-reponse-and-keep-only-exact-match-if-present/253412/4 "2020-11-25T14:00:10Z")

</div>

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