Skip to content

Instantly share code, notes, and snippets.

@stratospark
Created May 23, 2011 23:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save stratospark/987814 to your computer and use it in GitHub Desktop.
facet counts
Using elasticsearch/0.16.0
on
Java(TM) SE Runtime Environment (build 1.6.0_24-b07-334-10M3326)
Added this dynamic template
curl -XGET http://127.0.0.1:9200/samples/sample/_mapping?pretty=true
{
"sample" : {
"dynamic_templates" : [ {
"template_1" : {
"mapping" : {
"type" : "multi_field",
"fields" : {
"orig" : {
"include_in_all" : true,
"index" : "not_analyzed",
"store" : "yes",
"type" : "{dynamic_type}"
},
"{name}" : {
"include_in_all" : true,
"index" : "analyzed",
"store" : "yes",
"type" : "{dynamic_type}"
}
}
},
"match_mapping_type" : "string",
"match" : "*"
}
} ],
"properties" : {
"location" : {
"dynamic" : "true",
"properties" : {
"location_name" : {
"type" : "multi_field",
"fields" : {
"location_name" : {
"store" : "yes",
"type" : "string"
},
"orig" : {
"include_in_all" : false,
"index" : "not_analyzed",
"store" : "yes",
"type" : "string"
}
}
}
}
}
}
}
}
Wrote a python script that indexed the following object 10,000 times. For the first 1000, location.location_name was "1". For the next 1000, location.location_name was "2". And so on
{
'location': {
'location_name': "1"
}
}
Top facets should all have counts of 1000.
> curl -XPOST http://127.0.0.1:9200/samples/_search?pretty=true -d '
{
"query": {
"match_all": {}
},
"facets": {
"location.location_name": {
"terms": {
"field": "location.location_name.orig",
"size": 5
}
}
},
"size": 0
}'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 10000,
"max_score" : 1.0,
"hits" : [ ]
},
"facets" : {
"location.location_name" : {
"_type" : "terms",
"missing" : 0,
"terms" : [ {
"term" : "6",
"count" : 816
}, {
"term" : "5",
"count" : 648
}, {
"term" : "9",
"count" : 629
}, {
"term" : "2",
"count" : 616
}, {
"term" : "4",
"count" : 423
} ]
}
}
}
Narrowing the result set down to location_name = "6" gives us the correct facet count of 1000.
> curl -XPOST http://127.0.0.1:9200/samples/_search?pretty=true -d '
{
"query": {
"term": {
"location.location_name.orig": "6"
}
},
"facets": {
"location.location_name": {
"terms": {
"field": "location.location_name.orig",
"size": 1
}
}
},
"size": 0
}'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : 3.3577898,
"hits" : [ ]
},
"facets" : {
"location.location_name" : {
"_type" : "terms",
"missing" : 0,
"terms" : [ {
"term" : "6",
"count" : 1000
} ]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment