我最近尝试使用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字符串替换"查询"参数,我尝试空数组而不是新的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****/
...
}
不幸的是得到了相同的null结果…
我做错了什么?
在我的例子中是这样的:
$json = '{
"query": {
"match_all": {}
}
}';
$params = [
'type' => 'my_type',
'body'=> $json
];
我发现插入的数据是错误的。通过浏览器URL访问一些格式错误的数据似乎是可以的,但不使用curl命令行或SDK。
我在我的请求文件中写了{"name":"Yaourt","description":""}
而不是{name:"Yaourt",type:"",description:""}
,现在一切都如预期的那样工作了!
@ivanesi的回答有效。你也可以试试这个:
$params["index"] = $indexName;
$params["body"]["query"]["match_all"] = new stdClass();