Skip to content

Instantly share code, notes, and snippets.

@obonyojimmy
Forked from radu-gheorghe/count_children.sh
Created December 2, 2018 02:15
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 obonyojimmy/695161045958054ed1a6cc8f5b191c1f to your computer and use it in GitHub Desktop.
Save obonyojimmy/695161045958054ed1a6cc8f5b191c1f to your computer and use it in GitHub Desktop.
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