Elasticsearch:聚合数组中的不同值

标签 elasticsearch

我使用 Elasticsearch 来存储点击流量,每一行都包含已访问页面的主题。典型的行如下所示:

{
  "date": "2017-09-10T12:26:53.998Z",
  "pageid": "10263779",
  "loc_ll": [
    -73.6487,
    45.4671
  ],
  "ua_type": "Computer",
  "topics": [
    "Trains",
    "Planes",
    "Electric Cars"
  ]
}

我希望每个 topics 都是一个关键字,这样如果我搜索 cars 就不会返回任何内容。只有 Electric Cars 会返回结果。

我还想对所有行中的所有主题运行不同的查询,这样我就有了所有使用的主题的列表。

pageid 上执行此操作看起来如下所示,但我不确定如何为 topics 数组处理此问题。

{
  "aggs": {
    "ids": {
      "terms": {
        "field": pageid,
        "size": 10
      }
    }
  }
}

最佳答案

您查询和获取可用条款的方法看起来不错。也许你应该检查你的映射。如果您得到 cars 的结果,这看起来是因为您的 topics 映射是经过分析的字符串(例如,键入 text 而不是 keyword)。因此,请检查您对该字段的映射。

PUT keywordarray
{
  "mappings": {
    "item": {
      "properties": {
        "id": {
          "type": "integer"
        },
        "topics": {
          "type": "keyword"
        }
      }
    }
  }
}

有了这个样本数据

POST keywordarray/item
{
  "id": 123,
  "topics": [
    "first topic", "second topic", "another"
  ]
}

和这个聚合:

GET keywordarray/item/_search
{
  "size": 0,
  "aggs": {
    "topics": {
      "terms": {
        "field": "topics"
      }
    }
  }
}

将产生这样的结果:

"aggregations": {
  "topics": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [
      {
        "key": "another",
        "doc_count": 1
      },
      {
        "key": "first topic",
        "doc_count": 1
      },
      {
        "key": "second topic",
        "doc_count": 1
      }
    ]
  }
}

关于Elasticsearch:聚合数组中的不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46141326/

相关文章:

elasticsearch - Kibana:如何基于两个字段进行可视化

java - 如何模拟 Elasticsearch Java 客户端?

python - 使用现有字段作为 _id 使用 elasticsearch dsl python DocType

elasticsearch - Elasticsearch 无法启动-使用Ansible进行设置

elasticsearch - 在 Elasticsearch 中相当于分组并作为列表收集的东西是什么?

Elasticsearch 在 Ubuntu 18.04.2 LTS : does not have a Release file

elasticsearch - Elasticsearch:为什么Java客户端使用不同的查询语法?

elasticsearch - 如何生成巨大的随机数据并填充在K8S集群上运行的Elastic搜索?

mysql - Elastic Search 使用 River-jdbc 与远程 mysql 服务器同步数据

elasticsearch - elasticsearch-无法通过更新API更新密集向量字段