Skip to content

Instantly share code, notes, and snippets.

@karmi
Last active October 3, 2019 03:31
Show Gist options
  • Save karmi/5160251 to your computer and use it in GitHub Desktop.
Save karmi/5160251 to your computer and use it in GitHub Desktop.
A simple Elasticsearch example
# Let's delete the index first
curl -X DELETE localhost:9200/people
# Create an index with specific settings
curl -X POST localhost:9200/people -d '{
"settings" : {
"index" : { "number_of_shards" : 1, "number_of_replicas" : 0 }
}
}'
# Index documents...
curl -X PUT localhost:9200/people/person/1 -d '{
"first_name" : "John",
"last_name" : "Zorn",
"birthday" : "1953-09-02"
}'
curl -X PUT localhost:9200/people/person/2 -d '{
"first_name" : "John",
"last_name" : "Cage",
"birthday" : "1912-09-05"
}'
curl -X PUT localhost:9200/people/person/3 -d '{
"first_name" : "Mary",
"last_name" : "Shelley",
"birthday" : "1797-08-30"
}'
# Don't forget to refresh the index before searching it immediately!
curl -X POST localhost:9200/people/_refresh
# Search for people named "John"
curl -X GET localhost:9200/people/_search?pretty -d '{
"query" : {
"match" : {
"first_name" : "John"
}
}
}'
# Search for people born in the 20th century and sort them by birth date
curl -X GET localhost:9200/people/_search?pretty -d '{
"query" : {
"range" : {
"birthday" : {
"from" : "1900",
"to" : "2000"
}
}
},
"sort" : [
{ "birthday" : "asc" }
]
}'
# Search for people born in September
curl -X GET localhost:9200/people/_search?pretty -d '{
"query" : {
"filtered" : {
"query" : {
"match_all" : {}
},
"filter" : {
"script" : {
"script" : "new Date(doc.birthday.getValue()).getMonth() == month",
"params" : {
"month" : 8
}
}
}
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment