我使用 Elasticsearch 来存储点击流量,每一行都包含已访问页面的主题。典型的行如下所示:
{
"date": "2017-09-10T12:26:53.998Z",
"pageid": "10263779",
"loc_ll": [
-73.6487,
45.4671
],
"ua_type": "Computer",
"topics": [
"Trains",
"Planes",
"Electric Cars"
]
}
Run Code Online (Sandbox Code Playgroud)
我希望每个topics都是一个关键字,所以如果我搜索cars什么都不会返回。只会Electric Cars返回一个结果。
我还想对所有行中的所有主题运行一个不同的查询,因此我有一个使用的所有主题的列表。
在 a 上执行此操作pageid将如下所示,但我不确定如何为topics数组处理此问题。
{
"aggs": {
"ids": {
"terms": {
"field": pageid,
"size": 10
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您查询和获取可用术语的方法看起来不错。可能你应该检查你的映射。如果你得到cars这个结果,因为你的映射topics是一个分析过的字符串(例如 typetext而不是keyword)。因此,请检查您对该字段的映射。
PUT keywordarray
{
"mappings": {
"item": {
"properties": {
"id": {
"type": "integer"
},
"topics": {
"type": "keyword"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有了这个样本数据
POST keywordarray/item
{
"id": 123,
"topics": [
"first topic", "second topic", "another"
]
}
Run Code Online (Sandbox Code Playgroud)
和这个聚合:
GET keywordarray/item/_search
{
"size": 0,
"aggs": {
"topics": {
"terms": {
"field": "topics"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
将导致:
"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
}
]
}
}
Run Code Online (Sandbox Code Playgroud)