Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Query parents and count children
#cleanup first
curl -XDELETE localhost:9200/test/
echo
#create index
curl -XPUT localhost:9200/test
echo
#create parent type
curl -XPUT localhost:9200/test/test_parent
echo
#put a couple of parents
curl -XPUT localhost:9200/test/test_parent/1 -d '{"foo": "bar"}'
curl -XPUT localhost:9200/test/test_parent/2 -d '{"foo": "baz"}'
curl -XPUT localhost:9200/test/test_parent/3 -d '{"foo": "bar baz"}'
echo
#put the child mapping
curl -XPUT localhost:9200/test/test_child/_mapping -d '{
"test_child" : {
"_parent" : {
"type" : "test_parent"
}
}
}'
echo
#put a few children for each parent
for i in {1..3}; do
curl -XPOST localhost:9200/test/test_child?parent=1 -d '{"foo-child": "bar child"}'
echo
done
for i in {1..5}; do
curl -XPOST localhost:9200/test/test_child?parent=2 -d '{"foo-child": "bar child2"}'
echo
done
for i in {1..8}; do
curl -XPOST localhost:9200/test/test_child?parent=3 -d '{"foo-child": "bar child3"}'
echo
done
#refresh
curl -XPOST localhost:9200/test/_refresh
echo
#look for parents that contain "bar"
#and return their number of children
curl -XPOST localhost:9200/test/test_child/_search?pretty=true -d '{
"query": {
"has_parent": {
"parent_type": "test_parent",
"query": {
"term": {
"foo": "bar"
}
}
}
},
"facets": {
"test_facet": {
"terms": {
"field": "_parent"
}
}
}
}'
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment