# How to give priority to exact search in multiple should match?

**URL:** <https://discuss.elastic.co/t/how-to-give-priority-to-exact-search-in-multiple-should-match/253300>\
**Category:** Elasticsearch\
**Created:** [October 26, 2020, 11:54am UTC](https://discuss.elastic.co/t/how-to-give-priority-to-exact-search-in-multiple-should-match/253300 "2020-10-26T11:54:57Z")\
**Posts on this page:** 2\
**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 26, 2020, 11:54am UTC](https://discuss.elastic.co/t/how-to-give-priority-to-exact-search-in-multiple-should-match/253300/1 "2020-10-26T11:54:57Z")

</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:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 23, 2020, 11:55am UTC](https://discuss.elastic.co/t/how-to-give-priority-to-exact-search-in-multiple-should-match/253300/2 "2020-11-23T11:55:16Z")

</div>

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