# Aggregate results by query input

**URL:** <https://discuss.elastic.co/t/aggregate-results-by-query-input/26692>\
**Category:** Elasticsearch\
**Created:** [August 2, 2015, 5:46pm UTC](https://discuss.elastic.co/t/aggregate-results-by-query-input/26692 "2015-08-02T17:46:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![reshadman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/reshadman/32/3974_2.png) [@reshadman](https://discuss.elastic.co/u/reshadman)\
**Post date:** [August 2, 2015, 5:46pm UTC](https://discuss.elastic.co/t/aggregate-results-by-query-input/26692/1 "2015-08-02T17:46:08Z")

</div>

I have two indexes, the first one called **areas** is an index containing of multiple polygons, and second one is an index containing of multiple **points** indicating the document's lat/lon on earth, the index is called **stores**.

The problem is that I want to attach areas(polygons) that contain the **store's point** (lat/lon) inside of them, to the store's document as an embedded object. So I can easily find that a document on **stores** index has which areas.

In an **RDBMS** I was able to join this two table and find the needed records, In elasticsearch I can iterate over **areas** index documents one by one and perform a query to find the **stores** that fit in the **area's** polygon, and then perform an update on the **store** document, as the number of **areas** are huge, I have to perform over 50,000 queries on elasticsearch.

A good solution would be sending a bulk list of areas with the query and tell elasticsearch to aggregate returning results by them the answer would be something like this:

- Polygon A : [store1, store2, store3],
- Polygon B : [store1, store4, store5]

I didn't find anyway to implement this and I switched back to RDBMS solution which is not very optimized and fast too, but it is faster in design.

Is there anyway to implement this with elasticsearch?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [August 2, 2015, 9:12pm UTC](https://discuss.elastic.co/t/aggregate-results-by-query-input/26692/2 "2015-08-02T21:12:39Z")

</div>

Not directly.  
Another option would be to create a new index with this info already calculated, so you can query that instead.

---

<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 5, 2017, 11:57pm UTC](https://discuss.elastic.co/t/aggregate-results-by-query-input/26692/3 "2017-07-05T23:57:50Z")

</div>


