Created
December 19, 2012 21:23
-
-
Save martijnvg/4340646 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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?pretty' -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" | |
}' | |
curl -XGET 'localhost:9200/_refresh' | |
# This facet returns "missing" fields | |
curl -XGET 'http://localhost:9200/survey/response/_search?pretty' -d '{ | |
"size" : 0, | |
"facets": { | |
"p": { | |
"terms": { | |
"field": "nested_impression.topic", | |
"size": 10 | |
}, | |
"nested" : "nested_impression" | |
} | |
}, | |
"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?pretty' -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