My indexed elastic search documents contains a field which is an array. For example featuers of a car.
{
"features": [ "Anti-Lock Brakes",
"Alarm",
"Air Bag Driver",
"Cruise Control",
"Satellite Navigation"'
}
How can I search for documents with which includes values from a given array. For example: I want to checkout cars which contain features such as 'Satellite navigation' and 'Cruise control'.
I tried the following but it is not working (php code)
public function index(Request $request, Client $search, SerializerInterface $serializer): JsonResponse
...
$search_query = ["Satellite Navigation", "Cruise Control"]
$filters = [];
$filters[] = [
'terms' => [
'features' => $search_query,
]
];
$results = $search->search([
'index' => 'cars',
'body' => [
'from' => $offset,
'size' => self::PAGE_WINDOW,
'query' => [
"bool" => [
"must" => [
"match_all" => new \stdClass()
],
"filter" => $filters
]
]
]
]);
...
I would like get a hint on how to set the $filters so that I get the desired results.
Since you do not have the mapping for features
and you are adding index data, so a default mapping is generated.
Try this below search query, this will return all the documents that contain features
such as 'Satellite navigation'
and 'Cruise control'
Search Query:
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"features": "Satellite Navigation"
}
},
{
"match_phrase": {
"features": "Cruise Control"
}
}
]
}
}
}
Search Result:
"hits": [
{
"_index": "my-index",
"_type": "_doc",
"_id": "1",
"_score": 1.7178956,
"_source": {
"features": [
"Anti-Lock Brakes",
"Alarm",
"Air Bag Driver",
"Cruise Control",
"Satellite Navigation"
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.