elasticsearch:基础练习

首先搭建好elasticsearch集群,我这里搭建3台,伪分布式。

新建一个名为es1的索引库:

curl -XPUT http://192.168.1.28:9200/es1/

则出现以下:

lunce   5个主片:

其他几个是从片,主从绝对不会在一个节点上,比如1和1  2和2  主从都不在一个节点。 

 

建“表”:

#document:
curl -XPOST http://192.168.1.28:9200/es1/employee -d '
{
 "first_name" : "bin",
 "age" : 33,
 "about" : "I love to go rock climbing",
 "interests": [ "sports", "music" ]
}'

创建失败:

创建成功:

document id自动生成。

查看:

 

再创建一条:

 

#add field yes 

再添加:

增加一个属性

curl -XPOST http://192.168.1.28:9200/es1/employee -d '
{
 "first_name" : "pablo2",
 "age" : 33,
 "about" : "I love to go rock climbing",
 "interests": [ "sports", "music" ],
 "sex": "man"
}'

支持列的动态增长:

 

curl -XPOST http://192.168.1.28:9200/es1/employee/1 -d '
{
 "first_name" : "pablo2",
 "age" : 35,
 "about" : "I love to go rock climbing",
 "interests": [ "sports", "music" ],
 "sex": "man"
}'

 

#put:

curl -XPUT http://192.168.1.28:9200/es1/employee/1 -d '
{
 "first_name" : "god bin",
 "last_name" : "pang",
 "age" : 42,
 "about" : "I love to go rock climbing",
 "interests": [ "sports", "music" ]
}'

如果没有指定id(上面指定为1)的话:

 

指定为1的话我们把年龄42给改为45:

curl -XPUT http://192.168.1.28:9200/es1/employee -d '
{
 "first_name" : "god bin",
 "last_name" : "bin",
 "age" : 45,
 "about" : "I love to go rock climbing",
 "interests": [ "sports", "music" ]
}'

说明post和put都可以做创建和修改。put是必须要我们自定义指定id的,post则自动生成,如果post给定了id则对这个id做相应的修改。

 

#根据document的id来获取数据:(without pretty)


curl -XGET http://192.168.1.28:9200/es1/employee/1?pretty

这是根据id来查找的,并不是经过倒排索引来查找的。


#根据field来查询数据:


curl -XGET http://192.168.1.28:9200/es1/employee/_search?q=first_name="bin"

 

god 都返回来了 ,说明做了倒排索引了。

 

#根据field来查询数据:match


curl -XGET http://192.168.1.28:9200/es1/employee/_search?pretty -d '
{
 "query":
  {"match":
   {"first_name":"bin"}
  }
}'

 

#对多个field发起查询:multi_match


curl -XGET http://192.168.1.28:9200/es1/employee/_search?pretty -d '
{
 "query":
  {"multi_match":
   {
    "query":"bin",
    "fields":["last_name","first_name"],
    "operator":"and"
   }
  }
}'

 

 

 

目前测试数据为:

#多个term对多个field发起查询:bool(boolean) 
# 组合查询,must,must_not,should 
#  must + must : 交集
#  must +must_not :差集
#  should+should  : 并集

curl -XGET http://192.168.1.28:9200/es1/employee/_search?pretty -d '
{
 "query":
  {"bool" :
   {
    "must" : 
     {"match":
      {"first_name":"bin"}  //first_name这个filed必须含有bin
     },
    "must" : 
     {"match":
      {"age":33}  //age这个filed必须等于33岁
     }
   }
  }
}'

 

curl -XGET http://192.168.1.28:9200/es1/employee/_search?pretty -d '
{
 "query":
  {"bool" :
   {
    "must" : 
     {"match":
      {"first_name":"bin"}//必须含有bin
     },
    "must_not" : 
     {"match":
      {"age":33}//必须不等于33岁
     }
   }
  }
}'

 


curl -XGET http://192.168.1.28:9200/es1/employee/_search?pretty -d '
{
 "query":
  {"bool" :
   {
    "must_not" : 
     {"match":
      {"first_name":"bin"}
     },
    "must_not" : 
     {"match":
      {"age":33}
     }
   }
  }
}'

 

 

 


##查询first_name=bin的,或者年龄在20岁到33岁之间的

 

curl -XGET http://192.168.1.28:9200/es1/employee/_search -d '
{
 "query":
  {"bool" :
   {
   "must" :
    {"term" : 
     { "first_name" : "bin" }
    }
   ,
   "must_not" : 
    {"range":
     {"age" : { "from" : 20, "to" : 33 }
    }
   }
   }
  }
}'

 

 

#修改配置
curl -XPUT 'http://192.168.1.28:9200/test2/' -d'{"settings":{"number_of_replicas":2}}'  //一个主对应两个从

curl -XPUT 'http://192.168.1.28:9200/test3' -d'{"settings":{"number_of_shards":3,"number_of_replicas":3}}'  // 不能有3个从 因为总共就3个节点

curl -XPUT 'http://192.168.1.28:9200/test4/' -d'{"settings":{"number_of_shards":6,"number_of_replicas":4}}' //不能有4个节点  

布置主从节点的时候要有一定的考量,按实际需求来。

elasticsearch unassigned错误解决

https://blog.csdn.net/wer724853863/article/details/78533105

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值