How can I put `must_not` under `filter` in Elasticsearch? - elasticsearch

I'd like to use not equal in my filter but it doesn't work [13:24] [bool] failed to parse field [filter]:
"query": {
"bool": {
"filter": [
{
"must_not" : {
"term" : {
"status" : "DECLINED"
}
}
},
{
"term": { "type": "ORDER"}
}
]
}
}
it works if I put the must_not under query like below. How can I put not equal in filter?
"query": {
"bool": {
"must_not": {
"term": {
"status": "DECLINED"
}
},
"filter": ...

May be one more bool needed inside the filter ?
/_search
{
"query": {
"bool": {
"filter": {
"bool": {
"must": [
{
"term": { "type": "ORDER"}
}
],
"must_not": [
{
"term": {
"status": "DECLINED"
}
}
]
}
}
}
}
}

Related

Filter on field value or not exists

I'm trying to filter a field for a specific value OR that it does not exist.
I have the part for the specific value
{
"query": {
"match": {
"app.serviceType": {
"query": "MY_VALUE",
"type": "phrase"
}
}
}
}
I'd also like to add to this any case where the field serviceType doesn't exist at all.
Essentially I'd like the equivalent of this:
serviceType == "MY_VALUE" || string.IsNullOrEmpty(serviceType)
This request must match with your use case :
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"exists": {
"field": "serviceType"
}
},
{
"match_phrase": {
"serviceType": "MY_VALUE"
}
}
]
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "serviceType"
}
}
]
}
}
]
}
}
}
Based on the previous answer (which didn't work but got me close) I was able to get it to work.
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"exists": {
"field": "app.serviceType"
}
},
{
"match_phrase": {
"app.serviceType": "MY_VALUE"
}
}
]
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "app.serviceType"
}
}
]
}
}
]
}
}
}

ElasticSearch should with nested and bool must_not exists

With the following mapping:
"categories": {
"type": "nested",
"properties": {
"category": {
"type": "integer"
},
"score": {
"type": "float"
}
}
},
I want to use the categories field to return documents that either:
have a score above a threshold in a given category, or
do not have the categories field
This is my query:
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "categories",
"query": {
"bool": {
"must": [
{
"terms": {
"categories.category": [
<id>
]
}
},
{
"range": {
"categories.score": {
"gte": 0.5
}
}
}
]
}
}
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "categories"
}
}
]
}
}
],
"minimum_should_match": 1
}
}
}
It correctly returns documents both with and without the categories field, and orders the results so the ones I want are first, but it doesn't filter the results having score below the 0.5 threshold.
Great question.
That is because categories is not exactly a field from the elasticsearch point of view[a field on which inverted index is created and used for querying/searching] but categories.category and categories.score is.
As a result categories being not found in any document, which is actually true for all the documents, you observe the result what you see.
Modify the query to the below and you'd see your use-case working correctly.
POST <your_index_name>/_search
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "categories",
"query": {
"bool": {
"must": [
{
"terms": {
"categories.category": [
"100"
]
}
},
{
"range": {
"categories.score": {
"gte": 0.5
}
}
}
]
}
}
}
},
{
"bool": {
"must_not": [ <----- Note this
{
"nested": {
"path": "categories",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "categories.category"
}
},
{
"exists": {
"field": "categories.score"
}
}
]
}
}
}
}
]
}
}
],
"minimum_should_match": 1
}
}
}

Query regarding functionScoreQuery of elastic-builder npm for elasticsearch

