您的当前位置:首页正文

elasticsearch练习

来源:帮我找美食网
elasticsearch练习

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

1.5 查询年龄⼤于35⼩于60的,同上/只显⽰name age city

GET test003/_search{

\"_source\": [\"name\ \"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\ \"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\{ \"index\" : { \"_id\" : \"2\" } }

{ \"name\" : \"Underwood\{ \"index\" : { \"_id\" : \"3\" } }

{ \"name\" : \"Tran\{ \"index\" : { \"_id\" : \"4\" } }

{ \"name\" : \"Rivera\{ \"index\" : { \"_id\" : \"5\" } }

{ \"name\" : \"Rose\{ \"index\" : { \"_id\" : \"6\" } }

{ \"name\" : \"Lucy\{ \"index\" : { \"_id\" : \"7\" } }

{ \"name\" : \"Byrd\{ \"index\" : { \"_id\" : \"8\" } }

{ \"name\" : \"Foster\{ \"index\" : { \"_id\" : \"9\" } }

{ \"name\" : \"Gregory\{ \"index\" : { \"_id\" : \"10\" } }

{ \"name\" : \"Bryant\``````

// 操作数据4-聚合操作之分组POST _bulk

{\"index\":{\"_index\":\"test003\

{\"user\":\"zhangsan\北京\{\"index\":{\"_index\":\"test003\

{\"user\":\"lisi\上海\{\"index\":{\"_index\":\"test003\

{\"user\":\"wangwu\深圳\{\"index\":{\"_index\":\"test003\

{\"user\":\"zhaoliu\深圳\

因篇幅问题不能全部显示,请点此查看更多更全内容

Top