Skip to content

Instantly share code, notes, and snippets.

@zwrss
Created March 15, 2013 11:29
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zwrss/5169241 to your computer and use it in GitHub Desktop.
Save zwrss/5169241 to your computer and use it in GitHub Desktop.
ElasticSearch tree structure search.
#!/bin/sh
echo
curl -XDELETE 'http://localhost:9200/products'
echo
curl -XPUT 'localhost:9200/products' -d '
{
"mappings" : {
"Root" : {
"dynamic" : false,
"properties" : {
"name" : { "type" : "string", "index" : "not_analyzed" }
}
},
"Child" : {
"_parent" : { "type" : "Root" },
"dynamic" : false,
"properties" : {
"name" : { "type" : "string", "index" : "not_analyzed" },
"price" : { "type" : "double", "index" : "not_analyzed" },
"characteristics" : {
"properties" : {
"char1" : { "type" : "string", "index" : "not_analyzed" },
"char2" : { "type" : "string", "index" : "not_analyzed" },
"char3" : { "type" : "string", "index" : "not_analyzed" },
"char4" : { "type" : "string", "index" : "not_analyzed" },
"char5" : { "type" : "string", "index" : "not_analyzed" },
"char6" : { "type" : "string", "index" : "not_analyzed" }
}
}
}
}
}
}
'
echo
curl -XPOST 'http://localhost:9200/products/Root/1' -d '
{
"name" : "Composite item"
}
'
echo
curl -XPOST 'http://localhost:9200/products/Child/1?parent=1' -d '
{
"name" : "BSimple item 1",
"price" : 100.0,
"characteristics" : { "char1" : "val1", "char2" : "val2" }
}
'
echo
curl -XPOST 'http://localhost:9200/products/Child/2?parent=1' -d '
{
"name" : "ASimple item 2",
"price" : 0,
"characteristics" : { "char2" : "val2", "char3" : "val8" }
}
'
echo
curl -XPOST 'http://localhost:9200/products/Child/3?parent=1' -d '
{
"name" : "BSimple item 3",
"price" : 0,
"characteristics" : { "char2" : "val2", "char4" : "val1" }
}
'
echo
curl -XPOST 'http://localhost:9200/products/Child/4?parent=1' -d '
{
"name" : "BSimple item 4",
"price" : 0,
"characteristics" : { "char3" : "val2", "char4" : "val2" }
}
'
echo
curl -XPOST 'http://localhost:9200/products/Root/2' -d '
{
"name" : "Composite item 2"
}
'
echo
curl -XPOST 'http://localhost:9200/products/Child/5?parent=2' -d '
{
"name" : "ASimple item 5",
"price" : 100.0,
"characteristics" : { "char1" : "val2", "char2" : "val3" }
}
'
echo
curl -XPOST 'http://localhost:9200/products/Child/6?parent=2' -d '
{
"name" : "BSimple item 6",
"price" : 0,
"characteristics" : { "char2" : "val1", "char3" : "val2" }
}
'
echo
curl -XPOST 'http://localhost:9200/products/Child/7?parent=2' -d '
{
"name" : "ASimple item 7",
"price" : 0,
"characteristics" : { "char2" : "val2", "char4" : "val4" }
}
'
echo
curl -XPOST 'http://localhost:9200/products/Child/8?parent=2' -d '
{
"name" : "CSimple item 8",
"price" : 0,
"characteristics" : { "char3" : "val2", "char4" : "val4" }
}
'
echo
#!/bin/sh
echo
curl -X POST 'localhost:9200/products/_search?pretty=true' -d '
{
"query" : {
"has_child" : {
"type" : "Child",
"query" : {
"prefix" : { "name" : "AS" }
}
}
}
}
'
echo
echo
curl -X POST 'localhost:9200/products/_search?pretty=true' -d '
{
"query" : {
"has_child" : {
"type" : "Child",
"query" : {
"prefix" : { "name" : "CS" }
}
}
}
}
'
echo
@hotrush
Copy link

hotrush commented Oct 30, 2014

Nice gist, thanks. How does aggregations works with this example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment