elasticsearch练习
1.简单查询练习 source: test003/doc
1.1 查询name中包含”li”的人,
GET test003/_search
{
"query":
{
"regexp":{"user":".*li.*"}
}
}
1.2 查询msg中含有birthday的数据,
GET test003/_search
{
"query":
{
"match":{"message":"birthday"}
}
}
1.3 查询city上海的,
GET /test003/_search
{
"query":
{
"match":{"city":"上海"}
}
}
1.4 查询name wangwu或lisi的,
GET test003/_search
{
"query":
{
"terms":{
"user":["lisi","wangwu"]
}
}
}
1.5 查询年龄大于35小于60的,同上/只显示name age city
GET test003/_search
{
"_source": ["name","age","city"],
"query":
{
"range": {
"age": {
"gt": 35,
"lt": 60
}
}
}
}
1.6 查询年龄大于30且city不在北京的,
GET test003/_search
{
"query":
{
"bool":
{
"must": [
{"range": {
"age": {
"gte": 30
}
}}
],
"must_not": [
{
"term": {
"city": "北京"
}
}
]
}
}
}
1.7 查询name不含”li”且age大于20,
GET test003/_search
{
"query":
{
"bool":
{
"must": [
{"range": {
"age": {
"gte": 20
}
}}
],
"must_not": [
{"regexp": {
"user": ".*li.*"
}}
]
}
}
}
2.聚合复合查询 source: /employees/employee_info
2.1 查询salary最高的员工,只显示name和salary,返回top3
GET employees/_search
{
"_source": ["name","salary"],
"size": 3,
"sort": [
{
"salary": {
"order": "desc"
}
}
],
"aggs": {
"max_salary": {
"max": {
"field": "salary"
}
}
}
}
2.2 在gender 为male中,查询统计salary各项数据,top3
GET employees/_search
{
"sort": [
{
"salary": {
"order": "desc"
}
}
],
"size": 3,
"aggs": {
"male":
{
"filter": {"term": {
"gender": "male"}},
"aggs": {
"stats_salary": {
"stats": {
"field": "salary"
}
}
}
}
}
}
2.3 查询不同岗位的职员的最高salary
GET employees/_search
{
"aggs": {
"job":
{
"terms": {"field": "job.keyword"},
"aggs": {
"stats_salary": {
"stats": {
"field": "salary"
}
}
}
}
}
}
2.4 查询age大于25或salary大于12000的的java程序员
GET employees/employee_info/_search
{
"query" :
{
"bool":
{
"must": [
{ "match": {
"job": "Java Programmer"
}},
{
"range": {
"age": {
"gte": 25
}
}
},
{
"bool":
{
"should": [
{
"range": {
"salary": {
"gt": 20000
}
}
}
]
}
}
]
}
}
}
2.5 查询salary在15000下,15000-30000,30000以上的female员工
GET employees/_search
{
"size": 3,
"sort": [
{
"salary": {
"order": "desc"
}
}
],
"aggs": {
"female_employee": {
"filter": {"term": {
"gender": "female"}},
"aggs":
{
"salary_range":
{
"range": {
"field": "salary",
"ranges": [
{
"to": 15000
},
{
"from": 15000,
"to":30000
},
{
"from": 30000
}
]
}
}
}
}
}
}
数据源
// 操作数据3-聚合操作
PUT /employees/employee_info/_bulk
{ "index" : { "_id" : "1" } }
{ "name" : "Emma","age":32,"job":"Product Manager","gender":"female","salary":35000 }
{ "index" : { "_id" : "2" } }
{ "name" : "Underwood","age":41,"job":"Dev Manager","gender":"male","salary": 50000}
{ "index" : { "_id" : "3" } }
{ "name" : "Tran","age":25,"job":"Web Designer","gender":"male","salary":18000 }
{ "index" : { "_id" : "4" } }
{ "name" : "Rivera","age":26,"job":"Web Designer","gender":"female","salary": 22000}
{ "index" : { "_id" : "5" } }
{ "name" : "Rose","age":25,"job":"QA","gender":"female","salary":18000 }
{ "index" : { "_id" : "6" } }
{ "name" : "Lucy","age":31,"job":"QA","gender":"female","salary": 25000}
{ "index" : { "_id" : "7" } }
{ "name" : "Byrd","age":27,"job":"QA","gender":"male","salary":20000 }
{ "index" : { "_id" : "8" } }
{ "name" : "Foster","age":27,"job":"Java Programmer","gender":"male","salary": 20000}
{ "index" : { "_id" : "9" } }
{ "name" : "Gregory","age":32,"job":"Java Programmer","gender":"male","salary":22000 }
{ "index" : { "_id" : "10" } }
{ "name" : "Bryant","age":20,"job":"Java Programmer","gender":"male","salary": 9000}
// 操作数据4-聚合操作之分组
POST _bulk
{"index":{"_index":"test003","_type":"doc"}}
{"user":"zhangsan", "age":30,"message":"happy birthday","city":"北京","location":{"lat":30,"lon":40}}
{"index":{"_index":"test003","_type":"doc"}}
{"user":"lisi", "age":30,"message":"happy birthday","city":"上海","location":{"lat":38.970718,"lon":116.325747}}
{"index":{"_index":"test003","_type":"doc"}}
{"user":"wangwu", "age":35,"message":"Happy birthday","city":"深圳","location":{"lat":37.970718,"lon":116.325747}}
{"index":{"_index":"test003","_type":"doc"}}
{"user":"zhaoliu", "age":40,"message":"birthday happy","city":"深圳","location":{"lat":36.970718,"lon":116.325747}}