【问题标题】:Combine two queries in one request在一个请求中合并两个查询
【发布时间】:2018-01-28 23:44:24
【问题描述】:

我想用一个请求在 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 不支持多搜索,所以我必须使用 Guzzle 来发送请求。

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

我已经有以下正文,但 elastcisearch 正在返回错误请求错误

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

【问题讨论】:

    标签: php elasticsearch


    【解决方案1】:

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

    Multi-search API

    【讨论】:

    • multisearch 如果适合我并满足我的需求。确实 php-client 不支持 msearch 所以我使用 guzzle 来发送请求。但我必须解决一些关于如何发送正确正文的问题。请看我的更新
    【解决方案2】:

    虽然没有记录,但 elasticsearch 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      相关资源
      最近更新 更多