ElasticSearch PHP SDK搜索在match_all查询上返回null

时间:2016-09-25 19:09:38

标签: php elasticsearch silex

我最近尝试使用ES。所以我在云端环境中进行了设置。我使用curl请求文件插入数据,我可以用

看到它们
http://mydomain/ingredients/aliments/_search?size=350&pretty=true

然后我尝试使用Silex设置弹性SDK(v.2.0),但我无法获得相同的输出... 这是我的代码:

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

输出为NULL但是当我执行以下操作时:

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

输出符合预期:{"count":240,"_shards":{"total":5,"successful":5,"failed":0}}

我已经花了几个小时试图计算发生了什么,我试图用json字符串替换'query'args,我尝试了空数组而不是新的stdClass但似乎没有任何效果。

编辑:我再次阅读文档并尝试了官方示例:

$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****/
    ...
}

不幸的是得到了相同的空结果......

我做错了什么?

感谢阅读。

2 个答案:

答案 0 :(得分:0)

我发现插入的数据格式不正确。 通过浏览器URL访问一些格式错误的数据似乎没问题,但没有使用curl命令行或SDK。

而不是{name:"Yaourt",type:"",description:""},我在我的请求文件中写了{"name":"Yaourt","description":""},现在一切都按预期工作了!

答案 1 :(得分:0)

就我而言,它是这样工作的:

 $json = '{
     "query": {
         "match_all": {}
     }
 }';

 $params = [
     'type' => 'my_type',
     'body'=> $json
 ];