1.启动es后执行查询
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/_count?pretty' -d ' { "query": { "match_all": {} } } '
2.创建雇员信息
curl -H "Content-Type: application/json" -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 -H "Content-Type: application/json" -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 -H "Content-Type: application/json" -XPUT 'http://localhost:9200/megacorp/employee/3' -d '
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}
'
3.检索指定文档
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/1' curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/2' curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/3'
4.检索全部文档
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search'
返回结果包括了所有三个文档,放在数组 hits 中。一个搜索默认返回十条结果。
5.添加参数搜索
curl -X GET "localhost:9200/megacorp/employee/_search?q=last_name:Smith"
q=后边为参数名 : value
6.使用表达式查询
curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d' { "query" : { "match" : { "last_name" : "Smith" } } } '
7.更复杂的查询:年龄>30且最后一个名字为smith的
filter作为一个范围查询
curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
"query" : {
"bool": {
"must": {
"match" : {
"last_name" : "smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}
'
8.全文搜索
搜索下所有喜欢攀岩(rock climbing)的雇员:
curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
"query" : {
"match" : {
"about" : "rock climbing"
}
}
}
'
9.短语查询
仅匹配同时包含 “rock” 和 “climbing” ,并且以短语 “rock climbing” 的形式紧挨着的雇员记录。
使用一个叫做 match_phrase 的查询:
curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
'
10.聚合
聚合前需要先执行操作一下以下代码:
因为:5.x后对排序,聚合这些操作用单独的数据结构(fielddata)缓存到内存里了,text需要单独开启,
https://blog.youkuaiyun.com/u011403655/article/details/71107415
https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html
curl -X PUT "localhost:9200/megacorp/_mapping/employee/" -H 'Content-Type: application/json' -d'
{
"properties": {
"interests": {
"type": "text",
"fielddata": true
}
}
}
'
curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d' { "aggs": { "all_interests": { "terms": { "field": "interests" } } } } '
11.聚合的分级汇总
查询特定兴趣爱好员工的平均年龄:
curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
"aggs" : {
"all_interests" : {
"terms" : { "field" : "interests" },
"aggs" : {
"avg_age" : {
"avg" : { "field" : "age" }
}
}
}
}
}
'