如何计算 spring elasticsearch 中某个字段出现的次数?
How can I count the number of times a field occurred with spring elasticsearch?
我是 运行 一个 cronjob,每 X 分钟运行一次以找出最流行的标签(主题标签)。我使用 Fluentd 将用户活动存储在 Elasticsearch 中,所以假设用户搜索一个术语,它将像这样存储在 elasticsearch 中:
{ activity: "search", user: X, searchTerm: "XYZ"}
我有一个 activity 用于 标签。每次用户搜索标签时,它都会作为 activity 插入。这样我就可以找出用户喜欢的标签以及总体上最受欢迎的标签。
这是我的 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
如您所见,有不同的 activity 类型。我正在尝试查询 ES 并让它 return 每个唯一标签的搜索总量。如果您查看 TAG_SEARCH 对象,您可以看到它有一个字段 tag。我正在尝试查找唯一标签值的 TAG_SEARCH 活动总量!
这是我使用 NativeSearchQueryBuilder
.
尝试过的
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.withSearchType(COUNT)
.withIndices("user_activity").withTypes("user_activity")
.addAggregation(terms("activity").field("activity"))
它所做的是 return 每个唯一 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
}
]
如何找出 标签的总数:X 值 activity:TAG_SEARCH?
我希望 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
您只需要对 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"
}
}
}
}
我是 运行 一个 cronjob,每 X 分钟运行一次以找出最流行的标签(主题标签)。我使用 Fluentd 将用户活动存储在 Elasticsearch 中,所以假设用户搜索一个术语,它将像这样存储在 elasticsearch 中:
{ activity: "search", user: X, searchTerm: "XYZ"}
我有一个 activity 用于 标签。每次用户搜索标签时,它都会作为 activity 插入。这样我就可以找出用户喜欢的标签以及总体上最受欢迎的标签。
这是我的 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
如您所见,有不同的 activity 类型。我正在尝试查询 ES 并让它 return 每个唯一标签的搜索总量。如果您查看 TAG_SEARCH 对象,您可以看到它有一个字段 tag。我正在尝试查找唯一标签值的 TAG_SEARCH 活动总量!
这是我使用 NativeSearchQueryBuilder
.
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.withSearchType(COUNT)
.withIndices("user_activity").withTypes("user_activity")
.addAggregation(terms("activity").field("activity"))
它所做的是 return 每个唯一 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
}
]
如何找出 标签的总数:X 值 activity:TAG_SEARCH?
我希望 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
您只需要对 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"
}
}
}
}