How does ElasticSearch do nested range aggregation query

做~自己de王妃 提交于 2019-12-08 11:56:14

问题


** I am trying to aggregate and find price ranges if on the basis of nested offer price array (nested Array) object of sellerInfoES. Internal field is "offerPrice". How I can write aggregation on the nested array field in Elasticsearch. I tried this following query, but it's not working. Getting this error: Parse Failure [Found two aggregation type definitions in [price_ranges]: [nested] and [filter]]

Mapping:

{
   "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"
               }
            }
         }
      }
   }
}

Query:

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

回答1:


You have to use sub_aggregation. Use range aggregation inside nested aggregation like:

 {
 "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
                 }
              ]
           }
         }
       }
     }
   }
  }

Hope this helps!!



来源:https://stackoverflow.com/questions/36926511/how-does-elasticsearch-do-nested-range-aggregation-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!