您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

具有分组依据和位置条件的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"
          }
        }
      }
    }
  }
}
其他 2022/1/1 18:13:49 有595人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