# Query + Aggregations Driven From Query Response

**URL:** <https://discuss.elastic.co/t/query-aggregations-driven-from-query-response/188597>\
**Category:** Elasticsearch\
**Created:** [July 3, 2019, 2:52am UTC](https://discuss.elastic.co/t/query-aggregations-driven-from-query-response/188597 "2019-07-03T02:52:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thegrif](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thegrif/32/85515_2.png) [@thegrif](https://discuss.elastic.co/u/thegrif)\
**Post date:** [July 3, 2019, 2:52am UTC](https://discuss.elastic.co/t/query-aggregations-driven-from-query-response/188597/1 "2019-07-03T02:52:03Z")

</div>

**I am hoping the community can point me in the right direction regarding options for performing what I'd describe as a multistep query.**

The first "step" of the query is a regular boolean search. Results come back something like this:

```
      {
        "_index": "properties",
        "_type": "commercial",
        "_id": "71c202e2-9d29-11e9-840b-0e9d35d204a6",
        "_score": 2.7348974,
        "_source": {
          "fips_apn": "06067035-0103-007-0000",
          "property_city": "SACRAMENTO",
          "property_state": "CA"
        }
      },
      {
        "_index": "properties",
        "_type": "commercial",
        "_id": "71c202e5-9d29-11e9-840b-0e9d35d204a6",
        "_score": 2.7348974,
        "_source": {
          "fips_apn": "06067035-0111-014-0000",
          "property_city": "SACRAMENTO",
          "property_state": "CA"
        }
      }

```

I'm hoping to add to each response the aggregate number of indexed records that share the same fips\_apn value.

For example, in the following response, 4 records would have fips\_apn=06067035-0111-014-0000 and 6 records would have fips\_apn=06067035-0103-007-0000:

```
      {
        "_index": "properties",
        "_type": "commercial",
        "_id": "71c202e2-9d29-11e9-840b-0e9d35d204a6",
        "_score": 2.7348974,
        "_source": {
          "fips_apn": "06067035-0103-007-0000",
          "fips_apn_count": 6, <----------------*
          "property_city": "SACRAMENTO",
          "property_state": "CA"
        }
      },
      {
        "_index": "properties",
        "_type": "commercial",
        "_id": "71c202e5-9d29-11e9-840b-0e9d35d204a6",
        "_score": 2.7348974,
        "_source": {
          "fips_apn": "06067035-0111-014-0000",
          "fips_apn_count": 4, <----------------*
          "property_city": "SACRAMENTO",
          "property_state": "CA"
        }
      }

```

Is there a straightforward way of accomplishing this type of aggregation based on a value that comes back as part of a result? Or will this require a separate aggregation-focused query?

Thanks in advance for your help! I appreciate it 🙂

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 3, 2019, 8:34am UTC](https://discuss.elastic.co/t/query-aggregations-driven-from-query-response/188597/2 "2019-07-03T08:34:45Z")

</div>

Indeed there is: Use a [terms aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/7.2/search-aggregations-bucket-terms-aggregation.html) on the `fips_apn` field.

The response structure is different than what you are describing, but the idea is to group documents by a certain field value and then return a count of the documents in each of those buckets (you can go more fancy but this is the start of it).

---

<div class="post-metadata">

**Author:** ![Jurgen\_Debedts](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jurgen_debedts/32/49335_2.png) [@Jurgen\_Debedts](https://discuss.elastic.co/u/Jurgen_Debedts)\
**Post date:** [July 3, 2019, 9:06am UTC](https://discuss.elastic.co/t/query-aggregations-driven-from-query-response/188597/3 "2019-07-03T09:06:10Z")

</div>

Hello,

How do you want to use this? I can share a sample of this using the Java REST API, but no use if that is not your use case.

Jurgen

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 31, 2019, 9:06am UTC](https://discuss.elastic.co/t/query-aggregations-driven-from-query-response/188597/4 "2019-07-31T09:06:12Z")

</div>

Closed on July 31, 2019, 9:06am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
