Skip to content

Instantly share code, notes, and snippets.

@nezda
Created August 15, 2014 18:15
Show Gist options
  • Save nezda/60932c73a8485e9d9a49 to your computer and use it in GitHub Desktop.
Save nezda/60932c73a8485e9d9a49 to your computer and use it in GitHub Desktop.
Troubles porting an elasticsearch Terms Facet to a Terms Aggregation
#!/bin/sh
# Remove old data
curl -XDELETE "http://localhost:9200/fml?pretty=true"
# Add some data - default mappings are fine
curl -XPOST "http://localhost:9200/fml/widget/?pretty=true" -d '
{
"name": "uno",
"tags": [
{"name": "1",
"id": 1},
{"name": "2",
"id": 2}
]
}
'
curl -XPOST "http://localhost:9200/fml/widget/?pretty=true" -d '
{
"name": "dos",
"tags": [
{"name": "3",
"id": 3}
]
}
'
curl -XPOST "http://localhost:9200/fml/widget/?pretty=true" -d '
{
"name": "tres",
"tags": [
{"name": "1",
"id": 1},
{"name": "3",
"id": 3}
]
}
'
# Wait for ES to be synced (aka refresh indices)
curl -XPOST "http://localhost:9200/fml/_refresh?pretty=true"
echo "term facets: note, returns counts for 2 & 3 ..."
curl -XPOST "http://localhost:9200/fml/widget/_search?pretty=true" -d '
{
"facets": {
"case_tags": {
"terms": {
"field": "tags.id",
"size": 100000,
"exclude": [ 1 ]
}
}
}
}
'
echo "term aggregations (with semantically incorrect document filter that we really want applied to the resulting aggreation)..."
curl -XPOST "http://localhost:9200/fml/widget/_search?pretty=true" -d '
{
"aggregations": {
"case_tags": {
"filter": {
"bool": {
"must_not": [
{
"terms": {
"tags.id": [ 1 ]
}
}
]
}
},
"aggregations": {
"tag_ids": {
"terms": {
"field": "tags.id"
}
}
}
}
}
}
'
## this term facet-based "query format" is not supported by term aggregations
#echo "term aggregations exclude array (removes filter which is wrong)..."
#
#curl -XPOST "http://localhost:9200/fml/widget/_search?pretty=true" -d '
#{
# "aggregations": {
# "case_tags": {
# "aggregations": {
# "tag_ids": {
# "terms": {
# "field": "tags.id",
# "exclude": [ 1 ]
# }
# }
# }
# }
# }
#}
#'
## the new exclude format doesn't support numeric fields - fails with:
## Aggregation [tag_ids] cannot support the include/exclude settings as it can only be applied to string values]
#echo "term aggregations exclude regex (removes filter which is wrong)..."
#
#curl -XPOST "http://localhost:9200/fml/widget/_search?pretty=true" -d '
#{
# "aggregations": {
# "tag_ids": {
# "terms": {
# "field": "tags.id",
# "exclude": "1"
# }
# }
# }
#}
#'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment