Exact match on dynamic object field


#1

I have a field with a dynamic mapping:

"properties": {
  "tags": {
    "type": "object",
    "dynamic": "true"
  }
}

Now I want to find the documents that have tags.color = "red" and tags.species = "herring" but no other tags.

This query finds such documents:

{
  "query": {
    "bool": {
      "filter": [
        { "term": { "tags.color": "red" } },
        { "term": { "tags.species": "herring" } }
      ]
    }
  }
}

Unfortunately it also finds documents with more than those two tags. How can I remove those from the result?