參考elasticsearch權威指南中文版
curl -XPUT 'http://localhost:9200/megacorp/employee/1' -d '{
"first_name":"John","last_name":"Smith",
"age":25,
"about":"I love to go rock climbing",
"interests": ["sports","music"]
}'
curl -XPUT 'http://localhost:9200/megacorp/employee/2' -d '{
"first_name":"Jane",
"last_name":"Smith",
"age":32,
"about":"I like to collect rock albums",
"interests": ["music"]
}'
curl -XPUT 'http://localhost:9200/megacorp/employee/3' -d '{
"first_name":"Chuanjian",
"last_name":"Liang",
"age":35,
"about":"I like to build cabinets",
"interests": ["forestry"]
}'
curl -XPUT 'http://localhost:9200/megacorp/employee/4' -d '
{
"first_name":"Chuanjian",
"last_name":"Liang",
"age":25,
"about":"I like to singing",
"interests": ["travel"]
}
'
curl -XPUT 'http://localhost:9200/megacorp/employee/4' -d '
{
"first_name":"Xiao",
"last_name":"Hu",
"age":25,
"about":"I like to singing",
"interests": ["travel"]
}
'
curl -XGET 'http://localhost:9200/megacorp/employee/1'
curl -XGET 'http://localhost:9200/megacorp/employee/_search'
//search all employees
curl -XDELETE 'http://localhost:9200/megacorp/employee/1'
curl -XDELETE 'http://localhost:9200/megacorp/employee/1?pretty'
curl -XGET 'http://localhost:9200/megacorp/employee/_search' -d '{
"query":{
"match":{
"last_name":"Smith"
}
}
}'
//range search
curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d '{
"query":{
"filtered":{
"filter":{
"range":{
"age":{"gt":30}
}
}
}
}
}'
curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d '{
"query":{
"filtered":{
"filter":{"range":{"age":{"gt":30}}},
"query":{"match":{"last_name":"Smith"}}
}
}
}'
curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d'{
"query":{
"match":{
"about":"rock climbing"
}
}
}'
curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d'{
"query":{
"match_phrase":{
"about":"rock climbing"
}
}
}'
//highlighting
curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d'{
"query":{"match_phrase":{"about":"rock climbing"}},
"highlight":{"fields":{"about":{}}}
}'
//aggregations
curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d'{
"aggs":{"all_interests":{"terms":{"field":"interests"}}}
}'
curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d'{
"query":{"match":{"last_name":"Smith"}},
"aggs":{"all_interests":{"terms":{"field":"interests"}}}
}'
curl -XGET 'http://localhost:9200/megacorp/employee/_search?pretty' -d'{
"aggs":{"all_interests":{
"terms":{"field":"interests"},
"aggs":{"avg_age":{"avg":{"field":"age"}}}
}}
}'