在elasticsearch中搜索确切的值会返回许多结果

时间:2017-08-31 08:58:59

标签: php elasticsearch

我正在使用官方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个结果都具有field domain => 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"
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题是您的domain字段是分析后的字符串,应该不进行分析。您需要删除索引并使用以下映射重新创建它:

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

然后,您需要重新索引数据并像这样查询它,它将起作用:

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