First of all, I'm very new to Elasticsearch. I'm using the python library to run queries.
I have documents with lists embedded inside other lists, for example:
{"vendors": [{'id': 22603,
'name': 'Facebook',
'products': [{'id': 4469256,
'name': 'osquery',
'versions': [{'id': 169014,
'name': '3.2.7',
'affected': False,},
{'id': 44084,
'name': '3.2.6',
'affected': True}]}]}]}
}
For context, this is a vulnerabilities database.
A vendor can have multiple products and each product, besides the name, different versions.
Each version has a name and a flag affected.
What I need to get is: get me all the documents, where the product name is xxx, version is yyy and affected is zzz.
For example: product name is osquery, version is 3.2.7 and affected is True.
One of the many ways I've sent the query (with no success) is:
{'query': {'bool': {"must": [{"term": {"vendors.products.versions.affected": True}},
{'term': {'vendors.products.versions.name': "3.2.7"}},
{"term": {"vendors.products.name": "osquery"} } ] } }}
The problem is that this query is returning me the document I posted above, even though version 3.2.7 has affected = False.
So it seems its doing an OR instead of and AND inside the elements of the versions list, since it finds a version that matches, and another, different version, with a matching affected value, it returns the document, but is not the expected result.
Is there any way to force it to use the AND? I've tried the default_operator parameter in different queries, but that seems to work only for query_string queries. Or, is there a best way to query for elements inside lists?