2017-08-03 84 views
0

當我查詢Elasticsearch產品某一特定製造商,這個工程:與has_child PHP ElasticSearch複合查詢

$params = ['index' => 'products', 
    'type' => 'product', 
    'body' => ['query' => 
     ['match' => ['manufacturers_id' => $query],], 
    ], 
    ]; 

但是,當我也想在條件補充的是,產品自帶銀色的,這是我已經添加爲孩子記錄的產品記錄,我得到一個語法錯誤:

$params = ['index' => 'products', 
    'type' => 'product', 
    'body' => ['query' => 
     ['match' => ['manufacturers_id' => $query],], 
     ['query' => 
      ['has_child' => 
      ['type' => 'attributes', 
      ['query' => 
       ['color' => 'Silver'],], 
      ], 
      ], 
     ], 
    ], 
    ]; 

的錯誤是

{ 
    "error": { 
     "col": 49, 
     "line": 1, 
     "reason": "Unknown key for a START_OBJECT in [0].", 
     "root_cause": [ 
      { 
       "col": 49, 
       "line": 1, 
       "reason": "Unknown key for a START_OBJECT in [0].", 
       "type": "parsing_exception" 
      } 
     ], 
     "type": "parsing_exception" 
    }, 
    "status": 400 
} 

也試過

$params = ['index' => 'products', 
    'type' => 'product', 
    'body' => ["query"=> [ 
        "match"=> [ 
         "manufacturers_id"=> [11] 
        ], 
        "has_child"=> [ 
         "type"=> "attributes", 
         "query"=> [ 
          "match"=> [ 
           "color"=> "silver" 
          ], 
         ], 
        ], 
       ], 
    ], 
    ]; 

我得到「在上一1:39 START_ARRAY無法獲取文本。」

回答

0

終於得到了這個工作。非常感謝@pawle對Sense的建議,這真的有所幫助。

$params = ['index' => 'products', 
    'type' => 'product', 
    'body' => 
     [ 
      "query" => [ 
       "bool" => [ 
       "must" => [[ 
        "has_child" => [ 
         "type" => "attributes", 
         "query" => [ 
          "match" => [ 
          "attributes_value" => "silver" 
          ] 
         ] 
        ] 
       ], 
        [ 
         "match" => [ 
          "manufacturers_id" => 4 
         ] 
        ] 
       ] 
       ] 
      ] 
     ], 
    ]; 
1

試試這個:

"query"=> [ 
    "match"=> [ 
     "manufacturers_id"=> [1,2,3] 
    ], 
    "has_child"=> [ 
     "type"=> "attributes", 
     "query"=> [ 
      "match"=> [ 
       "color"=> "silver" 
      ] 
     ] 
    ] 
] 

我還建議從某種意義上說,它是Chrome瀏覽器這有助於編寫ES查詢插件。 See the screenshot

+0

獲取無法在1:39的START_ARRAY上獲取文本。 –