elastic / elasticsearch-php

Official PHP client for Elasticsearch.
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html
MIT License
5.27k stars 965 forks source link

msearch is not working #1348

Closed murilolivorato closed 12 months ago

murilolivorato commented 1 year ago

Hello , I am using

like this doc - https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html

well it is working in kibana . but when I try to do it , in php . does not work .

this is my test code -

  $this->elasticSeacrh->msearch([
                                                [ 'index' => 'index-test-one'],
                                                [ 'body'   => [
                                                    'query' => [
                                                        'match_all' => new \stdClass()
                                                    ]
                                                ]],
                                                [ 'index' => 'index-test-two'],
                                                [  'body'   => [
                                                    'query' => [
                                                        'match_all' => new \stdClass()
                                                    ]
                                                ]]
                                            ]);

it gives his error -

{"message":"The parameter body is required","errors":{"message":["The parameter body is required"]}}

I Tryed like this -


$response =  $this->elasticSeacrh->msearch([
            'body' => [
                [ 'index' =>  'index-test-one'],
                ["query" => [
                    'match_all' => new \stdClass()
                ]],

                [ 'index' =>  'index-test-two'],
                ["query" => [
                    'match_all' => new \stdClass()
                ]],
            ]
        ]);

and it gives me always empty results.

Elastic\Elasticsearch\Response\Elasticsearch Object
(
    [response:protected] => GuzzleHttp\Psr7\Response Object
        (
            [reasonPhrase:GuzzleHttp\Psr7\Response:private] => OK
            [statusCode:GuzzleHttp\Psr7\Response:private] => 200
            [headers:GuzzleHttp\Psr7\Response:private] => Array
                (
                    [X-elastic-product] => Array
                        (
                            [0] => Elasticsearch
                        )

                    [content-type] => Array
                        (
                            [0] => application/vnd.elasticsearch+json;compatible-with=8
                        )

                    [content-length] => Array
                        (
                            [0] => 5810
                        )

                )

            [headerNames:GuzzleHttp\Psr7\Response:private] => Array
                (
                    [x-elastic-product] => X-elastic-product
                    [content-type] => content-type
                    [content-length] => content-length
                )

            [protocol:GuzzleHttp\Psr7\Response:private] => 1.1
            [stream:GuzzleHttp\Psr7\Response:private] => GuzzleHttp\Psr7\Stream Object
                (
                    [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #19
                    [size:GuzzleHttp\Psr7\Stream:private] => 
                    [seekable:GuzzleHttp\Psr7\Stream:private] => 1
                    [readable:GuzzleHttp\Psr7\Stream:private] => 1
                    [writable:GuzzleHttp\Psr7\Stream:private] => 1
                    [uri:GuzzleHttp\Psr7\Stream:private] => php://temp
                    [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array
                        (
                        )

                )

        )

)

this works -

USING SEARCH -

   $response =   $elasticSeacrh->search(['index' => 'index-test-one',
            'size'   => 4,             // how many results *per shard* you want back
            'body'   => [
                'query' => [
                    'match_all' => new \stdClass()
                ]
            ]
        ]);
        dd($response['hits']);

RESULT

^ array:3 [▼
  "total" => array:2 [▶]
  "max_score" => 1.0
  "hits" => array:4 [▼
    0 => array:4 [▶]
    1 => array:4 [▶]
    2 => array:4 [▶]
    3 => array:4 [▶]
  ]
]

BUT using MSEARCH , does not work , always bringing empty result -

USING MSEARCH -

 $response = $elasticSeacrh->msearch(['body'  => [
            ['index' => 'metric-property-broker-daily-2023-06'],
            ['size'   => 4,  'query' => ['match_all' => new \stdClass() ]]
        ]]);

        dd($response['hits']);

RESULT

Undefined array key "hits"
ezimuel commented 1 year ago

@murilolivorato unfortunately, the usage of the msearch API is not so obvious. You need to use a structure like as follows:

$params = [
    'index' => 'index-test-one',
    'body' => [
        [],
        [ 'query' => [ 'match_all' => new stdClass ] ],
        [ 'index' => 'index-test-two' ],
        [ 'query' => [ 'match_all' => new stdClass ] ]
    ]
];
$response = $elasticSearch->msearch($params);

As you can see the empty value [] is required as first element in the body array. This is because the HTTP request is application/x-ndjson, basically an array of JSON. Let me know if this works for you, thanks!