Skip to content

Instantly share code, notes, and snippets.

@MarneusCalgarXP
Last active August 29, 2015 14:22
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 MarneusCalgarXP/cce1cee54645e196c93b to your computer and use it in GitHub Desktop.
Save MarneusCalgarXP/cce1cee54645e196c93b to your computer and use it in GitHub Desktop.
Nested facet/aggregation with only pending value
I'm trying to do the following:
A player can play to many games.
Every game session has a begining date and a end date.
- a player can have simultaneous game sessions
- a player can have multiple sessions of a same game
(to simplify the samples I set only full years as dates)
I would like a facet/aggregation that filters only the active sessions at a given date.
ie:
Player1:
- HearthStone: 2015-2016
- Magic : 2005-2016
Player2:
- HearthStone: 2014-2015
- Magic : 2005-2008 & 2009-2010
So if I choose the date 2015, I should have the following result:
- HearthStone : 2
- Magic : 1
I can't manage to get what I want
{
"player": {
"properties":{
"games":{
"type":"nested",
"properties": {
"name": {
"type": "string",
"index":"not_analyzed"
},
"periods": {
"type": "nested",
"properties": {
"beginDate": {
"type": "date",
"format": "dd/MM/yyyy"
},
"endDate": {
"type": "date",
"format": "dd/MM/yyyy"
}
}
}
}
}
}
}
}
{
"games" : [
{
"name": "HearthStone",
"periods": [ {
"startDate": "01/01/2015",
"endDate": "01/01/2015"
} ]
},
{
"name": "Magic",
"periods": [ {
"startDate": "01/01/2005",
"endDate": "01/01/2016"
} ]
}
]
}
{
"games" : [
{
"name": "HearthStone",
"periods": [ {
"startDate": "01/01/2014",
"endDate": "01/01/2015"
} ]
},
{
"name": "Magic",
"periods": [ {
"startDate": "01/01/2005",
"endDate": "01/01/2008"
}, {
"startDate": "01/01/2009",
"endDate": "01/01/2010"
}]
}
]
}
{
"size": 0,
"facets": {
"facet_games": {
"nested": "games",
"terms": {
"field": "games.name",
"size": 10
},
"facet_filter": {
"and": {
"filters": [
{
"nested": {
"path": "games.periods",
"query": {
"range": {
"games.periods.beginDate": {
"from": null,
"to": "01/01/2015",
"include_lower": true,
"include_upper": true
}
}
}
}
},
{
"nested": {
"path": "games.periods",
"query": {
"range": {
"games.periods.endDate": {
"from": "01/01/2015",
"to": null,
"include_lower": true,
"include_upper": true
}
}
}
}
}
]
}
}
}
}
}
#!/bin/sh
# CONSTANTS
ELASTIC_URL="http://localhost:9200"
INDEX_NAME="test"
DOCUMENT_NAME="player"
INDEX_URL="${ELASTIC_URL}/${INDEX_NAME}"
DOCUMENT_URL="${INDEX_URL}/${DOCUMENT_NAME}"
# METADATA : index (re)creation
echo "create index ${INDEX_URL}"
curl -XDELETE ${INDEX_URL} && echo""
curl -XPOST ${INDEX_URL} && echo ""
# METADATA : mappings creation
echo "set mappings"
curl -XPUT ${DOCUMENT_URL}/_mapping -d "`cat mapping-player.json`" && echo ""
# DATA : add 2 players
echo "add players"
curl -XPUT ${DOCUMENT_URL}/1 -d "`cat player-1.json`" && echo ""
curl -XPUT ${DOCUMENT_URL}/2 -d "`cat player-2.json`" && echo ""
# REFRESH INDEX
echo "refresh index"
curl -XPOST ${INDEX_URL}/_refresh && echo ""
# QUERY : facet for active session games
echo "query"
curl -XPOST ${INDEX_URL}/_search?pretty=true -d "`cat query.json`" && echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment