我使用 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/