# Position in facet

**URL:** <https://discuss.elastic.co/t/position-in-facet/4937>\
**Category:** Elasticsearch\
**Created:** [July 24, 2011, 1:20pm UTC](https://discuss.elastic.co/t/position-in-facet/4937 "2011-07-24T13:20:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mats\_Noren](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Mats\_Noren](https://discuss.elastic.co/u/Mats_Noren)\
**Post date:** [July 24, 2011, 1:20pm UTC](https://discuss.elastic.co/t/position-in-facet/4937/1 "2011-07-24T13:20:23Z")

</div>

Hi,  
I'm trying to use date\_histogram to calculate a users rank/position based on a script value. Somehow I sense there is a solution but I don't quite seem to get there.

For each date I have a document like this:

{  
"id": 1,  
"user\_id": 7,  
"points": 4500,  
"games": 2,  
"date": "2011-07-24"  
}

What I would like to do is to calculate for each day the users position in the total standings based on his average points per game.

To obtain a users position for a specific day I used search\_type=count and did a filter on a script.

script: "(doc['points'].value / doc['games'].value) \> 1000"

This required that I first had to obtain the users score for the same date.

Is there a way to use a query/filter in the script clause of another?

And does anyone have an idea how to solve my usecase?

Btw, Shay, thank you for a fantastic piece of software!

Regards,  
Mats

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [July 25, 2011, 12:11pm UTC](https://discuss.elastic.co/t/position-in-facet/4937/2 "2011-07-25T12:11:43Z")

</div>

I am not sure I followed the question properly..., but you can combine  
several filters using and/or filters, is that what you are after? Btw, for  
perf reasons, I would index the points / games value as another field, and  
use a range filter on it, will be much faster...

On Sun, Jul 24, 2011 at 4:20 PM, Mats Norén [mats.noren@gmail.com](mailto:mats.noren@gmail.com) wrote:

> Hi,  
> I'm trying to use date\_histogram to calculate a users rank/position based  
> on a script value. Somehow I sense there is a solution but I don't quite  
> seem to get there.
> 
> For each date I have a document like this:
> 
> {  
> "id": 1,  
> "user\_id": 7,  
> "points": 4500,  
> "games": 2,  
> "date": "2011-07-24"  
> }
> 
> What I would like to do is to calculate for each day the users position in  
> the total standings based on his average points per game.
> 
> To obtain a users position for a specific day I used search\_type=count and  
> did a filter on a script.
> 
> script: "(doc['points'].value / doc['games'].value) \> 1000"
> 
> This required that I first had to obtain the users score for the same date.
> 
> Is there a way to use a query/filter in the script clause of another?
> 
> And does anyone have an idea how to solve my usecase?
> 
> Btw, Shay, thank you for a fantastic piece of software!
> 
> Regards,  
> Mats

---

<div class="post-metadata">

**Author:** ![Mats\_Noren](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Mats\_Noren](https://discuss.elastic.co/u/Mats_Noren)\
**Post date:** [July 25, 2011, 1:36pm UTC](https://discuss.elastic.co/t/position-in-facet/4937/3 "2011-07-25T13:36:12Z")

</div>

Hi, Shay  
Not really, I would like use a count with a filter/query to get a  
users position for each interval in a date\_histogram.

So for instance if I have for each day in a month an average score of  
3400 points and there two other users with an average of 3500 and 3300  
I would like to get a position/rank 2 out of a total of 3 back from a  
date\_histogram facet.

As I tried to describe in my first query, I've used search\_type=count  
to get a users position for a certain day:

[http://localhost:9200/games/measure/\_search?search\_type=count](http://localhost:9200/games/measure/_search?search_type=count)  
{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"and" : [  
{  
"term" : {  
"date" : "2011-07-01"  
}  
},  
{  
"script": {  
"script": "(doc['points'].value / doc['games'].value) \<  
1000" \<--- Users average score for the day! A separate query for  
each day  
}  
}  
]  
}  
}  
}  
}

Basically what I'm after is to be able to use some kind of facet that  
for each date (or interval if that's possible) calculates the users  
position/rank based on a field or a script.

Pseudocode

"date\_histogram":{  
"key\_field":"date",  
"value\_script":"doc['points'].value / doc['games'].value",  
"interval":"day",  
"count\_query": {  
"term": { "user\_id": 1 },  
"operator": "gt"  
}  
},

Where the count\_query above appends/expands the key\_field(date in a  
date\_histogram) for each date\_histogram as an and-clause to the  
count\_query and the field used for the comparison is the same as the  
value\_script in the date\_histogram.

The result would be something like:

points\_per\_game: {

```
{ _type: date_histogram,
    entries: [
    {
            time: 1122336000000
            count: 3
            position: 2 <--- New field
            min: 1149.076923076923
            max: 1467.8333333333333
            total: 3865.710256410256
            total_count: 3
            mean: 1288.5700854700854
        }
      ,{
            time: 1122422400000
            count: 3
            position: 1
            min: 1039.8181818181818
            max: 1700
            total: 3934.818181818182
            total_count: 3
            mean: 1311.6060606060607
        }
     ]

```

Is this possible to achieve in another way?

/M

On Mon, Jul 25, 2011 at 2:11 PM, Shay Banon  
[shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:

> I am not sure I followed the question properly..., but you can combine  
> several filters using and/or filters, is that what you are after? Btw, for  
> perf reasons, I would index the points / games value as another field, and  
> use a range filter on it, will be much faster...
> 
> On Sun, Jul 24, 2011 at 4:20 PM, Mats Norén [mats.noren@gmail.com](mailto:mats.noren@gmail.com) wrote:
> 
> > Hi,  
> > I'm trying to use date\_histogram to calculate a users rank/position based  
> > on a script value. Somehow I sense there is a solution but I don't quite  
> > seem to get there.
> > 
> > For each date I have a document like this:
> > 
> > {  
> > "id": 1,  
> > "user\_id": 7,  
> > "points": 4500,  
> > "games": 2,  
> > "date": "2011-07-24"  
> > }
> > 
> > What I would like to do is to calculate for each day the users position in  
> > the total standings based on his average points per game.
> > 
> > To obtain a users position for a specific day I used search\_type=count and  
> > did a filter on a script.
> > 
> > script: "(doc['points'].value / doc['games'].value) \> 1000"
> > 
> > This required that I first had to obtain the users score for the same  
> > date.
> > 
> > Is there a way to use a query/filter in the script clause of another?
> > 
> > And does anyone have an idea how to solve my usecase?
> > 
> > Btw, Shay, thank you for a fantastic piece of software!
> > 
> > Regards,  
> > Mats

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [July 26, 2011, 6:03am UTC](https://discuss.elastic.co/t/position-in-facet/4937/4 "2011-07-26T06:03:40Z")

</div>

So you want a "two level" histogram, first broken down by date, and then by  
position? You can't do that, you will need to use something like terms facet  
and create a facet per date range (with the date range as a filter for the  
facet).

On Mon, Jul 25, 2011 at 4:36 PM, Mats Norén [mats.noren@gmail.com](mailto:mats.noren@gmail.com) wrote:

> Hi, Shay  
> Not really, I would like use a count with a filter/query to get a  
> users position for each interval in a date\_histogram.
> 
> So for instance if I have for each day in a month an average score of  
> 3400 points and there two other users with an average of 3500 and 3300  
> I would like to get a position/rank 2 out of a total of 3 back from a  
> date\_histogram facet.
> 
> As I tried to describe in my first query, I've used search\_type=count  
> to get a users position for a certain day:
> 
> [http://localhost:9200/games/measure/\_search?search\_type=count](http://localhost:9200/games/measure/_search?search_type=count)  
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "match\_all": {}  
> },  
> "filter": {  
> "and" : [  
> {  
> "term" : {  
> "date" : "2011-07-01"  
> }  
> },  
> {  
> "script": {  
> "script": "(doc['points'].value /  
> doc['games'].value) \<  
> 1000" \<--- Users average score for the day! A separate query for  
> each day  
> }  
> }  
> ]  
> }  
> }  
> }  
> }
> 
> Basically what I'm after is to be able to use some kind of facet that  
> for each date (or interval if that's possible) calculates the users  
> position/rank based on a field or a script.
> 
> Pseudocode
> 
> "date\_histogram":{  
> "key\_field":"date",  
> "value\_script":"doc['points'].value / doc['games'].value",  
> "interval":"day",  
> "count\_query": {  
> "term": { "user\_id": 1 },  
> "operator": "gt"  
> }  
> },
> 
> Where the count\_query above appends/expands the key\_field(date in a  
> date\_histogram) for each date\_histogram as an and-clause to the  
> count\_query and the field used for the comparison is the same as the  
> value\_script in the date\_histogram.
> 
> The result would be something like:
> 
> points\_per\_game: {
> 
> { \_type: date\_histogram,  
> entries: [  
> {  
> time: 1122336000000  
> count: 3  
> position: 2 \<--- New field  
> min: 1149.076923076923  
> max: 1467.8333333333333  
> total: 3865.710256410256  
> total\_count: 3  
> mean: 1288.5700854700854  
> }  
> ,{  
> time: 1122422400000  
> count: 3  
> position: 1  
> min: 1039.8181818181818  
> max: 1700  
> total: 3934.818181818182  
> total\_count: 3  
> mean: 1311.6060606060607  
> }  
> ]
> 
> Is this possible to achieve in another way?
> 
> /M
> 
> On Mon, Jul 25, 2011 at 2:11 PM, Shay Banon  
> [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> 
> > I am not sure I followed the question properly..., but you can combine  
> > several filters using and/or filters, is that what you are after? Btw,  
> > for  
> > perf reasons, I would index the points / games value as another field,  
> > and  
> > use a range filter on it, will be much faster...
> > 
> > On Sun, Jul 24, 2011 at 4:20 PM, Mats Norén [mats.noren@gmail.com](mailto:mats.noren@gmail.com)  
> > wrote:
> > 
> > > Hi,  
> > > I'm trying to use date\_histogram to calculate a users rank/position  
> > > based  
> > > on a script value. Somehow I sense there is a solution but I don't quite  
> > > seem to get there.
> > > 
> > > For each date I have a document like this:
> > > 
> > > {  
> > > "id": 1,  
> > > "user\_id": 7,  
> > > "points": 4500,  
> > > "games": 2,  
> > > "date": "2011-07-24"  
> > > }
> > > 
> > > What I would like to do is to calculate for each day the users position  
> > > in  
> > > the total standings based on his average points per game.
> > > 
> > > To obtain a users position for a specific day I used search\_type=count  
> > > and  
> > > did a filter on a script.
> > > 
> > > script: "(doc['points'].value / doc['games'].value) \> 1000"
> > > 
> > > This required that I first had to obtain the users score for the same  
> > > date.
> > > 
> > > Is there a way to use a query/filter in the script clause of another?
> > > 
> > > And does anyone have an idea how to solve my usecase?
> > > 
> > > Btw, Shay, thank you for a fantastic piece of software!
> > > 
> > > Regards,  
> > > Mats

---

<div class="post-metadata">

**Author:** ![Mats\_Noren](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Mats\_Noren](https://discuss.elastic.co/u/Mats_Noren)\
**Post date:** [July 26, 2011, 8:06am UTC](https://discuss.elastic.co/t/position-in-facet/4937/5 "2011-07-26T08:06:58Z")

</div>

Hi, Shay

Hmm....I'm not sure I quite understand your proposed solution.

I don't want it broken down by position (I think) but I would like to:

1. get a specific users points\_per\_game for a specific date (interval)
2. count how many records have a higher points\_per\_game for the date
3. do this for a given range of dates
4. return a list of counts (ie positions)

I've tried to come up with a proposed syntax for a threshold\_histogram  
(it's like a facet with a dynamic facet\_filter)

I'm back at my computer so I did a gist instead of pasting code into a  
mail 😉

> <https://gist.github.com/matsjg/1106230>

Is this possible?

/M

On Tue, Jul 26, 2011 at 8:03 AM, Shay Banon  
[shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:

> So you want a "two level" histogram, first broken down by date, and then by  
> position? You can't do that, you will need to use something like terms facet  
> and create a facet per date range (with the date range as a filter for the  
> facet).
> 
> On Mon, Jul 25, 2011 at 4:36 PM, Mats Norén [mats.noren@gmail.com](mailto:mats.noren@gmail.com) wrote:
> 
> > Hi, Shay  
> > Not really, I would like use a count with a filter/query to get a  
> > users position for each interval in a date\_histogram.
> > 
> > So for instance if I have for each day in a month an average score of  
> > 3400 points and there two other users with an average of 3500 and 3300  
> > I would like to get a position/rank 2 out of a total of 3 back from a  
> > date\_histogram facet.
> > 
> > As I tried to describe in my first query, I've used search\_type=count  
> > to get a users position for a certain day:
> > 
> > [http://localhost:9200/games/measure/\_search?search\_type=count](http://localhost:9200/games/measure/_search?search_type=count)  
> > {  
> > "query": {  
> > "filtered": {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "filter": {  
> > "and" : [  
> > {  
> > "term" : {  
> > "date" : "2011-07-01"  
> > }  
> > },  
> > {  
> > "script": {  
> > "script": "(doc['points'].value /  
> > doc['games'].value) \<  
> > 1000" \<--- Users average score for the day! A separate query for  
> > each day  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > }  
> > }
> > 
> > Basically what I'm after is to be able to use some kind of facet that  
> > for each date (or interval if that's possible) calculates the users  
> > position/rank based on a field or a script.
> > 
> > Pseudocode
> > 
> > "date\_histogram":{  
> > "key\_field":"date",  
> > "value\_script":"doc['points'].value / doc['games'].value",  
> > "interval":"day",  
> > "count\_query": {  
> > "term": { "user\_id": 1 },  
> > "operator": "gt"  
> > }  
> > },
> > 
> > Where the count\_query above appends/expands the key\_field(date in a  
> > date\_histogram) for each date\_histogram as an and-clause to the  
> > count\_query and the field used for the comparison is the same as the  
> > value\_script in the date\_histogram.
> > 
> > The result would be something like:
> > 
> > points\_per\_game: {
> > 
> > { \_type: date\_histogram,  
> > entries: [  
> > {  
> > time: 1122336000000  
> > count: 3  
> > position: 2 \<--- New field  
> > min: 1149.076923076923  
> > max: 1467.8333333333333  
> > total: 3865.710256410256  
> > total\_count: 3  
> > mean: 1288.5700854700854  
> > }  
> > ,{  
> > time: 1122422400000  
> > count: 3  
> > position: 1  
> > min: 1039.8181818181818  
> > max: 1700  
> > total: 3934.818181818182  
> > total\_count: 3  
> > mean: 1311.6060606060607  
> > }  
> > ]
> > 
> > Is this possible to achieve in another way?
> > 
> > /M
> > 
> > On Mon, Jul 25, 2011 at 2:11 PM, Shay Banon  
> > [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> > 
> > > I am not sure I followed the question properly..., but you can combine  
> > > several filters using and/or filters, is that what you are after? Btw,  
> > > for  
> > > perf reasons, I would index the points / games value as another field,  
> > > and  
> > > use a range filter on it, will be much faster...
> > > 
> > > On Sun, Jul 24, 2011 at 4:20 PM, Mats Norén [mats.noren@gmail.com](mailto:mats.noren@gmail.com)  
> > > wrote:
> > > 
> > > > Hi,  
> > > > I'm trying to use date\_histogram to calculate a users rank/position  
> > > > based  
> > > > on a script value. Somehow I sense there is a solution but I don't  
> > > > quite  
> > > > seem to get there.
> > > > 
> > > > For each date I have a document like this:
> > > > 
> > > > {  
> > > > "id": 1,  
> > > > "user\_id": 7,  
> > > > "points": 4500,  
> > > > "games": 2,  
> > > > "date": "2011-07-24"  
> > > > }
> > > > 
> > > > What I would like to do is to calculate for each day the users position  
> > > > in  
> > > > the total standings based on his average points per game.
> > > > 
> > > > To obtain a users position for a specific day I used search\_type=count  
> > > > and  
> > > > did a filter on a script.
> > > > 
> > > > script: "(doc['points'].value / doc['games'].value) \> 1000"
> > > > 
> > > > This required that I first had to obtain the users score for the same  
> > > > date.
> > > > 
> > > > Is there a way to use a query/filter in the script clause of another?
> > > > 
> > > > And does anyone have an idea how to solve my usecase?
> > > > 
> > > > Btw, Shay, thank you for a fantastic piece of software!
> > > > 
> > > > Regards,  
> > > > Mats

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [July 26, 2011, 12:00pm UTC](https://discuss.elastic.co/t/position-in-facet/4937/6 "2011-07-26T12:00:12Z")

</div>

I (think) I understand, and wondering if facet\_filter won't do what you  
want? Basically, _any_ facet type can be associated with a facet\_filter,  
which controls which documents will be included in the facet calculation.  
For example, you can use term filter, or range filter for that. Here is the  
docs for it (at the bottom):  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/).

Or, I just might be missing again what you are after 🙂

On Tue, Jul 26, 2011 at 11:06 AM, Mats Norén [mats.noren@gmail.com](mailto:mats.noren@gmail.com) wrote:

> Hi, Shay
> 
> Hmm....I'm not sure I quite understand your proposed solution.
> 
> I don't want it broken down by position (I think) but I would like to:
> 
> 1. get a specific users points\_per\_game for a specific date (interval)
> 2. count how many records have a higher points\_per\_game for the date
> 3. do this for a given range of dates
> 4. return a list of counts (ie positions)
> 
> I've tried to come up with a proposed syntax for a threshold\_histogram  
> (it's like a facet with a dynamic facet\_filter)
> 
> I'm back at my computer so I did a gist instead of pasting code into a  
> mail 😉
> 
> [gist:1106230 · GitHub](https://gist.github.com/1106230)
> 
> Is this possible?
> 
> /M
> 
> On Tue, Jul 26, 2011 at 8:03 AM, Shay Banon  
> [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> 
> > So you want a "two level" histogram, first broken down by date, and then  
> > by  
> > position? You can't do that, you will need to use something like terms  
> > facet  
> > and create a facet per date range (with the date range as a filter for  
> > the  
> > facet).
> > 
> > On Mon, Jul 25, 2011 at 4:36 PM, Mats Norén [mats.noren@gmail.com](mailto:mats.noren@gmail.com)  
> > wrote:
> > 
> > > Hi, Shay  
> > > Not really, I would like use a count with a filter/query to get a  
> > > users position for each interval in a date\_histogram.
> > > 
> > > So for instance if I have for each day in a month an average score of  
> > > 3400 points and there two other users with an average of 3500 and 3300  
> > > I would like to get a position/rank 2 out of a total of 3 back from a  
> > > date\_histogram facet.
> > > 
> > > As I tried to describe in my first query, I've used search\_type=count  
> > > to get a users position for a certain day:
> > > 
> > > [http://localhost:9200/games/measure/\_search?search\_type=count](http://localhost:9200/games/measure/_search?search_type=count)  
> > > {  
> > > "query": {  
> > > "filtered": {  
> > > "query": {  
> > > "match\_all": {}  
> > > },  
> > > "filter": {  
> > > "and" : [  
> > > {  
> > > "term" : {  
> > > "date" : "2011-07-01"  
> > > }  
> > > },  
> > > {  
> > > "script": {  
> > > "script": "(doc['points'].value /  
> > > doc['games'].value) \<  
> > > 1000" \<--- Users average score for the day! A separate query for  
> > > each day  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > Basically what I'm after is to be able to use some kind of facet that  
> > > for each date (or interval if that's possible) calculates the users  
> > > position/rank based on a field or a script.
> > > 
> > > Pseudocode
> > > 
> > > "date\_histogram":{  
> > > "key\_field":"date",  
> > > "value\_script":"doc['points'].value / doc['games'].value",  
> > > "interval":"day",  
> > > "count\_query": {  
> > > "term": { "user\_id": 1 },  
> > > "operator": "gt"  
> > > }  
> > > },
> > > 
> > > Where the count\_query above appends/expands the key\_field(date in a  
> > > date\_histogram) for each date\_histogram as an and-clause to the  
> > > count\_query and the field used for the comparison is the same as the  
> > > value\_script in the date\_histogram.
> > > 
> > > The result would be something like:
> > > 
> > > points\_per\_game: {
> > > 
> > > { \_type: date\_histogram,  
> > > entries: [  
> > > {  
> > > time: 1122336000000  
> > > count: 3  
> > > position: 2 \<--- New field  
> > > min: 1149.076923076923  
> > > max: 1467.8333333333333  
> > > total: 3865.710256410256  
> > > total\_count: 3  
> > > mean: 1288.5700854700854  
> > > }  
> > > ,{  
> > > time: 1122422400000  
> > > count: 3  
> > > position: 1  
> > > min: 1039.8181818181818  
> > > max: 1700  
> > > total: 3934.818181818182  
> > > total\_count: 3  
> > > mean: 1311.6060606060607  
> > > }  
> > > ]
> > > 
> > > Is this possible to achieve in another way?
> > > 
> > > /M
> > > 
> > > On Mon, Jul 25, 2011 at 2:11 PM, Shay Banon  
> > > [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> > > 
> > > > I am not sure I followed the question properly..., but you can combine  
> > > > several filters using and/or filters, is that what you are after? Btw,  
> > > > for  
> > > > perf reasons, I would index the points / games value as another field,  
> > > > and  
> > > > use a range filter on it, will be much faster...
> > > > 
> > > > On Sun, Jul 24, 2011 at 4:20 PM, Mats Norén [mats.noren@gmail.com](mailto:mats.noren@gmail.com)  
> > > > wrote:
> > > > 
> > > > > Hi,  
> > > > > I'm trying to use date\_histogram to calculate a users rank/position  
> > > > > based  
> > > > > on a script value. Somehow I sense there is a solution but I don't  
> > > > > quite  
> > > > > seem to get there.
> > > > > 
> > > > > For each date I have a document like this:
> > > > > 
> > > > > {  
> > > > > "id": 1,  
> > > > > "user\_id": 7,  
> > > > > "points": 4500,  
> > > > > "games": 2,  
> > > > > "date": "2011-07-24"  
> > > > > }
> > > > > 
> > > > > What I would like to do is to calculate for each day the users  
> > > > > position  
> > > > > in  
> > > > > the total standings based on his average points per game.
> > > > > 
> > > > > To obtain a users position for a specific day I used  
> > > > > search\_type=count  
> > > > > and  
> > > > > did a filter on a script.
> > > > > 
> > > > > script: "(doc['points'].value / doc['games'].value) \> 1000"
> > > > > 
> > > > > This required that I first had to obtain the users score for the same  
> > > > > date.
> > > > > 
> > > > > Is there a way to use a query/filter in the script clause of another?
> > > > > 
> > > > > And does anyone have an idea how to solve my usecase?
> > > > > 
> > > > > Btw, Shay, thank you for a fantastic piece of software!
> > > > > 
> > > > > Regards,  
> > > > > Mats

---

<div class="post-metadata">

**Author:** ![Mats\_Noren](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Mats\_Noren](https://discuss.elastic.co/u/Mats_Noren)\
**Post date:** [July 26, 2011, 3:29pm UTC](https://discuss.elastic.co/t/position-in-facet/4937/7 "2011-07-26T15:29:15Z")

</div>

Ok. I'll take a look at the facet\_filters again.  
I just don't see how I can do a count based on a filter that's varies by the day?

I'm thinking that my points\_per\_game for a certain query should be in the facet\_filter but I don't see how I can use it on the righthandside of a boolean expression, ie  
points\_per\_game \> (points\_per\_game\_for\_user\_id\_1\_for the\_day)

Is there an example available that does something similar?

Best regards,  
Mats

ds.  
Thanks again for your patience and for a great piece of software.  
ds.

26 jul 2011 kl. 14:00 skrev Shay Banon [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com):

> I (think) I understand, and wondering if facet\_filter won't do what you want? Basically, _any_ facet type can be associated with a facet\_filter, which controls which documents will be included in the facet calculation. For example, you can use term filter, or range filter for that. Here is the docs for it (at the bottom): [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/).
> 
> Or, I just might be missing again what you are after 🙂
> 
> On Tue, Jul 26, 2011 at 11:06 AM, Mats Norén [mats.noren@gmail.com](mailto:mats.noren@gmail.com) wrote:  
> Hi, Shay
> 
> Hmm....I'm not sure I quite understand your proposed solution.
> 
> I don't want it broken down by position (I think) but I would like to:
> 
> 1. get a specific users points\_per\_game for a specific date (interval)
> 2. count how many records have a higher points\_per\_game for the date
> 3. do this for a given range of dates
> 4. return a list of counts (ie positions)
> 
> I've tried to come up with a proposed syntax for a threshold\_histogram  
> (it's like a facet with a dynamic facet\_filter)
> 
> I'm back at my computer so I did a gist instead of pasting code into a  
> mail 😉
> 
> [gist:1106230 · GitHub](https://gist.github.com/1106230)
> 
> Is this possible?
> 
> /M
> 
> On Tue, Jul 26, 2011 at 8:03 AM, Shay Banon  
> [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> 
> > So you want a "two level" histogram, first broken down by date, and then by  
> > position? You can't do that, you will need to use something like terms facet  
> > and create a facet per date range (with the date range as a filter for the  
> > facet).
> > 
> > On Mon, Jul 25, 2011 at 4:36 PM, Mats Norén [mats.noren@gmail.com](mailto:mats.noren@gmail.com) wrote:
> > 
> > > Hi, Shay  
> > > Not really, I would like use a count with a filter/query to get a  
> > > users position for each interval in a date\_histogram.
> > > 
> > > So for instance if I have for each day in a month an average score of  
> > > 3400 points and there two other users with an average of 3500 and 3300  
> > > I would like to get a position/rank 2 out of a total of 3 back from a  
> > > date\_histogram facet.
> > > 
> > > As I tried to describe in my first query, I've used search\_type=count  
> > > to get a users position for a certain day:
> > > 
> > > [http://localhost:9200/games/measure/\_search?search\_type=count](http://localhost:9200/games/measure/_search?search_type=count)  
> > > {  
> > > "query": {  
> > > "filtered": {  
> > > "query": {  
> > > "match\_all": {}  
> > > },  
> > > "filter": {  
> > > "and" : [  
> > > {  
> > > "term" : {  
> > > "date" : "2011-07-01"  
> > > }  
> > > },  
> > > {  
> > > "script": {  
> > > "script": "(doc['points'].value /  
> > > doc['games'].value) \<  
> > > 1000" \<--- Users average score for the day! A separate query for  
> > > each day  
> > > }  
> > > }  
> > > ]  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > Basically what I'm after is to be able to use some kind of facet that  
> > > for each date (or interval if that's possible) calculates the users  
> > > position/rank based on a field or a script.
> > > 
> > > Pseudocode
> > > 
> > > "date\_histogram":{  
> > > "key\_field":"date",  
> > > "value\_script":"doc['points'].value / doc['games'].value",  
> > > "interval":"day",  
> > > "count\_query": {  
> > > "term": { "user\_id": 1 },  
> > > "operator": "gt"  
> > > }  
> > > },
> > > 
> > > Where the count\_query above appends/expands the key\_field(date in a  
> > > date\_histogram) for each date\_histogram as an and-clause to the  
> > > count\_query and the field used for the comparison is the same as the  
> > > value\_script in the date\_histogram.
> > > 
> > > The result would be something like:
> > > 
> > > points\_per\_game: {
> > > 
> > > { \_type: date\_histogram,  
> > > entries: [  
> > > {  
> > > time: 1122336000000  
> > > count: 3  
> > > position: 2 \<--- New field  
> > > min: 1149.076923076923  
> > > max: 1467.8333333333333  
> > > total: 3865.710256410256  
> > > total\_count: 3  
> > > mean: 1288.5700854700854  
> > > }  
> > > ,{  
> > > time: 1122422400000  
> > > count: 3  
> > > position: 1  
> > > min: 1039.8181818181818  
> > > max: 1700  
> > > total: 3934.818181818182  
> > > total\_count: 3  
> > > mean: 1311.6060606060607  
> > > }  
> > > ]
> > > 
> > > Is this possible to achieve in another way?
> > > 
> > > /M
> > > 
> > > On Mon, Jul 25, 2011 at 2:11 PM, Shay Banon  
> > > [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> > > 
> > > > I am not sure I followed the question properly..., but you can combine  
> > > > several filters using and/or filters, is that what you are after? Btw,  
> > > > for  
> > > > perf reasons, I would index the points / games value as another field,  
> > > > and  
> > > > use a range filter on it, will be much faster...
> > > > 
> > > > On Sun, Jul 24, 2011 at 4:20 PM, Mats Norén [mats.noren@gmail.com](mailto:mats.noren@gmail.com)  
> > > > wrote:
> > > > 
> > > > > Hi,  
> > > > > I'm trying to use date\_histogram to calculate a users rank/position  
> > > > > based  
> > > > > on a script value. Somehow I sense there is a solution but I don't  
> > > > > quite  
> > > > > seem to get there.
> > > > > 
> > > > > For each date I have a document like this:
> > > > > 
> > > > > {  
> > > > > "id": 1,  
> > > > > "user\_id": 7,  
> > > > > "points": 4500,  
> > > > > "games": 2,  
> > > > > "date": "2011-07-24"  
> > > > > }
> > > > > 
> > > > > What I would like to do is to calculate for each day the users position  
> > > > > in  
> > > > > the total standings based on his average points per game.
> > > > > 
> > > > > To obtain a users position for a specific day I used search\_type=count  
> > > > > and  
> > > > > did a filter on a script.
> > > > > 
> > > > > script: "(doc['points'].value / doc['games'].value) \> 1000"
> > > > > 
> > > > > This required that I first had to obtain the users score for the same  
> > > > > date.
> > > > > 
> > > > > Is there a way to use a query/filter in the script clause of another?
> > > > > 
> > > > > And does anyone have an idea how to solve my usecase?
> > > > > 
> > > > > Btw, Shay, thank you for a fantastic piece of software!
> > > > > 
> > > > > Regards,  
> > > > > Mats

---

<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, 3:59am UTC](https://discuss.elastic.co/t/position-in-facet/4937/8 "2017-07-06T03:59:15Z")

</div>


