# Parametric Search with aggregations

**URL:** <https://discuss.elastic.co/t/parametric-search-with-aggregations/16959>\
**Category:** Elasticsearch\
**Created:** [April 11, 2014, 2:13pm UTC](https://discuss.elastic.co/t/parametric-search-with-aggregations/16959 "2014-04-11T14:13:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cheyne\_Rood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cheyne_rood/32/1644_2.png) [@Cheyne\_Rood](https://discuss.elastic.co/u/Cheyne_Rood)\
**Post date:** [April 11, 2014, 2:13pm UTC](https://discuss.elastic.co/t/parametric-search-with-aggregations/16959/1 "2014-04-11T14:13:40Z")

</div>

I've been working on advanced parametric search with elasticsearch v1.1 and  
am having trouble getting aggregations to work in the way I'd like. I've  
scoured the documentation and tried a couple different approaches without  
luck, so I'm hoping someone on this list can help.

I have an index, "parts", and each indexed item has properties that I'd  
like to use for parametric search, along with some text fields that I'll  
allow the user to query.

On the frontend, I'd like to present the user with a search query input,  
but also a number of parametric filtering options...here's how that looks  
with only category filtering applied:  
[![CloudApp](https://d2ffutrenqvap3.cloudfront.net/items/30293E3F1Y0y240M421K/Screen%20Shot%202014-04-11%20at%209.30.38%20AM.png "Screen Shot 2014-04-11 at 9.30.38 AM.png") ](https://cl.ly/0N1K0m141w1s)

The challenges arise once I want to select a parametric filter, e.g.:  
[![CloudApp](https://d2ffutrenqvap3.cloudfront.net/items/0e1Z1L141X3m1j46310s/Screen%20Shot%202014-04-11%20at%209.45.11%20AM.png "Screen Shot 2014-04-11 at 9.45.11 AM.png") ](https://cl.ly/1F382C050J32)

Here, I've checked "0.14" under "Weights". Notice that the other options  
for weights have disappeared. This is of course because ES is only  
aggregating the weights in the result set, and the only value in the result  
set is "0.14". For the user, this is no good because they should be able  
to select other weight values. On the backend, I'm including the weight  
condition in my query:

> <https://gist.github.com/cheyner/10471361#file-elastic-parts-condition-in-query>

So I took another approach, and put the weight condition in the filters,  
rather than the query, so it wouldn't affect aggregations. Here's how the  
backend settings look now:

> <https://gist.github.com/cheyner/10471361#file-elastic-parts-condition-in-filters>

And here's the frontend result:  
[![CloudApp](https://d2ffutrenqvap3.cloudfront.net/items/1M381s2X0a282X1w1e3Y/Screen%20Shot%202014-04-11%20at%209.54.38%20AM.png "Screen Shot 2014-04-11 at 9.54.38 AM.png") ](https://cl.ly/2c262N0j2M0M)

The good news is that now I can see the sibling choices for my weight  
filters, along with accurate counts of how selecting a sibling option would  
affect the query results. The problem is that, because the weight  
condition is not considered in aggregations, the aggregation numbers are  
off for all of the non-weight parametric options. For example, the  
"Material" option has an aggregate count of 5 results...but selecting it  
would actually decrease the results to 0.

Does anyone know of a good solution for this? It seems fundamental to a  
lot of parametric search solutions, so I'm hoping there's an answer. The  
only route I can see now seems really expensive and messy: for each  
aggregation, I need to apply all of the query conditions in the aggregation  
filters...for example:

> <https://gist.github.com/cheyner/10471361#file-elastic-parts-conditions-in-aggregations>

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/19d42213-000a-4033-ac71-90d25ee1bd0c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/19d42213-000a-4033-ac71-90d25ee1bd0c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Cheyne\_Rood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cheyne_rood/32/1644_2.png) [@Cheyne\_Rood](https://discuss.elastic.co/u/Cheyne_Rood)\
**Post date:** [April 14, 2014, 3:27pm UTC](https://discuss.elastic.co/t/parametric-search-with-aggregations/16959/2 "2014-04-14T15:27:42Z")

</div>

Hey gang,

Just a quick update to say I've got this working, but I had to do it the  
hard way. I keep track of all the filters my users apply in the parametric  
options and factor them in to each aggregation. It's definitely going to  
result in more expensive queries, but I'm hoping it'll be performant enough  
to pass muster. So far it's still snappy with my test data.

On Friday, April 11, 2014 10:13:40 AM UTC-4, Cheyne Rood wrote:

> I've been working on advanced parametric search with elasticsearch v1.1  
> and am having trouble getting aggregations to work in the way I'd like.  
> I've scoured the documentation and tried a couple different approaches  
> without luck, so I'm hoping someone on this list can help.
> 
> I have an index, "parts", and each indexed item has properties that I'd  
> like to use for parametric search, along with some text fields that I'll  
> allow the user to query.
> 
> On the frontend, I'd like to present the user with a search query input,  
> but also a number of parametric filtering options...here's how that looks  
> with only category filtering applied:  
> [http://cl.ly/image/0N1K0m141w1s](http://cl.ly/image/0N1K0m141w1s)
> 
> The challenges arise once I want to select a parametric filter, e.g.:  
> [http://cl.ly/image/1F382C050J32](http://cl.ly/image/1F382C050J32)
> 
> Here, I've checked "0.14" under "Weights". Notice that the other options  
> for weights have disappeared. This is of course because ES is only  
> aggregating the weights in the result set, and the only value in the result  
> set is "0.14". For the user, this is no good because they should be able  
> to select other weight values. On the backend, I'm including the weight  
> condition in my query:
> 
> [Parametric search examples: Aggregating · GitHub](https://gist.github.com/cheyner/10471361#file-elastic-parts-condition-in-query)
> 
> So I took another approach, and put the weight condition in the filters,  
> rather than the query, so it wouldn't affect aggregations. Here's how the  
> backend settings look now:
> 
> [Parametric search examples: Aggregating · GitHub](https://gist.github.com/cheyner/10471361#file-elastic-parts-condition-in-filters)
> 
> And here's the frontend result:  
> [http://cl.ly/image/2c262N0j2M0M](http://cl.ly/image/2c262N0j2M0M)
> 
> The good news is that now I can see the sibling choices for my weight  
> filters, along with accurate counts of how selecting a sibling option would  
> affect the query results. The problem is that, because the weight  
> condition is not considered in aggregations, the aggregation numbers are  
> off for all of the non-weight parametric options. For example, the  
> "Material" option has an aggregate count of 5 results...but selecting it  
> would actually decrease the results to 0.
> 
> Does anyone know of a good solution for this? It seems fundamental to a  
> lot of parametric search solutions, so I'm hoping there's an answer. The  
> only route I can see now seems really expensive and messy: for each  
> aggregation, I need to apply all of the query conditions in the aggregation  
> filters...for example:  
> [Parametric search examples: Aggregating · GitHub](https://gist.github.com/cheyner/10471361#file-elastic-parts-conditions-in-aggregations)

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a3572a91-b532-4666-bc9b-ba03d56c5f06%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a3572a91-b532-4666-bc9b-ba03d56c5f06%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [April 16, 2014, 2:04pm UTC](https://discuss.elastic.co/t/parametric-search-with-aggregations/16959/3 "2014-04-16T14:04:54Z")

</div>

Or you can make 2 separate aggregations, one for your number crunching, and  
another for your global parametrics. Something like this conceptually:

{  
"aggs": {  
"metrics": {  
#all your standard metrics aggs here, all filtered by your query  
},  
"parametrics": {  
"global": {} #global means don't filter by query criteria  
# all your parametrics aggs here  
}  
}  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/43450dd0-7f3d-4746-b578-8a281d59995f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/43450dd0-7f3d-4746-b578-8a281d59995f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:35am UTC](https://discuss.elastic.co/t/parametric-search-with-aggregations/16959/4 "2017-07-06T01:35:30Z")

</div>


