【问题标题】:Using aggregation with filters in elastic search在弹性搜索中使用聚合和过滤器
【发布时间】:2014-12-22 03:19:08
【问题描述】:

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

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

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

【问题讨论】:

    标签: elasticsearch pyelasticsearch


    【解决方案1】:

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

    "query": {
      "filtered": {
       "query" : {
         "bool": {
              ...
         }
       },  
       "filter": {
        ...     
        }
      }
    }
    

    【讨论】:

    • 您确定我需要嵌套聚合吗?他们聚合了单独的东西,而且它看起来工作正常。
    • 你是对的 - 它确实有效,我已经更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多