首页 > 解决方案 > PHP Elasticsearch 批量索引问题:““0”不是有效参数。允许的参数是...”

问题描述

我有一个与 Elasticsearch 6.x 交互的 API 实现,所有索引任务都可以完美运行。现在我正在尝试使用批量 api 索引文档,但在过去 5 天 xD 中没有成功。

我遵循了文档,我的 $params 是这样的:

[
    "body" => [
        0 => [
            "index" => [
                "_index" => "test_schema",
                "_type" => "_doc",
                "_id" => "1"
            ]
        ],
        1 => [
            "field" => "value"
        ],
        2 => [
            "index" => [
                "_index" => "test_schema",
                "_type" => "_doc",
                "_id" => "2"
            ]
        ],
        3 => [
            "field" => "value"
        ]
    ]
]

但总是返回这个异常:

PHPUnit 7.3.5 by Sebastian Bergmann and contributors.

[
    "message" => ""0" is not a valid parameter. Allowed parameters are "_source", "_source_exclude", "_source_include", "client", "consistency", "custom", "fields", "filter_path", "human", "pipeline", "pipeline", "refresh", "replication", "type"",
    "exception" => "Elasticsearch\Common\Exceptions\UnexpectedValueException",
    "file" => "/home/vagrant/code/my-project/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Endpoints/AbstractEndpoint.php",
    "line" => 237,
    "trace" => [
        0 => [
            "file" => "/home/vagrant/code/my-project/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Endpoints/AbstractEndpoint.php",
            "line" => 74,
            "function" => "checkUserParams",
            "class" => "Elasticsearch\Endpoints\AbstractEndpoint",
            "type" => "->",
        ],
        1 => [
            "file" => "/home/vagrant/code/my-project/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Client.php",
            "line" => 747,
            "function" => "setParams",
            "class" => "Elasticsearch\Endpoints\AbstractEndpoint",
            "type" => "->",
        ],
        2 => [
            "function" => "bulk",
            "class" => "Elasticsearch\Client",
            "type" => "->",
        ],
        3 => [
            "file" => "/home/vagrant/code/my-project/app/Contracts/SearchProvider/Client/ClientAbstract.php",
            "line" => 45,
            "function" => "call_user_func",
        ],
        4 => [
            "file" => "/home/vagrant/code/my-project/app/Services/Elasticsearch/Document/DocumentHandler.php",
            "line" => 131,
            "function" => "__call",
            "class" => "App\Contracts\SearchProvider\Client\ClientAbstract",
            "type" => "->",
        ],
        5 => [
            "file" => "/home/vagrant/code/my-project/app/Http/Controllers/DocumentController.php",
            "line" => 90,
            "function" => "create",
            "class" => "App\Services\Elasticsearch\Document\DocumentHandler",
            "type" => "->",
        ],
        6 => [
            "function" => "App\Http\Controllers\{closure}",
            "class" => "App\Http\Controllers\DocumentController",
            "type" => "->",
        ],
        7 => [
            "file" => "/home/vagrant/code/my-project/app/Http/Controllers/DocumentController.php",
            "line" => 91,
            "function" => "array_map",
        ],
        8 => [
            "function" => "store",
            "class" => "App\Http\Controllers\DocumentController",
            "type" => "->",
        ],
        9 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Controller.php",
            "line" => 54,
            "function" => "call_user_func_array",
        ],
        10 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
            "line" => 45,
            "function" => "callAction",
            "class" => "Illuminate\Routing\Controller",
            "type" => "->",
        ],
        11 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
            "line" => 212,
            "function" => "dispatch",
            "class" => "Illuminate\Routing\ControllerDispatcher",
            "type" => "->",
        ],
        12 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
            "line" => 169,
            "function" => "runController",
            "class" => "Illuminate\Routing\Route",
            "type" => "->",
        ],
        13 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line" => 679,
            "function" => "run",
            "class" => "Illuminate\Routing\Route",
            "type" => "->",
        ],
        14 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line" => 30,
            "function" => "Illuminate\Routing\{closure}",
            "class" => "Illuminate\Routing\Router",
            "type" => "->",
        ],
        15 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php",
            "line" => 41,
            "function" => "Illuminate\Routing\{closure}",
            "class" => "Illuminate\Routing\Pipeline",
            "type" => "->",
        ],
        16 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line" => 151,
            "function" => "handle",
            "class" => "Illuminate\Routing\Middleware\SubstituteBindings",
            "type" => "->",
        ],
        17 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line" => 53,
            "function" => "Illuminate\Pipeline\{closure}",
            "class" => "Illuminate\Pipeline\Pipeline",
            "type" => "->",
        ],
        18 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php",
            "line" => 58,
            "function" => "Illuminate\Routing\{closure}",
            "class" => "Illuminate\Routing\Pipeline",
            "type" => "->",
        ],
        19 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line" => 151,
            "function" => "handle",
            "class" => "Illuminate\Routing\Middleware\ThrottleRequests",
            "type" => "->",
        ],
        20 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line" => 53,
            "function" => "Illuminate\Pipeline\{closure}",
            "class" => "Illuminate\Pipeline\Pipeline",
            "type" => "->",
        ],
        21 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line" => 104,
            "function" => "Illuminate\Routing\{closure}",
            "class" => "Illuminate\Routing\Pipeline",
            "type" => "->",
        ],
        22 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line" => 681,
            "function" => "then",
            "class" => "Illuminate\Pipeline\Pipeline",
            "type" => "->",
        ],
        23 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line" => 656,
            "function" => "runRouteWithinStack",
            "class" => "Illuminate\Routing\Router",
            "type" => "->",
        ],
        24 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line" => 622,
            "function" => "runRoute",
            "class" => "Illuminate\Routing\Router",
            "type" => "->",
        ],
        25 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line" => 611,
            "function" => "dispatchToRoute",
            "class" => "Illuminate\Routing\Router",
            "type" => "->",
        ],
        26 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line" => 176,
            "function" => "dispatch",
            "class" => "Illuminate\Routing\Router",
            "type" => "->",
        ],
        27 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line" => 30,
            "function" => "Illuminate\Foundation\Http\{closure}",
            "class" => "Illuminate\Foundation\Http\Kernel",
            "type" => "->",
        ],
        28 => [
            "file" => "/home/vagrant/code/my-project/app/Http/Middleware/SearchProviderConnect.php",
            "line" => 27,
            "function" => "Illuminate\Routing\{closure}",
            "class" => "Illuminate\Routing\Pipeline",
            "type" => "->",
        ],
        29 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line" => 151,
            "function" => "handle",
            "class" => "App\Http\Middleware\SearchProviderConnect",
            "type" => "->",
        ],
        30 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line" => 53,
            "function" => "Illuminate\Pipeline\{closure}",
            "class" => "Illuminate\Pipeline\Pipeline",
            "type" => "->",
        ],
        31 => [
            "file" => "/home/vagrant/code/my-project/vendor/spatie/laravel-cors/src/Cors.php",
            "line" => 28,
            "function" => "Illuminate\Routing\{closure}",
            "class" => "Illuminate\Routing\Pipeline",
            "type" => "->",
        ],
        32 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line" => 151,
            "function" => "handle",
            "class" => "Spatie\Cors\Cors",
            "type" => "->",
        ],
        33 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line" => 53,
            "function" => "Illuminate\Pipeline\{closure}",
            "class" => "Illuminate\Pipeline\Pipeline",
            "type" => "->",
        ],
        34 => [
            "file" => "/home/vagrant/code/my-project/vendor/fideloper/proxy/src/TrustProxies.php",
            "line" => 57,
            "function" => "Illuminate\Routing\{closure}",
            "class" => "Illuminate\Routing\Pipeline",
            "type" => "->",
        ],
        35 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line" => 151,
            "function" => "handle",
            "class" => "Fideloper\Proxy\TrustProxies",
            "type" => "->",
        ],
        36 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line" => 53,
            "function" => "Illuminate\Pipeline\{closure}",
            "class" => "Illuminate\Pipeline\Pipeline",
            "type" => "->",
        ],
        37 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line" => 31,
            "function" => "Illuminate\Routing\{closure}",
            "class" => "Illuminate\Routing\Pipeline",
            "type" => "->",
        ],
        38 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line" => 151,
            "function" => "handle",
            "class" => "Illuminate\Foundation\Http\Middleware\TransformsRequest",
            "type" => "->",
        ],
        39 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line" => 53,
            "function" => "Illuminate\Pipeline\{closure}",
            "class" => "Illuminate\Pipeline\Pipeline",
            "type" => "->",
        ],
        40 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line" => 31,
            "function" => "Illuminate\Routing\{closure}",
            "class" => "Illuminate\Routing\Pipeline",
            "type" => "->",
        ],
        41 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line" => 151,
            "function" => "handle",
            "class" => "Illuminate\Foundation\Http\Middleware\TransformsRequest",
            "type" => "->",
        ],
        42 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line" => 53,
            "function" => "Illuminate\Pipeline\{closure}",
            "class" => "Illuminate\Pipeline\Pipeline",
            "type" => "->",
        ],
        43 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
            "line" => 27,
            "function" => "Illuminate\Routing\{closure}",
            "class" => "Illuminate\Routing\Pipeline",
            "type" => "->",
        ],
        44 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line" => 151,
            "function" => "handle",
            "class" => "Illuminate\Foundation\Http\Middleware\ValidatePostSize",
            "type" => "->",
        ],
        45 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line" => 53,
            "function" => "Illuminate\Pipeline\{closure}",
            "class" => "Illuminate\Pipeline\Pipeline",
            "type" => "->",
        ],
        46 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php",
            "line" => 62,
            "function" => "Illuminate\Routing\{closure}",
            "class" => "Illuminate\Routing\Pipeline",
            "type" => "->",
        ],
        47 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line" => 151,
            "function" => "handle",
            "class" => "Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode",
            "type" => "->",
        ],
        48 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line" => 53,
            "function" => "Illuminate\Pipeline\{closure}",
            "class" => "Illuminate\Pipeline\Pipeline",
            "type" => "->",
        ],
        49 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line" => 104,
            "function" => "Illuminate\Routing\{closure}",
            "class" => "Illuminate\Routing\Pipeline",
            "type" => "->",
        ],
        50 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line" => 151,
            "function" => "then",
            "class" => "Illuminate\Pipeline\Pipeline",
            "type" => "->",
        ],
        51 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line" => 116,
            "function" => "sendRequestThroughRouter",
            "class" => "Illuminate\Foundation\Http\Kernel",
            "type" => "->",
        ],
        52 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php",
            "line" => 345,
            "function" => "handle",
            "class" => "Illuminate\Foundation\Http\Kernel",
            "type" => "->",
        ],
        53 => [
            "file" => "/home/vagrant/code/my-project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php",
            "line" => 317,
            "function" => "call",
            "class" => "Illuminate\Foundation\Testing\TestCase",
            "type" => "->",
        ],
        54 => [
            "file" => "/home/vagrant/code/my-project/tests/Feature/Controller/DocumentController.php",
            "line" => 121,
            "function" => "json",
            "class" => "Illuminate\Foundation\Testing\TestCase",
            "type" => "->",
        ],
        55 => [
            "file" => "/home/vagrant/code/my-project/vendor/phpunit/phpunit/src/Framework/TestCase.php",
            "line" => 1150,
            "function" => "storeDocument",
            "class" => "Tests\Feature\SearchProvider\Elasticsearch\DocumentControllerTest",
            "type" => "->",
        ],
        56 => [
            "file" => "/home/vagrant/code/my-project/vendor/phpunit/phpunit/src/Framework/TestCase.php",
            "line" => 844,
            "function" => "runTest",
            "class" => "PHPUnit\Framework\TestCase",
            "type" => "->",
        ],
        57 => [
            "file" => "/home/vagrant/code/my-project/vendor/phpunit/phpunit/src/Framework/TestResult.php",
            "line" => 665,
            "function" => "runBare",
            "class" => "PHPUnit\Framework\TestCase",
            "type" => "->",
        ],
        58 => [
            "file" => "/home/vagrant/code/my-project/vendor/phpunit/phpunit/src/Framework/TestCase.php",
            "line" => 798,
            "function" => "run",
            "class" => "PHPUnit\Framework\TestResult",
            "type" => "->",
        ],
        59 => [
            "file" => "/home/vagrant/code/my-project/vendor/phpunit/phpunit/src/Framework/TestSuite.php",
            "line" => 750,
            "function" => "run",
            "class" => "PHPUnit\Framework\TestCase",
            "type" => "->",
        ],
        60 => [
            "file" => "/home/vagrant/code/my-project/vendor/phpunit/phpunit/src/TextUI/TestRunner.php",
            "line" => 586,
            "function" => "run",
            "class" => "PHPUnit\Framework\TestSuite",
            "type" => "->",
        ],
        61 => [
            "file" => "/home/vagrant/code/my-project/vendor/phpunit/phpunit/src/TextUI/Command.php",
            "line" => 203,
            "function" => "doRun",
            "class" => "PHPUnit\TextUI\TestRunner",
            "type" => "->",
        ],
        62 => [
            "file" => "/home/vagrant/code/my-project/vendor/phpunit/phpunit/src/TextUI/Command.php",
            "line" => 159,
            "function" => "run",
            "class" => "PHPUnit\TextUI\Command",
            "type" => "->",
        ],
        63 => [
            "file" => "/home/vagrant/code/my-project/vendor/phpunit/phpunit/phpunit",
            "line" => 53,
            "function" => "main",
            "class" => "PHPUnit\TextUI\Command",
            "type" => "::",
        ]
    ]
]

