Ive implemented ElasticSearch in our organization application and all went well so far, but when I tried to add conditions to the query it started throw 400s on almost everything I try following their documentation.
My struggle is to query for only products with status ‘active’.
Here is a code snippet.
$products = $this->elasticsearch->search([
'index' => $product->getSearchIndex(),
'type' => $product->getSearchType(),
'body' => [
'_source' => ['category_id', 'title', 'status'],
'size' => 30,
'query' => [
'bool' => [
'must' => [
'multi_match' => [ // ^1 means that title field has a higher priority in search tree
'fields' => ['title^1'],
'type' => 'phrase_prefix',
'query' => $query,
],
'bool' => [
'must' => [
[
'term' => ['status' => ProductStatus::ACTIVE]
],
],
],
],
],
],
],
]);
Here is the full exception text I get from elastic using this piece of code above.
400 Bad Request: {"error":{"root_cause":[{"type":"parsing_exception","reason":"[multi_match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":170}],"type":"x_content_parse_exception","reason":"[1:170] [bool] failed to parse field [must]","caused_by":{"type":"parsing_exception","reason":"[multi_match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":170}},"status":400}
I tried using filters and so forth, but none of the instructions in docs kind of lead me to success.
What am I missing?