Skip to content

Instantly share code, notes, and snippets.

@juneym
Created July 14, 2011 18:22
Show Gist options
  • Save juneym/1083059 to your computer and use it in GitHub Desktop.
Save juneym/1083059 to your computer and use it in GitHub Desktop.
Sample Date Facet Range that can be used for showing facet label like: 30 minutes ago, 1 hour ago etc.
{
"query": {
"match_all": {}
},
"facets": {
"dtmPosted": {
"range": {
"key_script": "doc['dtmPosted'].date.getMillis()",
"value_script": "doc['dtmPosted'].date.getMillis()",
"ranges": [
{
"from": 1310665698000,
"to": 1310667498000
},
{
"from": 1310663898000,
"to": 1310665698000
},
{
"from": 1310660298000,
"to": 1310663898000
},
{
"from": 1310649498000,
"to": 1310660298000
},
{
"from": 1310631498000,
"to": 1310649498000
},
{
"from": 1310581098000,
"to": 1310631498000
},
{
"from": 1310494698000,
"to": 1310581098000
},
{
"from": 1310408298000,
"to": 1310494698000
},
{
"from": 1310321898000,
"to": 1310408298000
},
{
"from": 1310235498000,
"to": 1310235498000
},
{
"from": 1310149098000,
"to": 1310235498000
},
{
"from": 1310062698000,
"to": 1310149098000
},
{
"from": 1309976298000,
"to": 1310062698000
},
{
"from": 1309889898000,
"to": 1309976298000
},
{
"from": 1309803498000,
"to": 1309889898000
},
{
"to": 1309803498000
}
]
}
}
}
}
@juneym
Copy link
Author

juneym commented Jul 14, 2011

the above JSON snippet is based on the following "Elastica_Facet_Range" configuration:

    $facets  = new Elastica_Facet_Range("dtmPosted");
    $facets->setKeyValueScripts("doc['dtmPosted'].date.getMillis()", "doc['dtmPosted'].date.getMillis()");
    $now = time();
    $facets->setRanges(
       array(
           array('from' => strtotime("-30 minutes", $now) * 1000, 'to' => $now * 1000),
           array('from' => strtotime("-1 hour", $now) * 1000, 'to' =>strtotime("-30 minutes", $now) * 1000),
           array('from' => strtotime("-2 hour", $now) * 1000, 'to' => strtotime("-1 hour", $now) * 1000),
           array('from' => strtotime("-5 hour", $now) * 1000, 'to' => strtotime("-2 hour", $now) * 1000),
           array('from' => strtotime("-10 hour", $now) * 1000, 'to' => strtotime("-5 hour", $now) * 1000),
           array('from' => strtotime("-1 day", $now) * 1000, 'to' => strtotime("-10 hour", $now) * 1000),
           array('from' => strtotime("-2 day", $now) * 1000, 'to' => strtotime("-1 day", $now) * 1000),
           array('from' => strtotime("-3 day", $now) * 1000, 'to' => strtotime("-2 day", $now) * 1000),
           array('from' => strtotime("-4 day", $now) * 1000, 'to' => strtotime("-3 day", $now) * 1000),
           array('from' => strtotime("-5 day", $now) * 1000, 'to' => strtotime("-5 day", $now) * 1000),
           array('from' => strtotime("-6 day", $now) * 1000, 'to' => strtotime("-5 day", $now) * 1000),
           array('from' => strtotime("-7 day", $now) * 1000, 'to' => strtotime("-6 day", $now) * 1000),
           array('from' => strtotime("-8 day", $now) * 1000, 'to' => strtotime("-7 day", $now) * 1000),
           array('from' => strtotime("-9 day", $now) * 1000, 'to' => strtotime("-8 day", $now) * 1000),
           array('from' => strtotime("-10 day", $now) * 1000, 'to' => strtotime("-9 day", $now) * 1000),
           array('to' => strtotime("-10 day") * 1000),
       )
    );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment