# ElasticSearch - Match or less

**URL:** <https://discuss.elastic.co/t/elasticsearch-match-or-less/279341>\
**Category:** Elasticsearch\
**Created:** [July 22, 2021, 6:17am UTC](https://discuss.elastic.co/t/elasticsearch-match-or-less/279341 "2021-07-22T06:17:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhadad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zhadad/32/91976_2.png) [@zhadad](https://discuss.elastic.co/u/zhadad)\
**Post date:** [July 22, 2021, 6:17am UTC](https://discuss.elastic.co/t/elasticsearch-match-or-less/279341/1 "2021-07-22T06:17:56Z")

</div>

is there an way to query the following using elasticsearch (version 2) :

indexed documents examples :

{\_id=1 , "tags": ["t1"]}

{\_id=2 , "tags": ["t1", "t2"]}

{\_id=3 , "tags": ["t1", "t2", "t3"]}

The following are query examples and expected results :

**"tags": ["t1"]** ---\> document \_ids=1

**"tags": ["t1","t2"]** ---\> document \_ids=1,2 (document #1 is expected too)

**"tags": ["t1","t2","t3"]** ---\> document \_ids=1,2,3

**"tags": ["t2","t3"]** ---\> document \_ids=NONE

**"tags": ["t1","t3"]** ---\> document \_ids=1 only

```auto
PUT my-index-000001/_doc/1
{ "tags": ["t1"]}

PUT my-index-000001/_doc/2
{ "tags": ["t1", "t2"]}

PUT my-index-000001/_doc/3
{ "tags": ["t1", "t2", "t3"]}

```

The following query yield more than expected and include document #3 too in addition to #1&#2

```auto
GET my-index-000001/_search
{
  "query": {
    "match": {
      "tags": ["t1","t2"] 
    }
  }
}

```

And on the following, document #3 is not matched but either the expected #1

```auto
GET metric_*/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "tags": "t1"
                    }
                },
                {
                    "match": {
                        "tags": "t2"
                    }
                }
            ]
        }
    }
}

```

I need a way exclude any document that has tag value that is not exist in the query tag list

thanks in advance

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [July 22, 2021, 7:31am UTC](https://discuss.elastic.co/t/elasticsearch-match-or-less/279341/2 "2021-07-22T07:31:23Z")

</div>

I do not think I can find any way of doing this in Elasticsearch 2.x, but in recent versions (7.x) I suspect you might be able do this through a [terms set query](https://www.elastic.co/guide/en/elasticsearch/reference/7.13/query-dsl-terms-set-query.html) if you also index the size of the array in the document and use this as a `minimum_should_match_field` field.

---

<div class="post-metadata">

**Author:** ![zhadad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zhadad/32/91976_2.png) [@zhadad](https://discuss.elastic.co/u/zhadad)\
**Post date:** [July 22, 2021, 4:05pm UTC](https://discuss.elastic.co/t/elasticsearch-match-or-less/279341/3 "2021-07-22T16:05:25Z")

</div>

taking the blacklist approach solve the issue (using must\_not). since we know the full list of options in the tag field and it's relatively small.

query example :

```auto
GET tags/_search
{
    "query": {
        "bool": {
            "must_not": {
                "terms": {
                    "orgSourceIds": [
                        "t1",
                        "t2"
                    ]
                }
            }
        }
    }
}

```

---

<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 19, 2021, 4:06pm UTC](https://discuss.elastic.co/t/elasticsearch-match-or-less/279341/4 "2021-08-19T16:06:21Z")

</div>

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