elasticsearch教程 - Go语言中文社区

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_*

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/d1240673769/article/details/93621158
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