Search code examples
phpelasticsearchsilex

ElasticSearch PHP SDK search returning null on match_all query


I recently tried to use ES. So I set it up in a cloud 9 environnement. I inserted data using a curl request file and I can see them with

http://mydomain/ingredients/aliments/_search?size=350&pretty=true

I then tried to set up elastic SDK (v.2.0) with Silex but I can't get the same output... Here is my code :

$client = $app['elasticsearch'];
$params = array(
    'size' => 350,
    'index' => 'ingredients',
    'type'=>'aliment',
    'body' => array(
        'query'=>array(
            'match_all' => new \stdClass()
        )
    )
);
$ingredients = $client->search($params);

The output is NULL but when I do the following :

$params = array(
    'index' => 'ingredients',
    'type' => 'aliment'
);
$count = $client->count($params);

The output is as expected : {"count":240,"_shards":{"total":5,"successful":5,"failed":0}}

I've already spent a few hours trying to figure what's going on, I tried to replace the 'query' args with a json string, I tried empty array instead of the new stdClass but nothing seems to work.

Edit : I read the documentation again and tried the official example :

$client = $app['elasticsearch'];
$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" => "ingredients",
    "body" => [
        "query" => [
            "match_all" => []
        ]
    ]
];
$output = $client->search($params);
$scroll_id = $output['_scroll_id'];   /*<<<This works****/

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
        ]
    );
    var_dump($response); /*<<<THIS IS NULL****/
    ...
}

And unfortunately got same null result...

What am I doing wrong ?

Thanks for reading.


Solution

  • I found out that the inserted data was malformed. Accessing some malformed data via browser URL seems to be OK but not with a curl command line or the SDK.

    Instead of {name:"Yaourt",type:"",description:""} , I wrote {"name":"Yaourt","description":""} in my requests file and now everything work as expected !