Elasticsearch query for getting records with null values in a field - elasticsearch

I'm trying to get the result of all the records where the field iso3 contains some values and null, but I always get the following error:
{
"code": 400,
"error": "RequestError(400, 'parsing_exception', '[or] query malformed, no start_object after query name')"
}
this is the query:
{
"query": {
"bool": {
"should": [
{"term":{"iso3":"afg"}},
{"term":{"iso3":"idn"}},
{"term":{"iso3":"bgd"}},
{
"or": [
{"term": { "iso3": "" } },
{"term": { "iso3": null}}
]
}
]
}
},
"size": 20
}
so this part is wrong, but I cant understand why
{
"or": [
{"term": { "iso3": "" } },
{"term": { "iso3": null}}
]
}

There is no or query, but you can achieve what you need with the following query:
{
"query": {
"bool": {
"minimum_should_match": true,
"should": [
{
"term": {
"iso3": "afg"
}
},
{
"term": {
"iso3": "idn"
}
},
{
"term": {
"iso3": "bgd"
}
},
{
"term": {
"iso3": ""
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "iso3"
}
}
}
}
],
"filter": [
{
"exists": {
"field": "iso2"
}
}
]
}
},
"size": 20
}

Related

Elastic search - handling the condition using must or must not query

We have a requirement if newId is there then we have to get the data less than todays date
and if newId field is not there in the data then we have to get the data till expiry date + 2Months.
I was trying below query but result has not come as expected.
{
"id":"234",
"startDate":"23/07/2020",
"endDate":"24/09/20202",
"newId":"2345"
},
{
"id":"234",
"startDate":"23/07/2020",
"endDate":"24/09/20202",
"newId":null
},
{
"id":"235",
"startDate":"23/07/2020",
"endDate":"24/06/2020",
"newId":"2345"
},
Query that I was trying
{
"query": {
"bool": {
"must": [
{
"match_all": {}
},
{
"bool": {
"must": [
{
"bool": {
"must": [
{
"exists": {
"field": "newId"
}
},
{
"range": {
"endDate": {
"gte":"now/d"
}
}
}
]
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "newId"
}
},
{
"range": {
"endDate": {
"gte": "now-2M"
}
}
}
]
}
}
]
}
}
]
}
}
}
Expected result
{
"id":"234",
"startDate":"23/07/2020",
"endDate":"24/09/20202",
"newId":"2345"
},
{
"id":"234",
"startDate":"23/07/2020",
"endDate":"24/09/20202",
"newId":null
},
Great start! Your query is almost right, but you need a few more tweaks, namely to use should instead of must, because both sub-queries will never be true at the same time:
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"must": [
{
"exists": {
"field": "newId"
}
},
{
"range": {
"endDate": {
"gte": "now/d"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"range": {
"endDate": {
"gte": "now-2M"
}
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "newId"
}
}
]
}
}
]
}
}
]
}
}
}

Elasticsearch query error - [or] query malformed, no start_object after query name

Can somebody explain me please what is wrong with this query? I need to convert this generated query from Elasticsearch 2 to Elasticsearch 6. In ES2 this one works well, but in ES6 it throws me an error: [or] query malformed, no start_object after query name. I am lost in it. OR is necessary cause there could be more conditions than this one.
{
"query": {
"bool": {
"filter": {
"or": [
{
"nested": {
"path": "zalozcovia",
"query": {
"bool": {
"filter": [
{
"match": {
"zalozcovia.meno": "\u013dubo\u0161"
}
},
{
"match": {
"zalozcovia.priezvisko": "Majgot"
}
},
{
"match": {
"zalozcovia.mesto": "Trnava"
}
}
]
}
}
}
}
]
}
}
},
"size": 20,
"sort": [
{
"rok": "desc"
},
{
"cislo": "desc"
}
]
}
Thanks.
In ES6 there is afaik no "OR" Query (https://www.elastic.co/guide/en/elasticsearch/reference/6.4/query-dsl-or-query.html). You should use a bool query and use there the "should" Part (https://www.elastic.co/guide/en/elasticsearch/reference/6.4/query-dsl-bool-query.html).
{
"query": {
"bool": {
"filter": [{
"bool": {
"should": [{
"nested": {
"path": "zalozcovia",
"query": {
"bool": {
"filter": [{
"match": {
"zalozcovia.meno": "\u013dubo\u0161"
}
},
{
"match": {
"zalozcovia.priezvisko": "Majgot"
}
},
{
"match": {
"zalozcovia.mesto": "Trnava"
}
}
]
}
}
}
}]
}
}]
}
},
"size": 20,
"sort": [{
"rok": "desc"
},
{
"cislo": "desc"
}
]
}
Try changing "filter-or" with should
{
"query": {
"bool": {
"should" : [
{
"nested": {
"path": "zalozcovia",
"query": {
"bool": {
"filter": [
{
"match": {
"zalozcovia.meno": "\u013dubo\u0161"
}
},
{
"match": {
"zalozcovia.priezvisko": "Majgot"
}
},
{
"match": {
"zalozcovia.mesto": "Trnava"
}
}
]
}
}
}
}
]
}
},
"size": 20,
"sort": [
{
"rok": "desc"
},
{
"cislo": "desc"
}
]
}

Elasticsearch gives [function_score] malformed query, expected [END_OBJECT] but found [FIELD_NAME]

I have a query as follows to use function_score but it gives an error that the query is malformed and [END_OBJECT] is expected but found [FIELD_NAME]. I have checked the documentation and couldn't find what is incorrect or inconsistent with this. I haven't added any script in script_score here but even when I tried with adding script it gave the same problem.
{
"from": 0,
"query": {
"function_score": {
"query": {
"bool": {
"filter": [
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"match": {
"address.area.area.raw": "Durbarmarg"
}
},
{
"match": {
"address.area.city.raw": "Kathmandu"
}
},
{
"match": {
"address.area.district.raw": "Kathmandu"
}
},
{
"match": {
"address.area.state.raw": "State-3"
}
},
{
"match": {
"address.area.country.raw": "Nepal"
}
}
]
}
},
{
"nested": {
"inner_hits": {},
"path": "branchAddress",
"query": {
"bool": {
"must": [
{
"match": {
"branchAddress.area.area.raw": "Durbarmarg"
}
},
{
"match": {
"branchAddress.area.city.raw": "Kathmandu"
}
},
{
"match": {
"branchAddress.area.district.raw": "Kathmandu"
}
},
{
"match": {
"branchAddress.area.state.raw": "State-3"
}
},
{
"match": {
"branchAddress.area.country.raw": "Nepal"
}
}
]
}
}
}
}
]
}
}
],
"must": [
{
"term": {
"sub_categories.raw": "Restaurant"
}
}
],
"must_not": [],
"should": []
}
}
},
"script_score": {}
},
"size": 10
}
The error is as bellow:
raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.RequestError: TransportError(400, 'parsing_exception', '[function_score] malformed query, expected [END_OBJECT] but found [FIELD_NAME]')
The script_score section is not at the right place, it needs to be a sibling of the inner query:
{
"from": 0,
"query": {
"function_score": {
"query": {
...
},
"script_score": {} <--- script_score goes here
}
},
"size": 10
}

