【问题标题】:How to deal with aggregations (group by) in elasticsearch如何处理elasticsearch中的聚合(分组)
【发布时间】:2019-09-17 21:20:44
【问题描述】:

我正在从 SQL 迁移到 elasticsearch,但我遇到了一些聚合问题,尤其是 group by

我的查询看起来像

SELECT    count(*) as total,country_code 
FROM      orders 
WHERE     product_id = ? 
GROUP BY  country_code 
ORDER BY  total desc LIMIT 3 

SQL RESULT

这个我试过了,但是不行

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "line_items.product_id": {
                            "query": "0001112223333"
                        }
                    }
                }
            ]
        }
    },
    "from": 0,
    "size": 3,
    "aggregations": {
        "country_code": {
            "aggregations": {
                "COUNT(*)": {
                    "value_count": {
                        "field": "_index"
                    }
                }
            },
            "terms": {
                "field": "country_code",
                "size": 200
            }
        }
    }
}

ES RESULT

【问题讨论】:

  • “我试过这个但没有用” - 什么不完全有效?
  • @Caramiriel 我刚刚更新了我的问题请查看图片

标签: node.js elasticsearch elasticsearch-aggregation


【解决方案1】:

根据您的图像,使用keyword 数据类型而不是text

根据关键字的链接,

它们通常用于过滤(找到我所有的博客文章 status 已发布),用于排序和聚合。关键词 字段只能按其确切值进行搜索。

您观察到这些错误的原因是您尝试对 text 数据类型运行聚合查询。文本数据类型经过Analysis 阶段,ES 将在该阶段获取值,将其分解为标记并将它们存储在倒排索引中,

我建议您使用multi-fields,您的country_code 映射如下:

映射:

{  
   "properties":{  
      "country_code":{  
         "type":"text",
         "fields":{  
            "keyword":{  
               "type":"keyword"
            }
         }
      }
   }
}

聚合查询:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "line_items.product_id": {
                            "query": "0001112223333"
                        }
                    }
                }
            ]
        }
    },
    "from": 0,
    "size": 3,
    "aggregations": {
        "country_code": {
            "aggregations": {
                "COUNT(*)": {
                    "value_count": {
                        "field": "_index"
                    }
                }
            },
            "terms": { 
                "field": "country_code.keyword",          <----- change this
                "size": 200
            }
        }
    }
}

请注意我在聚合查询中使用country_code.keyword 的上述字段。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    您应该考虑将产品 ID 用作关键字而不是文本类型,然后对其使用术语查询而不是匹配查询,因为这样会更有效。此外,由于您不需要文档中的任何数据,因此您可以将查询的大小设置为 0。

    此外,您应该在 country_code 字段的映射中使用关键字类型。

    这个简单的查询应该可以完成您的工作 -

    {
      "size": 0,
      "query": {
        "term": {
          "line_items.product_id": 1116463
        }
      },
      "aggregations": {
        "ad_type": {
          "terms": {
            "field": "country_code",
            "size": 200
          }
        }
      }
    }
    

    附: - 也分享您的索引映射,因为它会使图片更清晰。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 2019-02-04
      • 2018-06-17
      • 1970-01-01
      • 2013-04-13
      相关资源
      最近更新 更多