我正在运行一个每X分钟运行一次的cronjob,以找出最受欢迎的标签是什么(标签)。我将用户活动存储在 Elasticsearch 中 Fluentd ,因此请说用户搜索一个术语,该术语将存储在elasticsearch中,如下所示:
{ activity: "search", user: X, searchTerm: "XYZ"}
我为 标签 开了一个活动。每次用户搜索代码时,都会将其作为 活动 插入。通过这种方式,我可以找出用户喜欢的标签,以及找出最受欢迎的标签。
这是我ES的结构:
{
"_index":"user_activity",
"_type":"user_activity",
"_id":"AVcokbsXKR86Bn8FzoFU",
"_score":1.0,
"_source":{
"user":{
"userId":4,
"name":"Another one",
},
"activity":"CREATE",
"date":1473854418419,
"article":{
{user who posted article...}
... Article data (title, etc...)
// Tags associated to this article
"tags":[
{
"tagId":23,
"tagName":"randomTagName"
}
],
},
}
},
{
"_index":"user_activity",
"_type":"user_activity",
"_id":"AVcomLEnKR86Bn8FzoFu",
"_score":1.0,
"_source":{
"user":{
"userId":1,
"name":"MEATTTT DAMOENNNN",
},
"activity":"TAG_SEARCH",
"date":1473854873951,
"tag":"photos"
}
}
... There can be different types of activities
正如您所看到的,有不同的活动类型。我试图查询ES并让它返回每个唯一标记的搜索总量。如果您查看 TAG_SEARCH 对象,则可以看到它有一个字段标记。我试图找到唯一标记值的 TAG_SEARCH 活动的总量!
以下是我尝试使用NativeSearchQueryBuilder
。
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.withSearchType(COUNT)
.withIndices("user_activity").withTypes("user_activity")
.addAggregation(terms("activity").field("activity"))
这样做会返回每个唯一活动值的文档总数。因此上述JSON的结果将是:
"buckets" : [ {
"key" : "create",
"doc_count" : 1
}, {
"key" : "tag_search",
"doc_count" : 1
}
]
但我想要得到的是:
"buckets" : [ {
"key" : "tag",
"value": "TagNameGoesHere",
"doc_count" : 4
}, {
"key" : "tag",
"value": "AnotherTagNameGoesHere",
"doc_count" : 10
}
]
如何找出活动:TAG_SEARCH 的唯一标记:X 值的总数?
我希望SearchQuery看起来像这样:
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.withSearchType(COUNT)
.withIndices("user_activity").withTypes("user_activity")
.addAggregation(terms("activity").value("TAG_SEARCH"))
.addAggregation(terms("tag")) // Count no docs per tag
答案 0 :(得分:3)
您只需要在activity
字段上查询,然后在tag
字段上进行汇总。
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchQuery("activity", "TAG_SEARCH"))
.withSearchType(COUNT)
.withIndices("user_activity")
.withTypes("user_activity")
.addAggregation(AggregationBuilders.terms("tags").field("tag"));
相应的原始JSON查询将是这个
POST user_activity/user_activity/_search?search_type=count
{
"query": {
"match": {
"activity": "TAG_SEARCH"
}
},
"aggs": {
"tags": {
"terms": {
"field": "tag"
}
}
}
}