Skip to content

Instantly share code, notes, and snippets.

@chrisyuska
Forked from lukas-vlcek/gist:3739469
Created September 17, 2012 20:50
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 chrisyuska/3739681 to your computer and use it in GitHub Desktop.
Save chrisyuska/3739681 to your computer and use it in GitHub Desktop.
#!/bin/sh
echo "\n --- delete index"
curl -X DELETE 'http://localhost:9200/'
echo "\n --- create index and put mapping into place"
curl -X POST 'http://localhost:9200/myindex/' -d '{
"mappings" : {
"person" : {
"properties" : {
"name" : {"type" : "string"},
"works" : {
"type" : "nested",
"include_in_parent" : false,
"properties" : {
"title" : {"type" : "string"},
"current" : {"type" : "boolean"},
"dummy" : {"type" : "string"}
}
}
}
}
},
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
}
}'
echo "\n --- index data"
curl -X PUT 'http://localhost:9200/myindex/person/1' -d '
{
"name" : "Lukas",
"works" : [
{
"title" : "developer",
"current" : true,
"dummy" : "match"
},
{
"title" : "dad",
"current" : true
},
{
"title" : "husband",
"current" : true,
"dummy" : "foo"
},
{
"title" : "brother",
"current" : true,
"dummy" : "bar"
}
]
}'
curl -X PUT 'http://localhost:9200/myindex/person/2' -d '
{
"name" : "Karel",
"works" : [
{
"title" : "developer",
"current" : true,
"dummy" : "match"
}
]
}'
curl -X PUT 'http://localhost:9200/myindex/person/3' -d '
{
"name" : "Jan",
"works" : [
{
"title" : "developer",
"current" : false,
"dummy" : "match"
}
]
}'
echo "\n --- optimize"
curl -X POST 'http://localhost:9200/_optimize'
#!/bin/sh
echo "\n --- query"
curl -X GET 'http://localhost:9200/_search?pretty=true' -d '
{
"query" : {
"nested" : {
"path" : "works",
"query" : {
"bool" : {
"should" : [
{ "text" : { "name" : "developer"} },
{ "text" : { "works.title" : "developer"} },
{ "text" : { "works.current" : true } }
]
}
}
}
},
"fields" : [
"name"
]
}'
echo "\n --- done"
# ===========================================
# Output
# ===========================================
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.89047337,
"hits" : [ {
"_index" : "myindex",
"_type" : "person",
"_id" : "2",
"_score" : 0.89047337,
"fields" : {
"name" : "Karel"
}
}, {
"_index" : "myindex",
"_type" : "person",
"_id" : "1",
"_score" : 0.34814507,
"fields" : {
"name" : "Lukas"
}
}, {
"_index" : "myindex",
"_type" : "person",
"_id" : "3",
"_score" : 0.27786776,
"fields" : {
"name" : "Jan"
}
} ]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment