Skip to content

Instantly share code, notes, and snippets.

@matsjg
Created July 26, 2011 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsjg/1106230 to your computer and use it in GitHub Desktop.
Save matsjg/1106230 to your computer and use it in GitHub Desktop.
// Input data
curl -XPUT http://localhost:9200/games/measure/1 -d '
{
"id": 1,
"user_id": 1,
"points": 3000,
"games": 3,
"points_per_game": 1000,
"date": "2011-07-23"
}
'
curl -XPUT http://localhost:9200/games/measure/2 -d '
{
"id": 2,
"user_id": 2,
"points": 6000,
"games": 4,
"points_per_game": 1500,
"date": "2011-07-23"
}'
curl -XPUT http://localhost:9200/games/measure/3 -d '
{
"id": 3,
"user_id": 1,
"points": 8000,
"games": 4,
"points_per_game": 2000,
"date": "2011-07-24"
}'
curl -XPUT http://localhost:9200/games/measure/4 -d '
{
"id": 4,
"user_id": 2,
"points": 2000,
"games": 4,
"points_per_game": 500,
"date": "2011-07-24"
}'
// I would like to compare a users value for a day with all other users values and see the position/rank of the user.
// I tried this one to get a single users value for the day:
curl -XPOST http://localhost:9200/games/measure/_search? -d '
{
"query": {
"constant_score": {
"filter": {
"and": [
{
"term": {
"user_id": "2"
}
},
{
"term": {
"date": "2011-07-24"
}
}
]
},
"boost": 1.0
}
},
"fields": ["points_per_game"]
}'
// Ok so now I've got the points per game for a specific user. What rank/position does he/she have on the day?
curl -XPOST http://localhost:9200/games/measure/_search?search_type=count -d '
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and" : [
{
"range" : {
"date" : {
"from" : "2011-07-24",
"to" : "2011-08-25"
}
}
},
{
"script": {
"script": "doc['points_per_game'].value > 500"
}
}
]
}
}
}
}'
/* The above returns 1, since there is only 1 user with a better score. Also it only works for a single day and I manually have to obtain the value to compare to (ie. the 500 of "user_id": "2".
What I would like to do is calculate a specific users rank/position for several days. Kind of like a threshold_facet.
With the result being something like:
*/
"threshold_histogram":{
"key_field":"date",
"value_field":"points_per_game",
"interval":"day",
"threshold_query": {
"term": { "user_id": 2 }
},
"operator": "gt"
// or
"threshold_histogram":{
"key_field":"date",
"value_field":"points_per_game",
"interval":"day",
"threshold_value": "500"
}
// where the threshold query (optional) is used to obtain a SINGLE record for each day to be used for comparison. This record need to contain the same value field.
// the default operator is > "gt"
"facets": {
"points_per_game_ranking": {
"_type": "threshold_histogram",
"entries": [
{
"time": 1122336000000,
"count": 0,
"total_count": 2
},
{
"time": 1122422400000,
"count": 1,
"total_count": 2
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment