如何使用curl将滚动id发送到elasticsearch

x759pob2  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(436)

我不知道如何使用curl将scroll\u id发送到elasticsearch。
这是我迄今为止尝试过的,但似乎不起作用。

$url = "http://distribution.virk.dk/cvr-permanent/virksomhed/_search?scroll=2m&_scroll_id=".$_POST["scroll_id"];
        $data = array(
            "_scroll_id" => $_POST["scroll_id"],
            "scroll_id" => $_POST["scroll_id"],
            "size" => 10, 
            "_source" => array(
                "Vrvirksomhed.cvrNummer",
                "Vrvirksomhed.elektroniskPost",
                "Vrvirksomhed.livsforloeb",
                "Vrvirksomhed.hjemmeside",
                "Vrvirksomhed.virksomhedMetadata.nyesteNavn.navn",
                "Vrvirksomhed.hovedbranche",
                "Vrvirksomhed.penheder",
                "Vrvirksomhed.telefonnummer",
                "Vrvirksomhed.virksomhedMetadata.nyesteBeliggenhedsadresse"
            ), 
            "query" => array (
                "bool" => array (
                    "must_not" => array (
                        "exists" => array (
                            "field" => "Vrvirksomhed.livsforloeb.periode.gyldigTil"
                        )
                    )
                )
            )
        );

elasticsearch每次都返回相同的10篇文章,因此我认为它没有正确地获取滚动id。
尝试了瓦尔的建议后更新了代码。使用sethosts,很长一段时间后我得到了一个超时。忽略sethosts,我得到的错误是在集群中找不到活动节点。

use Elasticsearch\ClientBuilder;

    require 'vendor/autoload.php';

    $username = "MY_USERNAME";
    $password = "MY_PASSWORD";

    $hosts = [
        'host' => 'distribution.virk.dk',
        'scheme' => 'http',
        'path'  => '/cvr-permanent',
        'port' => '80',
        'user' => $username,
        'pass' => $password
    ];

    $client = ClientBuilder::create()->setHosts($hosts)->build();
    $params = [
    'scroll' => '30s',
    'size'   => 50,
    'type'   => '/cvr-permanent/virksomhed',
    'index'  => 'virksomhed',
    'body'   => [
        'query' => [
            'match_all' => new \stdClass()
        ]
    ]
    ];

    // Execute the search
    // The response will contain the first batch of documents
    // and a scroll_id
    $response = $client->search($params);

    // Now we loop until the scroll "cursors" are exhausted
    while (isset($response['hits']['hits']) && count($response['hits']['hits']) > 0) {

        //**
        // Do your work here, on the $response['hits']['hits'] array
        //**

        // When done, get the new scroll_id
        // You must always refresh your _scroll_id!  It can change sometimes
        $scroll_id = $response['_scroll_id'];

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

643ylb081#

使用scroll api有两个步骤。
在第一步中,您需要发送查询和滚动上下文的持续时间。
在第二步中,您不需要再次发送查询,只需要发送上一次滚动搜索得到的滚动id。
你可以在这里找到一个完整的例子

相关问题