已解决
我正在尝试使用官方的Elasticsearch 5.0 php包返回结果。我可以通过URL和cURL获得所需的结果。
http://localhost:9200/index/doctype/_search?q=status:available
正如预期的那样,我得到了8次点击和8次点击结果。
curl -XGET 'localhost:9200/index/doctype/_search?pretty' -d'
{
"query" : {
"term" : { "status" : "available" }
}
}'
正如预期的那样,我得到了8次点击和8次点击结果。
使用以下各种代码,我得到8个结果,但0次命中。
$params['body']['query']['term']['status'] = 'available';
$params['q'] = 'status:available';
$params['body'] = [
'query' => [
'bool' => [
'must' => [
['match' => ['status' => 'available']],
]
]
]
];
通过以下方式调用:
$params = [
'index' => 'index',
'type' => 'doctype',
];
$skip = !empty($_GET['page']) ? (int)$_GET['page'] - 1 : 0;
$listings_per_page = 25;
$params['from'] = $skip * $per_page;
$params['size'] = $per_page;
// show only available
$params['body'] = [
'query' => [
'bool' => [
'must' => [
['match' => ['status' => 'available']],
]
]
]
];
$hosts = [env('ES_HOST', 'elasticsearch') . ':' . env('ES_PORT', '9200')];
$client = ClientBuilder::create()->setHosts($hosts)->build();
$es_results = $client->search($params);
我在$ params以及其他代码段中正确设置了索引和doctype。我在这里可能做错了什么?
最佳答案
您的from
和size
参数很可能计算不正确,并且from
可能高于8,因此为什么您看到results.hits.total: 8
但在results.hits.hits
中什么都看不到
确保from
为0,且size
至少为8(默认值为10),您会看到匹配。
关于php - ElasticSearch PHP API按字段搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41379944/