Last active
December 13, 2015 17:19
-
-
Save polyfractal/4947188 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
###Changes: | |
-Added "include_in_root" for each nested object. | |
-Removed the "nested" params from the last facet | |
This basically copies the nested doc into the root doc. You then reference the root "inner object" rather than the "nested object" to get the data. Be careful though, this breaks down if you have multiple nested docs that share the same name (e.g. array of nested), since the facet will operate on the entire array instead of individual ones. | |
See this thread for more info: https://groups.google.com/d/topic/elasticsearch/pjoNmosdCPs/discussion | |
##### Delete past attempts | |
curl -X DELETE localhost:9200/example | |
##### Create mapping with two nested documents (a 'thing' has 'foo' and 'bar') | |
curl -XPUT 'http://127.0.0.1:9200/example/?pretty=1' -d ' | |
{ | |
"mappings" : { | |
"thing" : { | |
"properties" : { | |
"name": { | |
"type": "string" | |
}, | |
"foo" : { | |
"type" : "nested", | |
"include_in_root": true, | |
"properties" : { "name": { "type": "string" }} | |
}, | |
"bar" : { | |
"type" : "nested", | |
"include_in_root": true, | |
"properties" : { "name": { "type": "string" }} | |
} | |
} | |
} | |
} | |
} | |
' | |
##### Add some data | |
curl -XPOST 'http://127.0.0.1:9200/example/thing?pretty=1' -d ' | |
{ | |
"name" : "My Thing", | |
"foo": {"name": "Foo"}, | |
"bar": {"name": "MyBar"} | |
} | |
' | |
curl -XPOST 'http://127.0.0.1:9200/example/thing?pretty=1' -d ' | |
{ | |
"name" : "Your Thing", | |
"foo": {"name": "Foo"}, | |
"bar": {"name": "YourBar"} | |
} | |
' | |
##### Query data | |
curl -XGET 'http://127.0.0.1:9200/example/thing/_search?pretty=1' -d ' | |
{ | |
"query" : { | |
"match" : { | |
"name": "Thing" | |
} | |
}, | |
"facets" : { | |
"foo_facet" : { | |
"terms" : { "field" : "foo.name" }, | |
"nested": "foo" | |
}, | |
"bar_facet" : { | |
"terms" : { "field" : "bar.name" }, | |
"nested": "bar" | |
}, | |
"bar_facet_where_bar_other" : { | |
"facet_filter": { "terms": { "bar.name": ["mybar"] } }, | |
"terms" : { "field" : "bar.name" } | |
"nested" : "bar" | |
}, | |
"foo_facet_where_bar_other" : { | |
"facet_filter": { "terms": { "bar.name": ["mybar"] } }, | |
"terms" : { "field" : "foo.name" } | |
} | |
} | |
} | |
' | |
#### Search results | |
{ | |
"took" : 2, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 5, | |
"successful" : 5, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : 2, | |
"max_score" : 0.8784157, | |
"hits" : [ { | |
"_index" : "example", | |
"_type" : "thing", | |
"_id" : "_ty1yk_gQ0eAyZl7mqxP-w", | |
"_score" : 0.8784157, "_source" : | |
{ | |
"name" : "My Thing", | |
"foo": {"name": "Foo"}, | |
"bar": {"name": "MyBar"} | |
} | |
}, { | |
"_index" : "example", | |
"_type" : "thing", | |
"_id" : "i1AokusmRdWyeaVQpOL9KQ", | |
"_score" : 0.8784157, "_source" : | |
{ | |
"name" : "Your Thing", | |
"foo": {"name": "Foo"}, | |
"bar": {"name": "YourBar"} | |
} | |
} ] | |
}, | |
"facets" : { | |
"foo_facet" : { | |
"_type" : "terms", | |
"missing" : 0, | |
"total" : 2, | |
"other" : 0, | |
"terms" : [ { | |
"term" : "foo", | |
"count" : 2 | |
} ] | |
}, | |
"bar_facet" : { | |
"_type" : "terms", | |
"missing" : 0, | |
"total" : 2, | |
"other" : 0, | |
"terms" : [ { | |
"term" : "yourbar", | |
"count" : 1 | |
}, { | |
"term" : "mybar", | |
"count" : 1 | |
} ] | |
}, | |
"bar_facet_where_bar_other" : { | |
"_type" : "terms", | |
"missing" : 0, | |
"total" : 1, | |
"other" : 0, | |
"terms" : [ { | |
"term" : "mybar", | |
"count" : 1 | |
} ] | |
}, | |
"foo_facet_where_bar_other" : { | |
"_type" : "terms", | |
"missing" : 0, | |
"total" : 1, | |
"other" : 0, | |
"terms" : [ { | |
"term" : "foo", | |
"count" : 1 | |
} ] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment