I am trying to run the following query against a local Elasticsearch instance that I know is functioning correctly, however I am receiving a 400 error due to the query apparently being malformed:
{
"size": 10,
"from": 0,
"query": {
"bool": {
"filter": null,
"must": [
{
"bool": null,
"match": null,
"match_phrase": null,
"query_string": {
"default_field": "name",
"query": "Test Name"
},
"range": null
}
],
"must_not": null,
"should": null
},
"match": null,
"match_phrase": null,
"query_string": null,
"range": null
}
}
Note: There are a bunch of null-valued parameters because the query is being automatically generated based on a custom OpenAPI spec - these are parameters that should be ignored by Elasticsearch, I am not trying to find or filter by any null-valued fields. Are there issues with having null-valued parameters in this way?
The specific error message is as follows:
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "[bool] query malformed, no start_object after query name",
"line": 1,
"col":67
}
],
"type": "x_content_parse_exception",
"reason": "[1:67] [bool] failed to parse field [must]",
"caused_by": {
"type": "parsing_exception",
"reason": "[bool] query malformed, no start_object after query name",
"line": 1,
"col": 67
}
},
"status":400
}
Note: The line and column numbers listed in the error won't be accurate since I've formatted the query for readability, the original query has no whitespace other than in the query string.
I've read through the official Elasticsearch API docs trying to figure out what I'm doing wrong, and have found some people online with similar parsing errors where their mistakes were unrelated, and they apparently didn't have issues specifically with null-valued search parameters, so I'm struggling to figure out what's going wrong here. Running the same query without the null-valued parameters seems to work so perhaps I'm wrong in thinking they shouldn't in general be an issue for Elasticsearch to parse/ignore? Having said that, the fact that the error message says about failing to parse the 'must' within the bool query suggests that the "filter": null
line was parsed correctly and therefore null values should be okay...
You should never have null values in your query. You should modify your query generator to never add those as ES will complain when parsing them as you can see.
If you take the query without any single null value, you've seen it work. Now just add "filter": null
and see what happens. Right, it fails!
So just modify your query generator and you'll be fine.
Moreover, you can only have one single constraint between two curly braces. The part below can never have match
, match_phrase
, query_string
and range
being non-null at the same time. Each of those constraint should be in there own section {...}
{
"bool": null,
"match": null,
"match_phrase": null,
"query_string": {
"default_field": "name",
"query": "Test Name"
},
"range": null
}