2017-08-31 90 views
0

我正在使用官方elasticsearch-php客户端的以下请求。在elasticsearch中搜索精确值返回许多结果

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()获取更多的结果的话,我想要什么? enter image description here

这是我的索引功能:

/** 
* @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" 
        } 
       } 
      } 
     } 
    } 
} 
+0

尝试使用'term'而不是'match' – Val

+0

运行'curl -XGET localhost:9200/stats/domain_stats/_mapping'时会得到什么? – Val

+0

嗨瓦尔!我用curl -XGET localhost:9200/stats/domain_stats/_mapping返回的映射更新了我的问题 –

回答

1

的问题是,你的domain字段是一个字符串分析,应该没有分析。您需要删除索引,并与下面的映射重新创建它:

   "domain": { 
        "type": "string", 
        "index": "not_analyzed" 
       }, 

然后,你需要重新索引您的数据和查询像这样,它会工作:

'body' => [ 
    'query' => [ 
     'term' => [ 
     'domain' => 'http://veehouder.cono.nl', 
     ], 
    ], 
    ],