2016-12-13 48 views
3

以下是我在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); 

但是,我無法理解如何爲'顏色'字段也包含條件。所以,我可以讓所有'三星'手機只有'黑'色。

+0

首先您需要確保您的'spec'字段是['nested' type](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html)。是這樣嗎? – Val

+0

這是我在索引映射時完成的。 – mi6crazyheart

回答

1

由於您的spec字段被正確地映射爲nested場,你只需要修改你這樣的查詢(即移動內部的bool/must一個nested查詢,然後再創建一個用於顏色約束):

$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' 
               ] 
              ] 
             ] 
            ] 
           ] 
          ] 
          ], 
          [ 
          'nested' => [ 
           'path' => "spec", 
           "score_mode" => "max", 
           'query' => [ 
            'bool' => [ 
             'must' => [ 
              [ 
               'match' => [ 
                "spec.id" => 201 
               ] 
              ], 
              [ 
               'match' => [ 
                "spec.value" => 'black' 
               ] 
              ] 
             ] 
            ] 
           ] 
          ] 
          ], 
         ] 
        ] 
       ] 
      ] 
     ]; 

$result = $client->search($params); 
+0

哇!它正在工作。再次感謝幫助我。現在我可以從這裏拿走它。 – mi6crazyheart

+0

很高興幫助! – Val