2017-02-18 40 views
1

我想用一個請求在elasticsearch服務器上執行多個查詢。具體來說,我有以下查詢(是elastcisearch-PHP-客戶端)在一個請求中合併兩個查詢

$params = [ 
        "index" => "bookydate", 
        "type" => "vendor_service", 
        "body" => [ 
         "query" => [ 
          "bool" => [ 
           "must" => [ 
            "term" => [ 
             "sys_service_id" => $request->input("sys_service_id") 
            ] 
           ], 
           "should" => [ 
            "geo_shape" => [ 
             "served_location" => [ 
              "shape" => [ 
               "type" => "point", 
               "coordinates" => [ 
                "{$request->input('loc_lon')}", 
                "{$request->input('loc_lat')}"] 
              ] 
             ] 
            ] 
           ] 
          ] 
         ] 
        ] 
       ]; 

我想要做的是還獲取所有具有"hole_country"true的文件。

我已經嘗試過的是向Elasticsearch服務器發出另一個請求,並且array_merge合併了這兩個結果,但由於PHP對具有多個相同鍵的數組的限制而無法工作。

UPDATE

Elastcisearch支持一種稱爲Multisearch功能,正是即時尋找。問題是php-client不支持multisearch,所以我必須使用Guzzle才能發送請求。

Guzzle文檔沒有關於如何構建正確的請求主體的完整信息。任何信息是值得歡迎的

已經我有以下的身體,但elastcisearch是returing壞請求錯誤

$body = [ 
     ["index"=>"bookydate"], 
     ["query"=>["bool"=> ["must"=>[["term"=>["sys_service_id"=>"1"]],["geo_shape"=>["served_location"=>["shape"=>["type"=>"circle","coordinates"=>[25,3],"radius"=>"90km"]]]]]]]], 
     ["index"=>"bookydate"], 
     ["query"=>["bool"=>["must"=>["term"=>["hole_country"=>true]]]]] 
    ]; 

回答

1

可以使用Elasticsearch的multisearch API。這或多或少地將所有查詢作爲JSON格式附加到單個POST請求中。我希望PHP客戶端支持這一點,否則您可能需要手動執行POST請求。

Multi-search API

+0

multisearch如果適合我,適合我的需求。事實上,php-client不支持msearch,因此我使用guzzle來發送請求。但是我必須解決一些關於如何發送正確身體的問題。請參閱我的更新 – dios231

1

雖然它不記錄的Multi Search API由elasticsearch PHP客戶端支持。

而不是search呼叫msearch和組你的查詢是這樣的: $params = [ 'body' => [ ["index" => "bookydate", "type" => "vendor_service"], ["query" => [ "bool" => [ "must" => [ "term" => [ "sys_service_id" => $request - > input("sys_service_id") ] ], "should" => [ "geo_shape" => [ "served_location" => [ "shape" => [ "type" => "point", "coordinates" => [ "{$request->input('loc_lon')}", "{$request->input('loc_lat')}" ] ] ] ] ] ] ]] ];

所以使用更新後的語法是正確的。您必須致電msearch