一尘不染

ElasticSearch如何执行嵌套范围聚合查询

elasticsearch

**如果要基于SellerInfoES的嵌套要约价格数组(嵌套数组)对象,我正在尝试汇总和查找价格范围。内部字段是“
offerPrice”。我如何在Elasticsearch中的嵌套数组字段上编写聚合。我尝试了以下查询,但无法正常工作。收到此错误:解析失败[在[price_ranges]中找到了两个聚合类型定义:[嵌套]和[过滤器]]

对应:

{
   "productsearch": {
      "mappings": {
         "product": {
            "properties": {
               "brand": {
                  "type": "string"
               },
               "categories": {
                  "type": "string"
               },
               "model": {
                  "type": "string"
               },
               "mrp": {
                  "type": "double"
               },
               "productName": {
                  "type": "string"
               },
               "rating": {
                  "type": "double"
               },
               "reviewCount": {
                  "type": "long"
               },
               "sellerInfoES": {
                  "type": "nested",
                  "properties": {
                     "addr": {
                        "type": "string"
                     },
                     "country": {
                        "type": "string"
                     },
                     "geoAddress": {
                        "type": "string"
                     },
                     "location": {
                        "type": "string"
                     },
                     "offerPrice": {
                        "type": "double"
                     },
                     "pinCode": {
                        "type": "string"
                     },
                     "sellerId": {
                        "type": "long"
                     },
                     "sellerLocation": {
                        "type": "geo_point"
                     },
                     "state": {
                        "type": "string"
                     }
                  }
               },
               "sku": {
                  "type": "long"
               },
               "subCategory": {
                  "type": "string"
               }
            }
         }
      }
   }
}

查询:

{
  "price_ranges": {
    "nested": {
      "path": "sellerInfoES"
    },
    "aggs": {
      "range": {
        "field": "offerPrice",
        "ranges": [
          {
            "gte": 1000
          },
          {
            "gte": 1000,
            "lte": 10000
          },
          {
            "gte": 10000,
            "lte": 25000
          },
          {
            "gte": 25000
          }
        ]
      }
    }
  }
}

阅读 542

收藏
2020-06-22

共1个答案

一尘不染

您必须使用sub_aggregationrange aggregation在内部使用,nested aggregation例如:

 {
 "aggs": {
  "nested_sellerInfo": {
     "nested": {
        "path": "sellerInfoES"
     },
     "aggs": {
        "offer_price_range": {
           "range": {
              "field": "sellerInfoES.offerPrice",
              "ranges": [
                 {
                    "from": 1000
                 },
                 {
                    "from": 1000,
                    "to": 10000
                 },
                 {
                    "from": 10000,
                    "to": 25000
                 },
                 {
                    "from": 25000
                 }
              ]
           }
         }
       }
     }
   }
  }

希望这可以帮助!!

2020-06-22