I am using a functionScoreQuery provided by the elastic-builder npm to query my elasticsearch, query is getting created but i am not able to get outer query params for my query as shown below.
i.e the outer query params are missing and that is why the query does not execute so i had to manually append query { } in my body. So if anyone can help me out and tell me what i am missing in my npm query to get those query params.
var not_body = elasticbuilder.functionScoreQuery()
.query(elasticbuilder.matchAllQuery())
.functions([
elasticbuilder.weightScoreFunction()
.filter(elasticbuilder.boolQuery().mustNot([
elasticbuilder.hasChildQuery(
elasticbuilder.boolQuery().must([
elasticbuilder.matchPhraseQuery("name", "raju" )
])
).type('student')
]))
.weight(2),
elasticbuilder.weightScoreFunction()
.filter(elasticbuilder.boolQuery().must([
elasticbuilder.hasChildQuery(
elasticbuilder.boolQuery().must([
elasticbuilder.matchPhraseQuery("class", "12")
])
).type('info')
]))
.weight(2)
]).minScore(4).scoreMode('sum');
Current Output body via this query:
{
"function_score": {
"functions": [
{
"filter": {
"bool": {
"must_not": {
"has_child": {
"query": {
"bool": {
"must": {
"match_phrase": {
"name" : "raju"
}
}
}
},
"type": "student"
}
}
}
},
"weight": 2
},
{
"filter": {
"bool": {
"must": {
"has_child": {
"query": {
"bool": {
"must": {
"match_phrase": {
"class" : "12"
}
}
}
},
"type": "info"
}
}
}
},
"weight": 2
}
],
"query": {
"match_all": {}
},
"min_score": 4,
"score_mode": "sum"
}
}
Expected Output body:
{
"query": {
"function_score": {
"functions": [
{
"filter": {
"bool": {
"must_not": {
"has_child": {
"query": {
"bool": {
"must": {
"match_phrase": {
"name" : "raju"
}
}
}
},
"type": "student"
}
}
}
},
"weight": 2
},
{
"filter": {
"bool": {
"must": {
"has_child": {
"query": {
"bool": {
"must": {
"match_phrase": {
"class" : "12"
}
}
}
},
"type": "info"
}
}
}
},
"weight": 2
}
],
"query": {
"match_all": {}
},
"min_score": 4,
"score_mode": "sum"
}
}
}
You should wrap this in a elasticbuilder.requestBodySearch()
In your case
elasticbuilder.requestBodySearch().query(not_body)
should do the job

How to use nested filters in Elasticsearch?

I have a set of articles. I want to filter on tags AND host.
E.g I want all articles that are tagged with 'news' OR 'sport' AND has the 'host' set to 'cnn.com' OR 'bbc.com'.
I tried to create this nested bool filter, but that did not work. It also returns articles from other hosts.
Any suggestions?
GET _search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"should": [
// Match one or more of these tags.
{ "term" : { "tags" : "sport"} },
{ "term" : { "tags" : "news"} },
{ "bool": {
// Only from one of these hosts.
"should": [
{ "term": { "host": "bbc.com" } },
{ "term": { "host": "cnn.com" } }
]
}}
]
}
}
}
}
}
Nesting the query like this worked. :-)
GET _search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{ "bool": {
"should": [
{ "term" : { "tags" : "sport"} },
{ "term" : { "tags" : "news"} }
]
}},
{ "bool": {
"should": [
{ "term": { "host": "bbc.com" } },
{ "term": { "host": "cnn.com" } }
]
}}
]
}
}
}
}
}

"boost" not working for "term" query

I'm running Elasticsearch 1.5.2 and trying the following query:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"gender": "male"
}
}
]
}
},
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"should": [
{
"term": {
"top_users": 1,
"boost": 2
}
}
]
}
}
}
}
}
Everything is fine until I add the "boost": 2 to the should -> term part. The complete query is much more complex, that's why I need to boost, but the remaining queries don't make any difference: ES returns an error 400 if a term query gets a boost argument:
QueryParsingException[[index_name] [_na] query malformed, must start with start_object]
Any suggestions?
It should be like this:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"gender": "male"
}
}
]
}
},
"query": {
"bool": {
"must": [
{
"match_all": {}
}
],
"should": [
{
"term": {
"top_users": {
"value": "1",
"boost": 2
}
}
}
]
}
}
}
}
}

Resources