以下是我在elasticsearch實例中編入索引的示例產品(2)數據。從多個條件的Elasticsearch提取數據
[{
"type": "mobile",
"name": "iPhone 7s",
"price": 70000,
"brand_id": "100",
"spec": [{
"id": 200, // brand id
"value": "apple"
}, {
"id": 201, // color id
"value": "black"
}, {
"id": 202, // battery size id
"value": "2200"
}]
}, {
"type": "mobile",
"name": "Samsung Galaxy 5S",
"price": 50000,
"brand_id": "101",
"spec": [{
"id": 200,
"value": "samsung"
}, {
"id": 201,
"value": "silver"
}, {
"id": 202,
"value": "3200"
}]
}]
我想要做的是,獲取所有手機數據,其中「品牌」將是「三星」 &「顏色」等於「銀」。
我使用PHP與Elasticsearch進行通信。這裏是一個示例PHP腳本,它將所有'samsung'手機從彈性返回。
$params = [
'index' => 'index name',
'type' => 'products',
'from' => $start,
'size' => $limit,
'body' => [
'query' => [
'bool' => [
'must' => [
'match' => [
'type' => 'mobile'
]
],
[
'nested' => [
'path' => "spec",
"score_mode" => "max",
'query' => [
'bool' => [
'must' => [
[
'match' => [
"spec.id" => 200
]
],
[
'match' => [
"spec.value" => 'samsung'
]
]
]
]
]
]
]
]
]
]
];
$result = $client->search($params);
但是,我無法理解如何爲'顏色'字段也包含條件。所以,我可以讓所有'三星'手機只有'黑'色。
首先您需要確保您的'spec'字段是['nested' type](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html)。是這樣嗎? – Val
這是我在索引映射時完成的。 – mi6crazyheart