首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在elasticsearch中搜索精确值将返回许多结果

在elasticsearch中搜索精确值将返回许多结果
EN

Stack Overflow用户
提问于 2017-08-31 16:58:59
回答 1查看 206关注 0票数 0

我对官方的elasticsearch-php客户端使用了以下请求。

代码语言:javascript
运行
复制
  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”的结果(见屏幕截图)。

我也有一个函数,这个请求工作正常,但在一个日期字段。

代码语言:javascript
运行
复制
  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()函数检索的结果比我想要的要多吗?

这是我的索引函数:

代码语言:javascript
运行
复制
/**
 * @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);
}

我的字段映射:

代码语言:javascript
运行
复制
{
    "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"
                    }
                }
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-31 18:01:37

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

代码语言:javascript
运行
复制
                "domain": {
                    "type": "string",
                    "index": "not_analyzed"
                },

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

代码语言:javascript
运行
复制
  'body' => [
    'query' => [
      'term' => [
        'domain' => 'http://veehouder.cono.nl',
      ],
    ],
  ],
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45977403

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档