# Match two tags in same time (not one at the time)

**URL:** <https://discuss.elastic.co/t/match-two-tags-in-same-time-not-one-at-the-time/303438>\
**Category:** Elasticsearch\
**Created:** [April 27, 2022, 7:23pm UTC](https://discuss.elastic.co/t/match-two-tags-in-same-time-not-one-at-the-time/303438 "2022-04-27T19:23:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eugenci](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eugenci/32/104948_2.png) [@eugenci](https://discuss.elastic.co/u/eugenci)\
**Post date:** [April 27, 2022, 7:23pm UTC](https://discuss.elastic.co/t/match-two-tags-in-same-time-not-one-at-the-time/303438/1 "2022-04-27T19:23:05Z")

</div>

I folders index with following mapping:

```auto
{
  "mappings": {
    "_doc": {
      "properties": {
        "tags": {
          "type": "nested",
          "properties": {
            "bg_color": {
              "type": "keyword"
            },
            "fg_color": {
              "type": "keyword"
            },
            "name": {
              "type": "keyword"
            }
          }
        },
        "title": {
          "type": "text"
        }
      }
    }
  }
}

```

And three Folder instances with following tags (colors are ignored, they are irrelevant to the problem):

folder1 has just one tag ["one"]  
folder2 has just one tag ["two"]  
folder12 has two tags ["one", "two"]

What I am trying to achieve is to filter folder that has **both tags**"one" **and**"two". The result should contain only "folder12".  
Here are queries that I've tried:

Attempt #1 returns zero results (i.e no matches)

```auto
GET /folders/_search
{
  "query": {
    "nested": {
      "path": "tags",
      "query": {
        "bool" : {
          "must": [
            {
              "term": {
                "tags.name": "two"
              }
            },
            {
              "term": {
                "tags.name": "one"
              }
            }
          ]
        }
      } 
    }
  }
}

```

Attempt #2 returns all three folders:

```auto
GET /folders/_search
{
  "query": {
    "nested": {
      "path": "tags",
      "query": {
        "bool" : {
          "must": [
            {
              "match_all": {}
            }
          ],
          "filter": {
              "terms": {
                "tags.name": ["two", "one"]
              }
            }
          }
        }
      } 
    }
  }

```

Attempt #3 again returns all three folders:

```auto
GET /folders/_search
{
  "query": {
    "nested": {
      "path": "tags",
      "query": {
        "bool": {
          "should": [
            { "match": { "tags.name": "two" } },
            { "match": { "tags.name": "one" } }
          ]
        }
      }
    }
  }
}

```

Please help me to find the query which will return only folder12 i.e. will match/filter only the items what both tags "one" and "two" present in same time.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 27, 2022, 8:38pm UTC](https://discuss.elastic.co/t/match-two-tags-in-same-time-not-one-at-the-time/303438/2 "2022-04-27T20:38:45Z")

</div>

Hi.  
Try this:

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "tags",
            "query": {
              "term": {
                "tags.name": {
                  "value": "one"
                }
              }
            }
          }
        },
         {
          "nested": {
            "path": "tags",
            "query": {
              "term": {
                "tags.name": {
                  "value": "two"
                }
              }
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![eugenci](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eugenci/32/104948_2.png) [@eugenci](https://discuss.elastic.co/u/eugenci)\
**Post date:** [April 28, 2022, 4:05am UTC](https://discuss.elastic.co/t/match-two-tags-in-same-time-not-one-at-the-time/303438/3 "2022-04-28T04:05:20Z")

</div>

It works! It returns only folder12 (i.e. folder with both tags only)  
So it was about correct placement of "nested "...

@RabBit_BR thank you for your help!

---

<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:** [May 26, 2022, 4:05am UTC](https://discuss.elastic.co/t/match-two-tags-in-same-time-not-one-at-the-time/303438/4 "2022-05-26T04:05:52Z")

</div>

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