I am going through an Elasticsearch tutorial, and I had something pop into my mind. What if I want to filter based on two different attributes at the same time? I have this query about searching for articles about Donald Trump from recent news articles:
GET news_headlines/_search
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"headline": "Donald Trump"
}
}
],
"filter":{
"range":{
"date": {
"gte": "2014-03-25",
"lte": "2016-03-25"
}
}
}
}
}
}
I tried adding something within the filter section to exclude articles with category equal to POLITICS, but I received an error. What would be the best way to syntactically incorporate that into my filter section? Thanks in advance... I am a noob at this.
CodePudding user response:
Sounds like you need must_not. It will exclude the docs matching the query.
Assuming you have a keyword field for category:
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"headline": "Donald Trump"
}
}
],
"filter":{
"range":{
"date": {
"gte": "2014-03-25",
"lte": "2016-03-25"
}
}
},
"must_not" : [
{
"term":{
"category": {
"value": "POLITICS",
}
}
}]
}
}
}