Querying a field for empty value

I'm trying to query for an empty field. In SQL parlance, I would do the following:
select * from cars where cartype="Auto" and make="Ferrari" and color="" and model="" and seats in ["", "bench"]
I'm doing the following, but I'm getting an error
{
"query": {
"bool": {
"must": [
{
"match": {
"CarType": "Auto"
}
},
{
"match": {
"Make": "Ferrari"
}
}
],
"must_not": [
{
"existence": true,
"field": "Color",
"null_value": true
},
{
"existence": true,
"field": "Model",
"null_value": true
}
]
}
}
}
But I get the following: "Parse Failure [Failed to parse source .... nexted: query malformed"
I'm following the example here
try like this
{
"query": {
"bool": {
"must": [
{
"match": {
"CarType": "Auto"
}
}
,
{
"match": {
"Make": "Ferrari"
}
}
], "must_not": [
{
"missing":{
"field":"Model",
"existence":true,
"null_value":true
}
},
{
"missing":{
"field":"Color",
"existence":true,
"null_value":true
}
}
]
}
}
}

ElasticSearch ignoring sort when filtered

ElasticSearch Version: 0.90.1, JVM: 1.6.0_51(20.51-b01-457)
I'm trying to do two things with my ElasticSearch query: 1) filter the results based on a boolean (searchable) and "open_date < tomorrow" and 2) two sort by the field "open_date" DESC
This produces the following query:
{
"query": {
"bool": {
"should": [
{
"prefix": {
"name": "foobar"
}
},
{
"query_string": {
"query": "foobar"
}
},
{
"match": {
"name": {
"query": "foobar"
}
}
}
],
"minimum_number_should_match": 1
},
"filtered": {
"filter": {
"and": [
{
"term": {
"searchable": true
}
},
{
"range": {
"open_date": {
"lt": "2013-07-16"
}
}
}
]
}
}
},
"sort": [
{
"open_date": "desc"
}
]
}
However, the results that come back are not being sorted by "open_date". If I remove the filter:
{
"query": {
"bool": {
"should": [
{
"prefix": {
"name": "foobar"
}
},
{
"query_string": {
"query": "foobar"
}
},
{
"match": {
"name": {
"query": "foobar"
}
}
}
],
"minimum_number_should_match": 1
}
},
"sort": [
{
"open_date": "desc"
}
]
}
... the results come back as expected.
Any ideas?
I'm not sure about the Tire code, but the JSON does not correctly construct a filtered query. My guess is that this overflows and causes the sort element to also not be correctly parsed.
A filtered query should be constructed like this (see http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query/ ):
{
"query": {
"filtered": { // Note: this contains both query and filter
"query": {
"bool": {
"should": [
{
"prefix": {
"name": "foobar"
}
},
{
"query_string": {
"query": "foobar"
}
},
{
"match": {
"name": {
"query": "foobar"
}
}
}
],
"minimum_number_should_match": 1
}
},
"filter": {
"and": [
{
"term": {
"searchable": true
}
},
{
"range": {
"open_date": {
"lt": "2013-07-16"
}
}
}
]
}
}
},
"sort": [
{
"open_date": "desc"
}
]
}
Cheers,
Boaz

Resources