There are two types of context now: query context and filter context. Any query clause inside query context contribute to the score of matching document i.e. how well a document matched a query whereas any query clause inside filter context determines whether or not the document matches and doesn't contribute to the score.
So in the query below I have marked both the contexts:
{
"query": {
"bool": {
"must": [
{ "match": { "field1": "terms1"}}, <------query context
],
"filter": [
{ "terms": { "someId": [3242, 12343, 1234] }}, <-------inside filter block every query clause will act as filter (filter context)
]
}
}
}
Therefore in order to use terms query as a filter you need to follow the below steps:
- Create a bool query say
boolQuery
- Create a terms query sat
termsQuery
- Add
termsQuery
to filter of boolQuery
.
- Set
boolQuery
as query.
This translates to below:
{
"query": {
"bool": {
"filter": [
{
"terms": {
"field1": ["terms1", "term2", "terms3"]
}
}
]
}
}
}