elasticsearch练习

最近在学习elasticsearch,做了一些练习,分享下练习成果,es基于6.7.2,用kibana处理DSL,有兴趣的伙伴可以自己试试

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}}

最新文章

  1. Android—基于GifView显示gif动态图片
  2. RecyclerView的使用(四)
  3. bzoj4409&&bzoj4410&&bzoj4411[Usaco2016 Feb Platinum]题解
  4. 数据库的模糊查询mybatis
  5. Redhat系列Linux的基础命令
  6. mysql分库分表
  7. 教你如何用PS制作多款按钮UI设计教程
  8. 【STL】-list的用法
  9. 分布式MySQL 数据库
  10. 标准I/O的替代软件
  11. 可供VC调用的QT编写的界面DLL方法
  12. JSP +++SERVIET总复习
  13. php 下载文件
  14. Linux - 简明Shell编程14 - 操作符(Operator)
  15. 邓_ ThinkPhp框架
  16. 50行ruby代码开发一个区块链
  17. windows2012服务器中安装php7+mysql5.7+apache2.4环境
  18. 比MySQL快6倍 深度解析国内首个云原生数据库POLARDB的“王者荣耀”
  19. Zabbix监控文件是否存在/文件大小
  20. Thread类的常用方法

热门文章

  1. xss构造--如何使用xss语句
  2. IDEA使用maven搭建SSM框架整合项目(超级详细,值得一看)
  3. Webservice报错客户端发现响应内容类型为“application/json;charset=UTF-8”,但应为“text/xml”。
  4. C#开发PACS医学影像处理系统(十六):2D处理之影像平移和缩放
  5. hystrix总结之缓存
  6. RXJAVA之概述
  7. Java基础一篇过(八)常见异常速查
  8. vulnhub-Os-hackNos-2
  9. web自动化整理
  10. Laver 文件版本遍历器