Elasticsearch:我想计算特定值在特定字段中出现的次数
Elasticsearch: I want to count how many times a particular value has appeared in particular field
我有一个简单的问题变成了一个难题。我想计算特定值在我的 elasticseach 索引的特定字段中出现了多少次。
假设我有 product.name,我想知道确切的单词 "Shampoo" 在其中出现了多少次。
我知道 terms
聚合提供了所有唯一值及其出现次数。但是,考虑到我有数十亿条记录,这对我不利。我不想通过额外的步骤在 keys
中找到我想要的值。
Defines a single bucket of all the documents in the current document set context that match a specified filter. Often this will be used to narrow down the current aggregation context to a specific set of documents.
可以将类似 value_count 的聚合应用于过滤器聚合中返回的存储桶,以获取文档总数。
我已经将产品类型设为对象,如果它是嵌套数据类型,则需要使用嵌套查询来代替简单的术语查询。
映射
{
"index92" : {
"mappings" : {
"properties" : {
"product" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
}
查询:
{
"size": 0,
"aggs": {
"shampoo": {
"filter": {
"term": {
"product.name.keyword": "shampoo"
}
},"aggs": {
"count": {
"value_count": {
"field": "product.name.keyword"
}
}
}
}
}
}
结果:
"aggregations" : {
"shampoo" : {
"doc_count" : 2,
"count" : {
"value" : 2
}
}
}
我有一个简单的问题变成了一个难题。我想计算特定值在我的 elasticseach 索引的特定字段中出现了多少次。
假设我有 product.name,我想知道确切的单词 "Shampoo" 在其中出现了多少次。
我知道 terms
聚合提供了所有唯一值及其出现次数。但是,考虑到我有数十亿条记录,这对我不利。我不想通过额外的步骤在 keys
中找到我想要的值。
Defines a single bucket of all the documents in the current document set context that match a specified filter. Often this will be used to narrow down the current aggregation context to a specific set of documents.
可以将类似 value_count 的聚合应用于过滤器聚合中返回的存储桶,以获取文档总数。
我已经将产品类型设为对象,如果它是嵌套数据类型,则需要使用嵌套查询来代替简单的术语查询。
映射
{
"index92" : {
"mappings" : {
"properties" : {
"product" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
}
查询:
{
"size": 0,
"aggs": {
"shampoo": {
"filter": {
"term": {
"product.name.keyword": "shampoo"
}
},"aggs": {
"count": {
"value_count": {
"field": "product.name.keyword"
}
}
}
}
}
}
结果:
"aggregations" : {
"shampoo" : {
"doc_count" : 2,
"count" : {
"value" : 2
}
}
}