1

我有一个弹性搜索运行这样的文档:

{
  id: 1,
  price: 620000,
  propertyType: "HO",
  location: {
    lat: 51.41999,
    lon: -0.14426
  },
  active: true,
  rentOrSale: "S",
}

我正在尝试使用聚合来获取有关使用聚合的某个区域的统计信息,并且我正在使用的查询如下:

{
  "sort": [
    {
      "id": "desc"
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "rentOrSale": "s"
          }
        },
        {
          "term": {
            "active": true
          }
        }
      ]
    },
    "filtered": {
      "filter": {
        "and": [
          {
            "geo_distance": {
              "distance": "15.0mi",
              "location": {
                "lat": 51.50735,
                "lon": -0.12776
              }
            }
          }
        ]
      }
    }
  },
  "aggs": {
    "propertytype_agg": {
      "terms": {
        "field": "propertyType"
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    },
    "bed_agg": {
      "terms": {
        "field": "numberOfBedrooms"
      },
      "aggs": {
        "avg_price": {
          "avg": {
            "field": "price"
          }
        }
      }
    }
  }
}

但在结果中我看不到聚合。只要我删除查询的布尔或过滤部分,我就可以看到聚合。我不知道为什么会这样,也不知道如何获得这些过滤器的聚合。我已经尝试使用这个问题的答案,但我无法解决它。有任何想法吗?

4

1 回答 1

1

我认为您的查询需要稍微重新安排 - 将“过滤”进一步向上移动并重复“查询”命令:

"query": {
  "filtered": {
   "query" : {
     "bool": {
          ...
     }
   },  
   "filter": {
    ...     
    }
  }
}
于 2014-12-21T12:32:00.363 回答