我对官方的elasticsearch-php客户端使用了以下请求。
private function getAllStatisticsByDomain() {
$params = [
'index' => 'stats',
'type' => 'domain_stats',
'size' => 99,
'body' => [
'query' => [
'match' => [
'domain' => 'http://veehouder.cono.nl',
],
],
],
];
$response = $this->getElasticClient()->search($params);
return $response['hits']['hits'];
}前4个结果都有域=> http://veehouder.cono.nl,但它还检索了更多没有值“http://veehouder.cono.nl”的结果(见屏幕截图)。
我也有一个函数,这个请求工作正常,但在一个日期字段。
private function getAllStatisticsByDay() {
$params = [
'index' => 'stats',
'type' => 'domain_stats',
'size' => 99,
'body' => [
'query' => [
'match' => [
'date' => date('Y-m-d'),
],
],
],
];
$response = $this->getElasticClient()->search($params);
return $response['hits']['hits'];
}有人能解释一下为什么getAllStatisticsByDomain()函数检索的结果比我想要的要多吗?

这是我的索引函数:
/**
* @param $id
* @param $domain
* @param $googlePSMobileScore
* @param $googlePSMobileUsabilityScore
* @param $googlePSDesktopScore
* @param $mozPDA
* @param $mozUPA
*/
function insert($id, $domain, $googlePSMobileScore, $googlePSMobileUsabilityScore, $googlePSDesktopScore, $mozPDA, $mozUPA, $date) {
$params = [
'index' => 'stats',
'type' => 'domain_stats',
'id' => $id,
'body' => [
'domain' => $domain,
'googlePSMobileScore' => $googlePSMobileScore,
'googlePSMobileUsabilityScore' => $googlePSMobileUsabilityScore,
'googlePSDesktopScore' => $googlePSDesktopScore,
'mozPDA' => $mozPDA,
'mozUPA' => $mozUPA,
'date' => $date,
],
];
getElasticClient()->index($params);
}我的字段映射:
{
"stats": {
"mappings": {
"domain_stats": {
"properties": {
"date": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"domain": {
"type": "string"
},
"googlePSDesktopScore": {
"type": "long"
},
"googlePSMobileScore": {
"type": "long"
},
"googlePSMobileUsabilityScore": {
"type": "long"
},
"mozPDA": {
"type": "double"
},
"mozUPA": {
"type": "double"
}
}
}
}
}
}发布于 2017-08-31 18:01:37
问题是你的domain字段是一个被分析的字符串,它不应该被分析。您需要删除索引并使用以下映射重新创建它:
"domain": {
"type": "string",
"index": "not_analyzed"
},然后,您需要重新索引您的数据,并像这样查询它,它将会工作:
'body' => [
'query' => [
'term' => [
'domain' => 'http://veehouder.cono.nl',
],
],
],https://stackoverflow.com/questions/45977403
复制相似问题