在一个请求中组合两个查询

时间:2017-02-18 16:11:45

标签: php elasticsearch

我想用一个请求在elasticsearch服务器上执行多个查询。具体来说,我有以下查询(在elastcisearch-php-client上)

$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对具有多个相同密钥的数组的限制而无效。

更新

Elastcisearch支持名为Multisearch的功能,正是我正在寻找的功能。问题是php-client不支持multisearch所以我必须使用Guzzle才能发送请求。

Guzzle文档没有关于如何构建正确的请求正文的完整信息。欢迎任何信息

我已经拥有以下机构但弹性网络正在撤回错误的请求错误

    $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]]]]]
    ];

2 个答案:

答案 0 :(得分:1)

您可以使用Elasticsearch的multisearch API。这或多或少会在单个POST请求中将所有查询作为JSON格式附加。我希望PHP客户端支持这一点,否则您可能必须手动执行POST请求。

Multi-search API

答案 1 :(得分:1)

虽然没有记录,但弹性搜索php客户端支持Multi Search API

而不是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