ElasticSearch PHP SDK 搜索在 match_all 查询中返回空值
ElasticSearch PHP SDK search returning null on match_all query
我最近尝试使用 ES。所以我在 cloud 9 环境中设置它。我使用 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****/
...
}
不幸的是得到了相同的空结果...
我做错了什么?
感谢阅读。
我发现插入的数据格式不正确。
通过浏览器访问一些格式错误的数据 URL 似乎可以,但不能使用 curl 命令行或 SDK。
我在请求文件中写了 {"name":"Yaourt","description":""}
而不是 {name:"Yaourt",type:"",description:""}
,现在一切都按预期工作了!
在我的例子中,它是这样工作的:
$json = '{
"query": {
"match_all": {}
}
}';
$params = [
'type' => 'my_type',
'body'=> $json
];
@ivanesi 的回答有效。你也可以试试这个:
$params["index"] = $indexName;
$params["body"]["query"]["match_all"] = new \stdClass();
我最近尝试使用 ES。所以我在 cloud 9 环境中设置它。我使用 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****/
...
}
不幸的是得到了相同的空结果...
我做错了什么?
感谢阅读。
我发现插入的数据格式不正确。 通过浏览器访问一些格式错误的数据 URL 似乎可以,但不能使用 curl 命令行或 SDK。
我在请求文件中写了 {"name":"Yaourt","description":""}
而不是 {name:"Yaourt",type:"",description:""}
,现在一切都按预期工作了!
在我的例子中,它是这样工作的:
$json = '{
"query": {
"match_all": {}
}
}';
$params = [
'type' => 'my_type',
'body'=> $json
];
@ivanesi 的回答有效。你也可以试试这个:
$params["index"] = $indexName;
$params["body"]["query"]["match_all"] = new \stdClass();