我不知道为什么,我查看了 elasticsearch php sdk 代码但没有任何线索。有没有人知道这里发生了什么?

预先感谢您的关注。

更新:

我有一个 ClientAbstract 类,它封装了第三方“客户端”实现(在本例中为 Elasticsearch\ClientBuilde),因此,要调用任何与 Elasticsearch 相关的问题,我必须抽象每个方法。

为了避免这项繁重的工作,我刚刚实现了一个神奇的方法“__call”:

public function __call(string $name, array $arguments)
{
    return call_user_func([$this->client, $name], $arguments);
}

标签: phpelasticsearch

解决方案


该错误与我的“__call”实现有关”,我正在使用“call_user_func”,我应该使用“call_user_func_array”......修复如下:

public function __call(string $name, array $arguments)
{
    return call_user_func_array([$this->client, $name], $arguments);
}

或使用 '...' 运算符:

public function __call(string $name, array $arguments)
{
    return call_user_func([$this->client, $name], ...$arguments);
}

那是因为 "$arguments" 总是一个数组,所以传递给 Elasticsearch 客户端的真实数据是:

[
    0 => [
        "body" => [
            0 => [
                "index" => [
                    "_index" => "test_schema",
                    "_type" => "_doc",
                    "_id" => "1"
                ]
            ],
            1 => [
                "field" => "value"
            ],
            2 => [
                "index" => [
                    "_index" => "test_schema",
                    "_type" => "_doc",
                    "_id" => "2"
                ]
            ],
            3 => [
                "field" => "value"
            ]
        ]
    ]
]

这说明“0”不是有效参数。允许的参数是“_source”、“_source_exclude”、“_source_include”、“client”、“consistency”、“custom”、“fields”、“filter_path”、“human "、"管道"、"管道"、"刷新"、"复制"、"类型""的东西。


推荐阅读