首页 > 解决方案 > Powershell elasticsearch不起作用,缺少scrollId

问题描述

我正在创建一个 PowerShell 脚本来从公共服务中获取大量文件(我对此没有任何所有权)。我需要使用滚动 API 来获取我需要的所有文件,但我遇到了一个问题:在提供滚动 ID 时,Elasticsearch 使用邮递员和 PowerShell 做出不同的响应。

我已经在邮递员中手动重新创建了这些步骤,没有任何问题。

Function scrollBody ($scrollID) {
    $scrollBody = @"
    {
        "scroll": "2m",
        "scroll_id": "$scrollId"
    }
"@
    return $scrollBody
}


    $body = (scrollBody `
        -scrollID $scrollID
    | ConvertFrom-Json)

    $scrollUri = $ServerUri + "/_search/scroll"
    $response = Invoke-WebRequest `
        -Method GET `
        -Uri $scrollUri `
        -ContentType 'application/json' `
        -Body $body `
        | ConvertFrom-Json

我从代码的另一部分获得了工作滚动 ID,但我已经使用邮递员测试了这些 ID,它们工作正常。

我怀疑这是我提供身体的方式,但我遇到了这个错误:

{
    "error": {
        "root_cause": [
            {
                "type": "action_request_validation_exception",
                "reason": "Validation Failed: 1: scrollId is missing;"
            }
        ],
        "type": "action_request_validation_exception",
        "reason": "Validation Failed: 1: scrollId is missing;"
    },
    "status": 400
}

提前致谢

标签: powershellelasticsearch

解决方案


我无法测试,但在调用函数以生成具有 scrollID 的对象时,您似乎遇到了语法错误。

您可以将其添加到请求的哈希中,这样会更容易阅读。反引号在某种程度上是一种反模式。

$serverUri = 'http://distribution.virk.dk/offentliggoerelser/_search/scroll'

$iwrParams = @{
    'Uri'         = $serverUri
    'Method'      = 'GET'
    'ContentType' = 'applications/json'
    'UseBasicParsing' = $true
    'Body' = @{
        'scroll'    = '2m'
        'scroll_id' = $scrollId
    }
}

Invoke-WebRequest @iwrParams

推荐阅读