elasticsearch教程

es服务地址:192.168.149.129:9200
1. 获取集群状态
curl  -XGET "192.168.149.129:9200/_cat/health?v&pretty" 
2.获取索引
curl -XGET "192.168.149.129:9200/_cat/indices?v&pretty"

在这里插入图片描述

3.创建索引
curl -H "Content-Type: application/json" -XPOST '192.168.149.129:9200/bank/account/_bulk?pretty&refresh' --data-binary "@account.json"

account.json文本如下:
在这里插入图片描述
说明:bank为索引名,account为类型,_bulk表示批量导入,refresh为立即生效,@account.json为安装目录下的json数据文件(即将该数据文件添加到bank索引中)
注:-H这部分不加的话,可能会报错,加上之后报错消失
在这里插入图片描述
注意:accounts.json每行必须以\n换行。如果提示The bulk request must be terminated by a newline [\n],请检查最后一行是否以\n换行。
下图表示数据添加成功:
在这里插入图片描述
查询索引,发现bank已添加:
在这里插入图片描述

4.搜索 search API

1)REST请求URI发送搜索参数

 curl -XGET '192.168.149.129:9200/bank/_search?q=*&sort=account_number:asc&pretty'
注:bank为索引名,q=*表示查询所有的,sort=account_number表示按照account_number进行排序,asc表示升序

在这里插入图片描述

2)REST请求主体发送搜索参数

curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d' 
{
    "query":{"match_all":{}}, #匹配索引下面所有的文档
    "sort":[{"account_number":"asc"}] #查询结果按照account_number字段进行升序排列
}'
注:查询语句可以不用换行,换行是为了显示清晰,也可以直接写成:
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
 { "query":{ "match_all":{} },
   "sort":[{"account_number":"asc"}] }'

在这里插入图片描述
查询示例:

a. 指定显示结果数:"size":n
 #显示20条
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
{ "query":{"match_all":{}}, 
  "sort":[{"account_number":"asc"}], 
  "size":20 }'

b. 指定显示范围:"from":n #从第n个开始
#从第10个开始,显示20条(10~29)
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
 { "query":{"match_all":{}}, 
   "sort":[{"account_number":"asc"}], 
   "size":20, 
   "from":10 }' 

c. 排序:按照balance降序排
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
 { "query":{"match_all":{}}, 
   "sort":[{"balance":"desc"}]}'
或者:
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
 { "query":{"match_all":{}}, 
   "sort":{"balance":{"order":"desc"}} }'

d. 指定显示的字段:"_source":["field1","field2"]
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
 { "query":{"match_all":{}}, 
   "sort":[{"balance":"desc"}], 
   "_source":["balance","firstname","account_number"] }'

e. match和match_phrase
#匹配account_number为20的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
 { "query": 
       {"match":{"account_number":20}} 
 }'  

#匹配地址中包含mill(不区分大小写)的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
 { "query": 
      {"match":{"address":"mill"}}, 
   "_source":["address"] }'  

#匹配地址中包含mill或者lane(不区分大小写)的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
 { "query": 
      { "match":{"address":"mill lane"} }, 
   "_source":["address"] }'   

 #匹配地址中包含"mill lane" 的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d' 
{ "query":
      { "match_phrase":{"address":"mill lane"} }, 
  "_source":["address"] }'  

f. bool查询 -must #与的关系,同时匹配
 #匹配地址中包含"mill lane" 的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d' 
{ "query":
     {"bool":
          {"must":
                [ {"match":{"address":"mill"}},
                  {"match":{"address":"lane"}}
                ]
          }
     },
  "_source":["address"] }'  
 
g. bool查询 -should #或的关系,匹配任意一个
#匹配地址包含mill或者lane的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d' 
{ "query": 
     {"bool":
         {"should":
            [ {"match":{"address":"mill"}},
              {"match":{"address":"lane"}}
            ]
         }
      },
  "_source":["address"] }' 

h. bool查询 - must_not #既不包含A也不包含B
#匹配地址既不包含mill,也不包含lane
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d' 
{ "query":
      {"bool": 
           {"must_not":
                      [ {"match":{"address":"mill"}}, 
                        {"match":{"address":"lane"}}
                      ] 
            } 
      }, 
 "_source":["address"] }' 

i. bool查询- 组合使用
#匹配地址包含mill,但是年龄不是38的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d' 
{ "query":
     {"bool":
         {"must":[{"match":{"address":"mill"}}],
          "must_not":[{"match":{"age":38}}]
         }
     },
  "_source":["address","age"] }' 

j. 过滤器和聚合
#匹配地址包含mill,且年龄在20~30之间的
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d' {"query":
      {"bool":
          { "must":{"match":{"address":"mill"}},
           "filter":{"range":{"age":{"gte":20,"lte":30}}}
          }
      }, 
 "_source":["address","age"] }' 

k. 聚合演示 - 按照state分组,count递减排序
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d'
 { "size":0,
   "aggs":
        {"group_by_state":
            {"terms":{"field":"state.keyword"}}
        } 
  }'  #默认递减

l. 聚合演示 - 按state计算平均账户余额
curl -H "Content-Type: application/json" -XGET '192.168.149.129:9200/bank/_search?pretty' -d' 
{
 "size":0, 
 "aggs":{ 
      "group_by_state": 
             { "terms": {"field":"state.keyword"}, 
               "aggs":{"average_balance":{"avg":{"field":"balance"}}}
             }
        }
}'

5. 删除索引

curl -XDELETE 127.0.0.1:9200/index_name1,index_name2,index_name3…
删除多个索引,索引之间用逗号

也支持通配符,如:
curl -XDELETE 127.0.0.1:9200/index_*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jepson2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值