Rest:Representational State Transfer
document文档数据导入到es集群
put更新
主从(粗细)散列分布,绝不能放在1个节点上
file
segment(段,多个document组成)
document(一条记录,一个对象实例)
field(对象的属性)
term(项,分词之后的词条)
CURL命令
在命令行下访问url
的一个工具(理解为浏览器)
可以简单实现常见的get/pos
t请求
-X
指定http请求的方法:HEAD
,GET
,POST
,PUT
,DELETE
-d
指定要传输的数据
PUT
和POST
都可做创建和修改更新
,就看id
是否存在
# 建立索引库
curl -XPUT http://192.168.118.102:9200/appke/
# 删除索引库
curl -XDELETE http://192.168.118.102:9200/test2/
# 创建document,添加数据自动创建id
curl -XPOST http://192.168.118.102:9200/appke/employee -d '
{
"first_name" : "bin",
"age" : 33,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music"]
}'
curl -XPOST http://192.168.118.102:9200/appke/employee -d '
{
"first_name" : "bin guo",
"age" : 47,
"about" : "I love to fitness",
"interests": [ "fishing", "music"]
}'
curl -XPOST http://192.168.118.102:9200/appke/employee/2 -d '
{
"first_name" : "bin",
"age" : 45,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}'
# 添加属性,动态类的增值
curl -XPOST http://192.168.118.102:9200/appke/employee -d '
{
"first_name" : "pablo2",
"age" : 33,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ],
"sex": "man"
}'
# 指定id
curl -XPOST http://192.168.118.102:9200/appke/employee/222 -d '
{
"first_name" : "pablo2",
"age" : 35,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ],
"sex": "man"
}'
# 更新,下面都有问题
# appke/employee/1 1是id,随便的,相当于增加
curl -XPUT http://192.168.118.102:9200/appke/employee/1 -d '
{
"first_name" : "god bin",
"last_name" : "pang",
"age" : 42,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}'
# 错误❌ 找不到句柄,参数没给全
curl -XPUT http://192.168.118.102:9200/appke/employee -d '
{
"first_name" : "god bin",
"last_name" : "bin",
"age" : 45,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}'
# 修改更新
curl -XPUT http://192.168.118.102:9200/appke/employee/1 -d '
{
"first_name" : "god bin",
"last_name" : "pang",
"age" : 40,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}'
document
获取数据
#根据document的id来获取数据:(?pretty有缩进)
curl -XGET http://192.168.118.102:9200/appke/employee/1?pretty
#根据field来查询数据:
curl -XGET http://192.168.118.102:9200/appke/employee/_search?q=first_name="bin"
# 根据field来查询数据:match,查询条件封装!
curl -XGET http://192.168.118.102:9200/appke/employee/_search?pretty -d '
{
"query":
{"match":
{"first_name":"bin"}
}
}'
#对多个field发起查询:multi_match,匹配多个字段
curl -XGET http://192.168.118.102:9200/appke/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.118.102:9200/appke/employee/_search?pretty -d '
{
"query":
{"bool" :
{
"must" :
{"match":
{"first_name":"bin"}
},
"must" :
{"match":
{"age":33}
}
}
}
}'
curl -XGET http://192.168.118.102:9200/appke/employee/_search?pretty -d '
{
"query":
{"bool" :
{
"must" :
{"match":
{"first_name":"bin"}
},
"must_not" :
{"match":
{"age":33}
}
}
}
}'
curl -XGET http://192.168.118.102:9200/appke/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.118.102:9200/appke/employee/_search -d '
{
"query":
{"bool" :
{
"must" :
{"term" :
{ "first_name" : "bin" }
}
,
"must_not" :
{"range":
{"age" : { "from" : 20, "to" : 33 }
}
}
}
}
}'
修改配置
# 2个从,主默认5个 5x(1+2)=15
curl -XPUT 'http://192.168.118.102:9200/test2/' -d'{"settings":{"number_of_replicas":2}}'
# 3个主,3个从 ,3x(1+3)=12,总共只有3个节点,最多只能有2个从,即3x(1+2)=9
curl -XPUT 'http://192.168.118.102:9200/test3/' -d'{"settings":{"number_of_shards":3,"number_of_replicas":3}}'
curl -XPUT 'http://192.168.118.102:9200/test4/' -d'{"settings":{"number_of_shards":6,"number_of_replicas":4}}'
curl -XPOST http://192.168.9.11:9200/appke/person/_mapping -d'
{
"person": {
"properties": {
"content": {
"type": "string",
"store": "no",
"term_vector": "with_positions_offsets",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"include_in_all": "true",
"boost": 8
}
}
}
}'
REST
的操作分为以下几种
-
GET
:获取对象的当前状态; -
PUT
:改变对象的状态; -
POST
:创建对象; -
DELETE
:删除对象; -
HEAD
:获取头信息