1

I have an elastic search running with documents like this one:

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

I'm trying to use aggregates to get statistics about a certain area using aggregations and the query I'm using is the following:

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

But in the result I can't see the aggregations. As soon as I remove either the bool or filtered part of the query I can see the aggregations. I can't figure out why this is happening, nor how do I get the aggregations for these filters. I've tried using the answer to this question but I've not been able to solve it. Any ideas?

1 Answer 1

1

I think your query need to be slightly re-arranged - move the "filtered" further up and repeat the "query" command:

"query": {
  "filtered": {
   "query" : {
     "bool": {
          ...
     }
   },  
   "filter": {
    ...     
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Are you sure I need to nest the aggregations? They aggregate separate thing and it seams to be working fine.
You're right - it does work, I've updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.