微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

具有分组依据和位置条件的elasticsearch总和聚合

如何解决具有分组依据和位置条件的elasticsearch总和聚合

根据上面的查询,您将拥有一个products带有product类型文档的索引,该文档的映射如下所示:

curl -XPUT localhost:9200/products -d '
{
  "mappings": {
    "product": {
      "properties": {
        "Color": {
          "type": "string"
        },
        "Name": {
          "type": "string"
        },
        "ListPrice": {
          "type": "double"
        },
        "Standardcost": {
          "type": "double"
        }
      }
    }
  }
}'

然后,相当于您上面给出的sql的ES查询如下所示:

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "default_field": "Name",
          "query": "Mountain*"
        }
      },
      "filter": {
        "bool": {
          "must_not": [
            {
              "missing": {
                "field": "Color"
              }
            },
            {
              "term": {
                "ListPrice": 0
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "by_color": {
      "terms": {
        "field": "Color"
      },
      "aggs": {
        "total_price": {
          "sum": {
            "field": "ListPrice"
          }
        },
        "total_cost": {
          "sum": {
            "field": "Standardcost"
          }
        }
      }
    }
  }
}

解决方法

我是ElasticSearch的新手。

当前,我们正在将代码从关系数据库迁移到ElasticSearch。因此,我们正在将查询转换为ElasticSearch查询格式。

我正在寻找与以下查询等效的ElasticSearch-

SELECT Color,SUM(ListPrice),SUM(StandardCost)
FROM Production.Product
WHERE Color IS NOT NULL 
    AND ListPrice != 0.00 
    AND Name LIKE 'Mountain%'
GROUP BY Color

有人可以为我提供上面的ElasticSearch查询示例吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。