代码之家  ›  专栏  ›  技术社区  ›  user3174311

如何使用PHP API对ElasticSearch结果进行分页

  •  0
  • user3174311  · 技术社区  · 8 年前

    按照文档中的示例,我在我的类型中搜索如下:

        $params = [
            'index' => $this->index,
            'type' => $this->type,
            "scroll" => "30s",
            "size" => 10,
            'body' => $json
        ];
    
        $response = $this->es->search($params);
    

    https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_search_operations.html#_scrolling

    然后医生建议:

    while (isset($response['hits']['hits']) && count($response['hits']['hits']) > 0) {
    $scroll_id = $response['_scroll_id'];
    $response = $client->scroll([
            "scroll_id" => $scroll_id,  //...using our previously obtained _scroll_id
            "scroll" => "30s"           // and the same timeout window
        ]
    );
    }
    

    但不清楚是否必须执行新请求,或者while循环是否要将所有结果存储在一个变量中并将其传递给模板。

    谢谢

    1 回复  |  直到 8 年前
        1
  •  0
  •   cn007b Dheerendra Kulkarni    8 年前

    scroll 参数,您将收到正常有效负载(\u碎片、抓取、命中等)和 _scroll_id hits 你会有 total 找到的文档数,因此您有:每页项目数和项目总数。

    要计算页数,你需要做简单的数学运算。

    要从elasticsearch接收下一批数据,请运行以下命令:

    curl 'localhost:9200/_search/scroll ' -d '{
        "scroll" : "30s", 
        "scroll_id" : "cXVlcnlUaGVuRmV0Y2g7NTsxMzU0MDQzNjY6QnNHMjd0bXlTZ3Ftd1dkblRUd3NQZzsxMDE1NzI4Mjc6ajFzVmtLQUdSaEduRWFRVi1GZE05UTsxMDE1NzI4MjY6ajFzVmtLQUdSaEduRWFRVi1GZE05UTsxMDE1ODAzODc6TURuUG5nbzRUVU9NUUFjSERqM2hIQTsxMDE1ODAzODg6TURuUG5nbzRUVU9NUUFjSERqM2hIQTswOw==" 
    }'
    

    使用scroll,您只能接收下一批,不能跳转到特定页面。

    推荐文章