【问题标题】:How to use pagination with scan search type in elasticsearch?如何在弹性搜索中使用扫描搜索类型的分页?
【发布时间】:2016-04-25 19:55:16
【问题描述】:

我使用这个配置来扫描我的索引

$client = ClientBuilder::create()->build();
$params = [
    "search_type" => "scan",    // use search_type=scan
    "scroll" => "30s",          // how long between scroll requests. should be small!
    "size" => 50,               // how many results *per shard* you want back
    "index" => "my_index",
    "body" => [
        "query" => [
            "match_all" => []
        ]
    ]
];

$docs = $client->search($params);   // Execute the search
$scroll_id = $docs['_scroll_id'];   // The response will contain no results, just a _scroll_id

// Now we loop until the scroll "cursors" are exhausted
while (\true) {

    // Execute a Scroll request
    $response = $client->scroll([
            "scroll_id" => $scroll_id,  //...using our previously obtained _scroll_id
            "scroll" => "30s"           // and the same timeout window
        ]
    );

    // Check to see if we got any search hits from the scroll
    if (count($response['hits']['hits']) > 0) {
        // If yes, Do Work Here

        // Get new scroll_id
        // Must always refresh your _scroll_id!  It can change sometimes
        $scroll_id = $response['_scroll_id'];
    } else {
        // No results, scroll cursor is empty.  You've exported all the data
        break;
    }
}

但我想按 id 排序此扫描并选择从 10.000 到 11.000 的结果(我的索引有 33.000 结果)。怎么办?

我在这个搜索正文中使用了 from 和 size 但它不起作用。

【问题讨论】:

  • 你能解释一下这背后的真实用例吗?你想达到什么目的?

标签: elasticsearch


【解决方案1】:

当使用扫描和滚动时,分页有效,但数据在每一页中重复。所以只能使用 from to

点击链接Here is the data for pagination

它仅检索分页索引中的 10000 条记录。在 10,000 个文档之后,它会显示错误。因此还可以搜索两个日期之间的数据以获得更好的结果。

示例:

$params['body']['query']['filtered']['filter']['bool']['must'][]['range']['meta.datetime']['gte'] = $startdate;
$params['body']['query']['filtered']['filter']['bool']['must'][]['range']['meta.datetime']['lte'] = $enddate;

【讨论】:

    猜你喜欢
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多