Skip to content

Instantly share code, notes, and snippets.

Created December 19, 2012 16:57
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 anonymous/4338257 to your computer and use it in GitHub Desktop.
Save anonymous/4338257 to your computer and use it in GitHub Desktop.
Elastic Search Terms Facet Weirdness
# Create Index
curl -XPUT 'http://localhost:9200/survey/' -d '{
"number_of_shards": 1,
"number_of_replicas": 0
}'
# Put mapping
curl -XPUT 'http://localhost:9200/survey/response/_mapping' -d '{
"response" : {
"_source" : {
"compression" : true
},
"properties" : {
"impression" : {
"type": "object",
"properties" : {
"topic" : {
"type":"string"
},
"sentiment" : {
"type":"string"
}
}
}
}
}
}'
# Index documents
curl -XPUT 'http://localhost:9200/survey/response/1' -d '{
"user" : "napoleon",
"impression" : [ { "topic" : "checkout", "sentiment" : "negative" }, { "topic" : "other", "sentiment" : "positive"}]
}'
curl -XPUT 'http://localhost:9200/survey/response/2' -d '{
"user" : "snowball",
"impression" : [ { "topic" : "colour", "sentiment" : "positive" }, { "topic" : "other", "sentiment" : "negative"}]
}'
curl -XPUT 'http://localhost:9200/survey/response/3' -d '{
"user" : "squealer",
"impression" : [ { "topic" : "size", "sentiment" : "positive" }, { "topic" : "other", "sentiment" : "positive"}]
}'
# This query works but produces wrong results because I forgot to set the field type to nested
curl -XGET 'http://localhost:9200/survey/response/_search' -d '{
"size" : 0,
"facets": {
"p": {
"terms": {
"field": "impression.topic",
"size": 10
}
}
},
"query": {
"bool": {
"must": [
{
"term": {
"impression.topic": "colour"
}
},
{
"term": {
"impression.sentiment": "negative"
}
}
]
}
}
}'
# Update mapping to add an identical field with nested type
curl -XPUT 'http://localhost:9200/survey/response/_mapping' -d '{
"response" : {
"_source" : {
"compression" : true
},
"properties" : {
"impression" : {
"type": "object",
"properties" : {
"topic" : {
"type":"string"
},
"sentiment" : {
"type":"string"
}
}
},
"nested_impression" : {
"type": "nested",
"properties" : {
"topic" : {
"type":"string"
},
"sentiment" : {
"type":"string"
}
}
}
}
}
}'
# Use the update API to copy the old field to the new field and re-index the document
curl -XPOST 'http://localhost:9200/survey/response/1/_update' -d '{
"script" : "ctx._source.nested_impression = ctx._source.impression"
}'
curl -XPOST 'http://localhost:9200/survey/response/2/_update' -d '{
"script" : "ctx._source.nested_impression = ctx._source.impression"
}'
curl -XPOST 'http://localhost:9200/survey/response/3/_update' -d '{
"script" : "ctx._source.nested_impression = ctx._source.impression"
}'
# This facet returns "missing" fields
curl -XGET 'http://localhost:9200/survey/response/_search' -d '{
"size" : 0,
"facets": {
"p": {
"terms": {
"field": "nested_impression.topic",
"size": 10
}
}
},
"query": {
"nested": {
"path": "nested_impression",
"query": {
"bool": {
"must": [
{
"term": {
"nested_impression.topic": "colour"
}
},
{
"term": {
"nested_impression.sentiment": "positive"
}
}
]
}
}
}
}
}'
# Yet the query returns the correct document.
curl -XGET 'http://localhost:9200/survey/response/_search' -d '{
"query": {
"nested": {
"path": "nested_impression",
"query": {
"bool": {
"must": [
{
"term": {
"nested_impression.topic": "colour"
}
},
{
"term": {
"nested_impression.sentiment": "positive"
}
}
]
}
}
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment