Skip to content

Instantly share code, notes, and snippets.

Created September 9, 2013 15:57
Show Gist options
  • Save anonymous/6497664 to your computer and use it in GitHub Desktop.
Save anonymous/6497664 to your computer and use it in GitHub Desktop.
Elasticsearch Statistical Facets work with either script or filter, but not both example
curl -XDELETE 'http://localhost:9200/tester
curl -XPUT 'http://localhost:9200/tester' -d '{}'
curl -XPUT 'http://localhost:9200/tester/test/_mapping' -d '{
"authors": {
"properties": {
"name": {"type": "string"},
"records": {
"type": "object", "index": "not_analyzed", "include_in_parent":"true",
"properties": {
"name": {"type": "string", "index": "not_analyzed"},
"count": {"type": "integer", "index": "not_analyzed"}
}
}
}
}
}' && echo
curl -XPUT 'http://localhost:9200/tester/test/1' -d '{
"name": "searchterm banana",
"records": [
{"name": "weight1", "count": 16},
{"name": "weight2", "count": 54}
]
}' && echo
curl -XPUT 'http://localhost:9200/tester/test/1' -d '{
"name": "searchterm banana second",
"records": [
{"name": "weight3", "count": 6},
{"name": "weight6", "count": 5}
]
}' && echo
curl -XPOST 'http://localhost:9200/tester/_refresh' && echo
#JSON Queries
#valid results
{"query":
{"query":
{"filtered":
{"query":
{"match_all":
{}
}
}
}
},
"facets":{
"counts":{
"statistical":{
"field":"records.count"
},
"facet_filter" : {
"term" : { "name" : "kimchy"}
}
}
}
}
#valid results
{"query":
{"query":
{"filtered":
{"query":
{"match_all":
{}
}
}
}
},
"facets":{
"counts":{
"statistical":{
"field":{
"_script":{
"script":"sum(doc['records.count'].values)",
"lang":"python"
}
}
}
}
}
}
#non-real infinite results
{"query":
{"query":
{"filtered":
{"query":
{"match_all":
{}
}
}
}
},
"facets":{
"counts":{
"statistical":{
"field":{
"_script":{
"script":"sum(doc['records.count'].values)",
"lang":"python"
}
}
},
"facet_filter" : {
"term" : { "name" : "kimchy"}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment