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

I folders index with following mapping:

{
  "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)

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

Attempt #2 returns all three folders:

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:

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.

Hi.
Try this:

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

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!

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