# Find one term result, or a list of match results

**URL:** <https://discuss.elastic.co/t/find-one-term-result-or-a-list-of-match-results/237472>\
**Category:** Elasticsearch\
**Created:** [June 17, 2020, 12:42pm UTC](https://discuss.elastic.co/t/find-one-term-result-or-a-list-of-match-results/237472 "2020-06-17T12:42:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![darraghenright](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/darraghenright/32/70533_2.png) [@darraghenright](https://discuss.elastic.co/u/darraghenright)\
**Post date:** [June 17, 2020, 12:42pm UTC](https://discuss.elastic.co/t/find-one-term-result-or-a-list-of-match-results/237472/1 "2020-06-17T12:42:19Z")

</div>

Hi there 👋

Sorry, this is undoubtedly a bit of noob question but I am still trying to get to grips with compound queries in Elasticsearch.

In my example, each document has a `name` field and we can assume that each `name` field is unique. The mapping is as follows (there's also an id field that I've excluded for brevity):

```auto
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "exact": { 
            "type": "keyword"
          }
        }
      }
    }
  }
}

```

I am wondering how to go about the following as a single query:

> Perform a keyword match query on the `name` field to find one result. If that query doesn't find any results, perform a match query on the `name` field to return a list of similar results.

In other words, find one exact result OR find a list of similar results.

My first attempt was pretty naive, this obviously returns the exact match if present AND similar results:

```auto
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "name.exact": "Acme Incorporated"
          }
        },
        {
          "match": {
            "name": "Acme Incorporated"
          }
        }
      ]
    }
  }
}

```

Any advice would be greatly appreciated 🙂 Thanks!

---

<div class="post-metadata">

**Author:** ![darraghenright](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/darraghenright/32/70533_2.png) [@darraghenright](https://discuss.elastic.co/u/darraghenright)\
**Post date:** [June 17, 2020, 2:30pm UTC](https://discuss.elastic.co/t/find-one-term-result-or-a-list-of-match-results/237472/2 "2020-06-17T14:30:40Z")

</div>

I did some more reading and I guess that another approach would be to use `_mmatch` to perform two separate queries, and pick and choose as required. This will work for my use case but I'm not sure if that's really the best approach so I am all ears on that 🙂

---

<div class="post-metadata">

**Author:** ![darraghenright](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/darraghenright/32/70533_2.png) [@darraghenright](https://discuss.elastic.co/u/darraghenright)\
**Post date:** [June 18, 2020, 12:49pm UTC](https://discuss.elastic.co/t/find-one-term-result-or-a-list-of-match-results/237472/3 "2020-06-18T12:49:51Z")

</div>

I've decided to go with the Multi Search approach. In this case the first query will be the same keyword-based term query on the `name` field to attempt to find an exact match.

For the second query, I'll perform a match query, but I will also explicitly exclude possible exact results with `must_not`. Something like:

```auto
{
	"query": {
		"bool": {
			"must": {
				"match": {
					"name": "Acme Incorporated"
				}
			},
			"must_not": {
				"term": {
					"name.exact": "Acme Incorporated"
				}
			}
		}
	}
}

```

I can then decide at application level what to do with the results.

---

<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:** [July 16, 2020, 12:49pm UTC](https://discuss.elastic.co/t/find-one-term-result-or-a-list-of-match-results/237472/4 "2020-07-16T12:49:56Z")

</div>

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