How to specify or target a field from a specific document type in queries or filters


  1. Documents of two different types, let's say 'product' and 'category', are indexed to the same Elasticsearch index.
  2. Both document types have a field named 'tags'.


I want to build a query that returns results of both types, but the documents of type 'product' are allowed to have tags 'X' and 'Y', and the documents of type 'category' are only allowed to have tag 'Z'. How can I achieve this? It appears I can't use product.tags and category.tags in filter clauses since then ES will look for documents' product/category field, which is not what I intend. I have found a few people scattered over the internet who suggest to do it like that but ES seems to interpret 'category' and 'product' as fields of a document rather than the document type, so I get no results since these fields don't exist on any doc type.


While for the example above there might be some kind of workaround, I'm looking for a general way to target or specify fields of a specific document type when writing queries. I basically want to 'namespace' the field names used in my query so only documents of the type I want to work with are considered.