I have a problem with results come from an ES query. Filtering a query gives some confusing results, and I don't know how to overcome of it. So:
{ "query":{ "bool":{ "should":[ { "multi_match": { "query":"searchquery", "operator":"and", "fields":["Name", "Description", "Text"] } }, { "bool":{ "must": [ {"terms": {"Id": [1, 2]}}, {"term": {"_type": "Project"}} ] } } ] } } }
What I want here is: get all records whose Name, or Description, or Text is match the query OR all Projects (it's a _type in the mapping), whose Id 1, or 2. The results seems exactly what I expected, I got eg: 5 hits, 2 Contact _type (matches "searchquery"), 1 Project (matches "searchquery") and 2 projects with Id 1, or 2. Good!
But I have some authorization, so not all users access all Modules (Projects have another field eg: ModuleId).
So I want to restrict the results to specific ModuleIds:
{ "query":{ "bool":{ "should":[ { "multi_match": { "query":"searchquery", "operator":"and", "fields":["Name", "Description", "Text"] } }, { "bool":{ "must": [ {"terms": {"Id": [1, 2]}}, {"term": {"_type": "Project"}} ] } } ], "filter": [ {"terms": {"ModuleId": [9, 5]}} ] } } }
And boom! In the result I got all, whose match the above query and all Projects with ModuleId 9, or 5. What I want is to narrow the results, not broaden them. I've tried many combinations with should, must, filter(bool filter as well). I've also tried post_filter which "magically" gives me the expected result, but I have some aggregations as well (not in this example), sot it is not an option.