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

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
}

Related

Elasticsearch query for getting records with null values in a field

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
}

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
}
}
]
}
}

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

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"
}
]
}

Issue with elastic search when using nested query

I have following query:
{
"from": 0,
"size": 20,
"sort": {
"prices_count": "desc"
},
"query": {
"bool": {
"must": [{
"terms": {
"category_ids": ["3"]
}
}, {
"terms": {
"manufacturer_id": ["5"]
}
}, {
"range": {
"prices_count": {
"gte": 1
}
}
}]
},
"nested": {
"bool": {
"must": [{
"match": {
"specs.model": "iphone-6s"
}
}]
}
}
}
}
I get the following error:
Fatal error: Uncaught exception 'Elastica\Exception\ResponseException' with message 'failed to parse search source. expected field name but got [START_OBJECT]' in
If i leave just the nested part in the query, it works as expected.
Is it not possible to have and 'ordinary' and nested query in the same request, or am I just doing it wrong?
You need to write it like this:
{
"from": 0,
"size": 20,
"sort": {
"prices_count": "desc"
},
"query": {
"bool": {
"must": [
{
"terms": {
"category_ids": [
"3"
]
}
},
{
"terms": {
"manufacturer_id": [
"5"
]
}
},
{
"range": {
"prices_count": {
"gte": 1
}
}
},
{
"nested": {
"path": "specs",
"query": {
"match": {
"specs.model": "iphone-6s"
}
}
}
}
]
}
}
}

Resources