Skip to content

Instantly share code, notes, and snippets.

@dadoonet
Forked from saiglen/gist:ad8b89836547c7e0ee60
Created July 16, 2014 11:39
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 dadoonet/64458f7423863d93c49e to your computer and use it in GitHub Desktop.
Save dadoonet/64458f7423863d93c49e to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
echo "\n Delete old index"
curl -X DELETE 'http://localhost:9200/bands'
echo "\n Create Index Mapping"
curl -X POST 'http://localhost:9200/bands/' -d '{
"mappings" : {
"documents" : {
"properties" : {
"title" : {
"type" : "string"
},
"year" : {
"type" : "integer"
},
"xref" : {
"type" : "nested",
"properties" : {
"name" : {
"type" : "string"
}
}
}
}
}
}
}'
echo "\n Verify Mapping"
curl 'http://localhost:9200/_mapping?pretty'
echo "\nAdd Pink Floyd"
curl -X PUT 'http://localhost:9200/bands/documents/1' -d '
{
"title" : "Pink Floyd",
"year" : "1965",
"xref" : [
{
"name" : "Syd Barrett"
},
{
"name" : "Roger Waters"
},
{
"name" : "David Gilmour"
},
{
"name" : "Nick Mason"
},
{
"name" : "Roger Waters"
}
]
}'
echo "\n Add Queen"
curl -X PUT 'http://localhost:9200/bands/documents/2' -d '
{
"title" : "Queen",
"year" : "1970",
"xref" : [
{
"name" : "Freddie Mercury"
},
{
"name" : "Brian May"
},
{
"name" : "John Deacon"
},
{
"name" : "Roger Taylor"
}
]
}'
echo "\n Refresh Index"
curl -XGET "http://localhost:9200/bands/_refresh"
echo "\n XREF Search 'Roger' - (2 Results)"
curl -XGET "http://localhost:9200/_search?pretty=true" -d '
{
"query" : {
"nested" : {
"path" : "xref",
"query" : {
"bool" : {
"must" : [
{ "match" : { "xref.name" : "Roger" } }
]
}
}
}
},
"fields" : [
"title"
]
}'
curl -XGET "http://localhost:9200/bands/_search?pretty=true" -d'
{
"query": {
"filtered": {
"query": {
"nested": {
"path": "xref",
"query": {
"bool": {
"must": [
{
"match": {
"xref.name": "Roger"
}
}
]
}
}
}
},
"filter": {
"range": {
"year": {
"from": 1970,
"to": 1980
}
}
}
}
},
"fields": [
"title"
]
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment