elasticsearch : make a should into a filter - elasticsearch

I would want to do search with a filter that exclude result that not match a condition OR anothor condition:
I tried to do a should into a filter but it fails:
POST /my_index/_search
{
"query": {
"bool": {
"filter": [
{
"should": [
{
"match": {
"type1_title": "searched match"
}
},
{
"match": {
"type2_title": "searched match"
}
}
]
}
]
}
}
}
it raises that error:
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "[should] query malformed, no start_object after query name",
"line": 9,
"col": 21
}
],
"type": "parsing_exception",
"reason": "[should] query malformed, no start_object after query name",
"line": 9,
"col": 21
},
"status": 400
}
Do you know if we can do an or in a filter?

Why not simply bool/should, there's no need for filter here
POST /my_index/_search
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"match": {
"type1_title": "searched match"
}
},
{
"match": {
"type2_title": "searched match"
}
}
]
}
}
}
If you really want to keep the bool/filter/should construct then you need to do it like this:
POST /my_index/_search
{
"query": {
"bool": {
"filter": [
{
"bool": {
"should": [
{
"match": {
"type1_title": "searched match"
}
},
{
"match": {
"type2_title": "searched match"
}
}
]
}
}
]
}
}
}

Related

Post_filter on nested type raises elastic error

My index mapping: https://pastebin.com/eBPKXufH
The query:
{
"query": {
"bool": {
"filter": [
[
{
"nested": {
"path": "categories",
"query": {
"match": {
"categories.cats": "360"
}
}
}
}
]
]
}
},
"sort": {
"sale": {
"order": "desc"
}
},
"post_filter": [
{
"nested": {
"path": "64",
"filter": {
"match": {
"64.value": "207"
}
}
}
}
]
}
The error I get is:
{
"error": {
"root_cause": [
{
"type": "x_content_parse_exception",
"reason": "[1:29] [bool] failed to parse field [filter]"
}
],
"type": "x_content_parse_exception",
"reason": "[1:29] [bool] failed to parse field [filter]",
"caused_by": {
"type": "illegal_state_exception",
"reason": "expected value but got [START_ARRAY]"
}
},
"status": 400
}
What I try to accomplish is filter by nested value of categories.cats which is constant query and then post_filter it by nested value of object 64 etc, which is filter criteria but I also need all other values in aggregations (did not post the aggregation query, since it is not relevant I think)
You have too many square brackets:
"bool": {
"filter": [
[ <--- remove this and the closing one
{
should be
"bool": {
"filter": [
{

ES 7 - Use match query with filters

I use ElasticSearch 7.0, and I have this simple query :
"bool": {
"must": {
"match": {
"title": {
"query": "engineer"
}
}
},
"filter": {
"0": {
"term": {
"type_id": 1
}
},
"term": {
"active": 1
}
}
}
And I get this error :
[match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]
I tried :
"must": {
"match": {
"title": "engineer"
}
},
But same error, I can't see my syntax mistake here ? I have same query working with multimatch with same filters.
Your filters must be enclosed in an array, like this:
{
"bool": {
"must": {
"match": {
"title": {
"query": "engineer"
}
}
},
"filter": [
{
"term": {
"type_id": 1
}
},
{
"term": {
"active": 1
}
}
]
}
}

How to compare two fields in the same document in ElasticSearch?

I am using ElasticSearch 7.2.0,
I have documents in an index with three fields - id, field1, field2
I want to query and return those documents whose field1 > 20 AND field1 > field2
Following is the data that the index has -
Following is the query that I'm trying -
GET /test/_search
{
"query": {
"function_score": {
"query": {
"constant_score": {
"filter": {
"bool": {
"must": [
{
"range" : {
"field1" : {
"gte" : 20
}
}
},
{
"script": {
"source": "doc['field1'].value > doc['field2'].value",
"params": {
}
}}
]
}
}
}
}
}
}
}
Following is the error -
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "[script] query does not support [source]",
"line": 18,
"col": 29
}
],
"type": "parsing_exception",
"reason": "[script] query does not support [source]",
"line": 18,
"col": 29
},
"status": 400
}
Your query is correct! You just need to wrap the script inside another script. The first is to denote a script query, the second is to actually define the script:
{
"query": {
"function_score": {
"query": {
"constant_score": {
"filter": {
"bool": {
"must": [
{
"range": {
"field1": {
"gte": 20
}
}
},
{
"script": {
"script": {
"source": "doc['field1'].value > doc['field2'].value",
"params": {}
}
}
}
]
}
}
}
}
}
}
}

malformed query, expected END_OBJECT but found FIELD_NAME error in Kibana (Elastic Search)

I am running the following GET query within my Kibana Console and for some reason I am getting a error in the response window as follows :
// error
[match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]
Can anyone suggest why I am not able to use multiple match blocks within the 'should' section?
// response - if i take out one of the match blocks it works??
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "[match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
"line": 9,
"col": 13
}
],
"type": "parsing_exception",
"reason": "[match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
"line": 9,
"col": 13
},
"status": 400
}
// my query
GET _search
{
"query": {
"bool": {
"should": [
{
"match": {
"text": "facebook advice"
},
"match": {
"profile": "facebook advice"
}
}
],
"minimum_number_should_match": 1,
"filter": {
"term": {
"accountid": "22"
}
}
}
}
Your query is malformed. Write it like this instead:
GET _search
{
"query": {
"bool": {
"should": [
{
"match": {
"text": "facebook advice"
}
},
{
"match": {
"profile": "facebook advice"
}
}
],
"minimum_number_should_match": 1,
"filter": {
"term": {
"accountid": "22"
}
}
}
}
}
Give the below query a try.. It works for me.
-------- working console query -------------
POST /usage-metering-stats/_search?size=10
{
"query": {
"bool": {
"must": [{
"term": {
"tenantId": "2222"
}
},
{
"term": {
"instanceId": "1212"
}
},
{
"term": {
"cspId": "25680"
}
},
{
"term": {
"api": "2"
}
}
]
}
},
"aggs": {
"totalCount": { "sum": { "field": "count" } }
}
}

Elasticsearch (version 2.3) Function Score Query with filtered type query

I am very new to elastic search, We are migrating from Solr to elastic-search. As part of migration working converting existing Solr query to elastic-search DSL query.
Here is the DSL query I have partially completed using function score feature.
{
"query": {
"function_score": {
"query": {
"filtered": {
"match": {
"name": "barack obama"
},
"filter": {
"range": {
"relevance": {
"gte": 6
}
},
"bool": {
"must_not": [
{
"terms": {
"classIds": [
199,
220
],
"execution": "and"
}
}
],
"must": [
{
"term": {
"classIds": 10597
}
}
]
}
}
}
},
"boost_mode": "replace",
"functions": [
{
"script_score": {
"script": {
"lang": "groovy",
"file": "calculate-score",
"params": {
"relevance_boost": 1,
"class_penalize": 0.25
}
}
}
}
]
}
}
}
This query returning error while am running against elastic-search cluster. Please help me to figure out the issue.
Here calculate-score is groovy script and its working fine, I tested that with simple query.
Here is the error response:
{
"error": {
"root_cause": [
{
"type": "query_parsing_exception",
"reason": "[filtered] query does not support [match]",
"index": "nodes_5e27a7d3-b370-40bd-9e71-cf04a36297c0",
"line": 6,
"col": 11
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "nodes_5e27a7d3-b370-40bd-9e71-cf04a36297c0",
"node": "NOAwAtVwQS25egu7AIaHEg",
"reason": {
"type": "query_parsing_exception",
"reason": "[filtered] query does not support [match]",
"index": "nodes_5e27a7d3-b370-40bd-9e71-cf04a36297c0",
"line": 6,
"col": 11
}
}
]
},
"status": 400
}
Here is Solr query I am trying to convert to elastic-search:
SOLR QUERY (UNIQUE_NODE_CORE): q={!boost b="product(pow(field(relevance),1.0000),if(exists(query({!v='all_class_ids:226'})),0.25,1),if(exists(query({!v='all_class_ids:14106'})),0.25,1),if(exists(query({!v='all_class_ids:656'})),0.25,1))"}
raw_name:"barack obama"
&rows=1
&start=0
&sort=score desc,relevance desc
-&fq=class_id:"10597"
-fq=relevance:[6 TO *]
-&fq=-all_class_ids:"14127"
-&fq=-all_class_ids:"14106"
-&fq=-all_class_ids:"226"
&fl=ontology_id,url_friendly_name,name,score,raw_notable_for,property_207578
Just need help to run filtered query with function score.
Great job, you're almost there, you're just missing a query section inside your filtered query in order to wrap the match query. As well, the range filter can be inserted into the bool/must. Quite a mouthful, I know.
{
"query": {
"function_score": {
"query": {
"filtered": {
"query": {
"match": {
"name": "barack obama"
}
},
"filter": {
"bool": {
"must_not": [
{
"terms": {
"classIds": [
199,
220
],
"execution": "and"
}
}
],
"must": [
{
"range": {
"relevance": {
"gte": 6
}
}
},
{
"term": {
"classIds": 10597
}
}
]
}
}
}
},
"boost_mode": "replace",
"functions": [
{
"script_score": {
"script": {
"lang": "groovy",
"file": "calculate-score",
"params": {
"relevance_boost": 1,
"class_penalize": 0.25
}
}
}
}
]
}
}
}
Note that since ES 2.0 the filtered query is deprecated and you can rewrite it with a bool/must/filter query like this:
{
"query": {
"function_score": {
"query": {
"bool": {
"must": {
"match": {
"name": "barack obama"
}
},
"filter": [
{
"range": {
"relevance": {
"gte": 6
}
}
},
{
"term": {
"classIds": 10597
}
}
],
"must_not": [
{
"terms": {
"classIds": [
199,
220
],
"execution": "and"
}
}
]
}
},
"boost_mode": "replace",
"functions": [
{
"script_score": {
"script": {
"lang": "groovy",
"file": "calculate-score",
"params": {
"relevance_boost": 1,
"class_penalize": 0.25
}
}
}
}
]
}
}
}

Resources