ElasticSearch学习(一)

ElasticSearch学习:
简介:是一个基于Lucene的实时的分布式搜索和分析引擎。设计与云计算中,能够达到实时搜索,稳定,可靠,快速安装使用方便,是基于RESultful接口。
天生支持分布式
ES VS Mysql

1.elasticSearch的安装和使用:
1.从官网上下载下来就可以使用。解压没有任何配置需要修改。开箱即用
bin/elasticSearch
2.验证是否启动成功:
IP:9200
后台启动
bin/elasticSearch -d
elasticSearch没有停止和启动的命令。需要下载elasticsearch-servicewrapper-master/ 这个插件,githib中,然后将server目录copy到ES的bin目录下即可

安装成功:
{
“name” : “crvuzb4”,
“cluster_name” : “elasticsearch”,
“cluster_uuid” : “cutbX2mZTEuehZ06PgslLg”,
“version” : {
“number” : “6.6.2”,
“build_flavor” : “default”,
“build_type” : “tar”,
“build_hash” : “3bd3e59”,
“build_date” : “2019-03-06T15:16:26.864148Z”,
“build_snapshot” : false,
“lucene_version” : “7.6.0”,
“minimum_wire_compatibility_version” : “5.6.0”,
“minimum_index_compatibility_version” : “5.0.0”
},
“tagline” : “You Know, for Search”
}

curl 命令简介:
-x 请求接口的方式。 Post Get put delete head
-d 需要带过去的参数
curl -XGET ‘localhost:9200’
1.使用curl在ES上创建索引库:
curl -XPUT ‘localhost:9200/test’
返回:{“acknowlwdged”:true}
2.使用curl在ES中的test索引上创建一个类型:

curl -XPUT ‘localhost:9200/test/emp/1’-d ‘{“name”:”张三”,”age”:10}’

表示在test索引上创建一个emp的类型。这个类型中有一个id 为1 的 员工的数据

使用以上这种方式请求ES会报错的,应该加上请求的头:
-H "Content-Type: application/json”

完整的请求应该是:curl -XPUT ‘localhost:9200/test/emp/1’ -H “Content-Type: application/json” -d’{“name”:“zhangsan”,“age”:“20”}’
返回:{“_index":“test”,"_type":“emp”,"_id":“1”,"_version":1,“result”:“created”,"_shards":{“total”:2,“successful”:1,“failed”:0},"_seq_no":0,"_primary_term":1}
第一次操作:version默认为1。 当今后每一次对ID=1 的数据进行操作的时候版本号回改变

curl -XPOST ‘localhost:9200/test/emp’ -H “Content-Type: application/json” -d’{“name”:“zhangsan”,“age”:“20”}’
{"_index":“test”,"_type":“emp”,"_id":“8VG2hWkBlzI-pKhwJKP8”,"_version":1,“result”:“created”,"_shards":{“total”:2,“successful”:1,“failed”:0},"_seq_no":0,"_primary_term":1}

3.查询创建的索引:
curl -XGET ‘localhost:9200/test/emp/1’。返回的结果没有被格式化
返回结果:
{“_index":“test”,"_type":“emp”,"_id":“1”,"_version":3,"_seq_no":2,"_primary_term": 1,“found”:true,"_source":{“name”:“zhangsan”,“age”:“20”}}

curl -XGET ‘localhost:9200/test/emp/1?pretty=true’ 返回的结果JSON格式化

返回结果:
{
“_index” : “test”,
“_type” : “emp”,
“_id” : “1”,
“_version” : 3,
“_seq_no” : 2,
“_primary_term” : 1,
“found” : true,
“_source” : {
“name” : “zhangsan”,
“age” : “20”
}
}
查寻指定字段的值:curl -XGET ‘localhost:9200/test/emp/1?_source=name&pretty=true’
返回结果:
{
“_index” : “test”,
“_type” : “emp”,
“_id” : “1”,
“_version” : 3,
“_seq_no” : 2,
“_primary_term” : 1,
“found” : true,
“_source” : {
“name” : “zhangsan”
}
}
返回多个:
curl -XGET ‘localhost:9200/test/emp/1?_source=name,age&pretty=true’
返回结果:
{
“_index” : “test”,
“_type” : “emp”,
“_id” : “1”,
“_version” : 3,
“_seq_no” : 2,
“_primary_term” : 1,
“found” : true,
“_source” : {
“name” : “zhangsan”,
“age” : “20”
}
}
只返回source中的内容:curl -XGET ‘localhost:9200/test/emp/1/_source’
返回结果:{“name”:“zhangsan”,“age”:“20”}
查询出所有数据:curl -XGET ‘localhost:9200/test/emp/_search?pretty’
返回结果:
{
“took” : 2,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : 2,
“max_score” : 1.0,
“hits” : [
{
“_index” : “test”,
“_type” : “emp”,
“_id” : “8VG2hWkBlzI-pKhwJKP8”,
“_score” : 1.0,
“_source” : {
“name” : “zhangsan”,
“age” : “20”
}
},
{
“_index” : “test”,
“_type” : “emp”,
“_id” : “1”,
“_score” : 1.0,
“_source” : {
“name” : “zhangsan”,
“age” : “20”
}
}
]
}
}
根据指定的条件进行查询:
curl -XGET ‘localhost:9200/test/emp/_search?q=name:zhangsan&pretty’
意思是查询索引库为test类型为emp中的所有名字为zhangsan的数据。
返回结果:
{
“took” : 22,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : 2,
“max_score” : 0.2876821,
“hits” : [
{
“_index” : “test”,
“_type” : “emp”,
“_id” : “8VG2hWkBlzI-pKhwJKP8”,
“_score” : 0.2876821,
“_source” : {
“name” : “zhangsan”,
“age” : “20”
}
},
{
“_index” : “test”,
“_type” : “emp”,
“_id” : “1”,
“_score” : 0.2876821,
“_source” : {
“name” : “zhangsan”,
“age” : “20”
}
}
]
}
}

Put:更新: post:新增
Put是幂等的, post不是
所谓的幂等就是无论执行多少次最终的结果都是一样的
在新增的时候可以使用put 和Post 区别在于:
Post操作的是一组资源 : curl -XPOST ‘localhost:9200/test/emp/‘
Put操作的是指定的一个资源:curl -XPUT ‘localhost:9200/test/emp/1’
例如:向数据库添加数据。当ID是自增长的时候使用post。当ID需要程序生成的时候需要使用put

ES 创建索引和索引库时需要注意的问题:
1.索引库的名称必须全部小写,不能以下划线开头,也不能包括逗号
2.如果没有明确指定索引数据的ID,那么ES会自己随机生成一个ID,需要使用post参数
3.如果想要保证我们每次的操作的数据都是新增的:
1:使用随机的ID(POST方式)
2.在URL后面添加参数:
curl -XPUT http://localhost:9200/test/emp/2?op_type=create -d
‘{“name”:”lis”,”age”:34}’
curl -XPUT ‘localhost:9200/test/emp/1/_create’ -H “Content-Type: application/json” -d’{“name”:“zhangsan”,“age”:“20”}’
返回结果:
{“error”:{“root_cause”:[{“type”:“version_conflict_engine_exception”,“reason”:"[emp][1]: version conflict, document already exists (current version [3])",“index_uuid”:“15m_n4rTQkiLAmGV48SerQ”,“shard”:“3”,“index”:“test”}],“type”:“version_conflict_engine_exception”,“reason”:"[emp][1]: version conflict, document already exists (current version [3])",“index_uuid”:“15m_n4rTQkiLAmGV48SerQ”,“shard”:“3”,“index”:“test”},“status”:409}
ES对文档的更新:
ES可以使用put或者post对文档进行更新,如果指定的ID的文档已经存在,则指定更新操作
注意:执行更新操作的时候
ES首先将旧的文档标记为删除状态,然后添加新的文档,就的文档不会立即消失,但是你也无法访问
ES会在你继续添加更多数据的时候清理已经标记为删除状态的文档:
curl -XPUT ‘localhost:9200/test/emp/1’ -H “Content-Type: application/json” -d’{“name”:“zhangsan”,“age”:“20”}’
以上的操作是全部更新:
返回的结果:
{“_index":“test”,"_type":“emp”,"_id":“1”,"_version":4,“result”:“updated”,"_shards":{“total”:2,“successful”:1,“failed”:0},"_seq_no":3,"_primary_term":1}
使用局部更新:对特定的字段进行更新:
在使用局部更新的时候必须使用post才行:
curl -XPOST ‘localhost:9200/test/emp/1/_update’ -H “Content-type:application/json” -d ‘{“doc”:{“age”:30}}’

返回结果:
{“_index":“test”,"_type":“emp”,"_id":“1”,"_version":5,“result”:“updated”,"_shards":{“total”:2,“successful”:1,“failed”:0},"_seq_no":4,"_primary_term":1}

ES删除:
1.删除指定索引库下的指定类型的ID的数据:curl -XDELETE ‘localhost:9200/test/emp/8VG2hWkBlzI-pKhwJKP8’
返回结果:{“_index":“test”,"_type":“emp”,"_id":“8VG2hWkBlzI-pKhwJKP8”,"_version":2,“result”:“deleted”,"_shards":{“total”:2,“successful”:1,“failed”:0},"_seq_no":1,"_primary_term":1}
删除说明:
如果文档存在,ES会返回200 OK的状态码,found属性值为true,version属性值+1
如果文档不存在,es会返回404NotFound的状态码,found 属性值为false,但是_version的属性值任然会加+1
删除一个文档,这个文档不会立即被删除,而是在内存标记为删除了。,后面会慢慢在后台删除

2.根据查询条件进行删除:
	curl -XDELETE 'localhost:9200/test/emp/_query' -H "Content-type:application/json"  -d ‘{"query":{"match":{"name":"zhangsan"}}}'

返回结果:
{“_index":“test”,"_type":“emp”,"_id":"_query","_version":1,“result”:“not_found”,"_shards":{“total”: 2,“successful”:1,“failed”:0},"_seq_no":3,"_primary_term":1}
ES 支持多索引库多类型进行操作
通过查询API删除多个索引库下多种类型下的数据
curl -XDELETE ’localhost:9200/test,test01/emp,user/_query?q=user:huyong’
删除所有索引库中匹配的数据
curl -XDELETE ‘localhost:9200/_all/_query?q=tag:wo’
ES 的批量操作-bulk:
bulk API 可以帮助我们同时执行多个请求
格式:

action:index/create/update/delete
metedata:_index._type._id
request body: _source(删除操作不需要)
{action:{metadata}}
{request boy}
{action:{metadata}}	
{request boy}

Create 和index的区别:
如果数据存在,使用create操作失败,会提示文档已经存在,使用index则可以成功执行
使用文件的方式:
vim requests
curl -XPOST/PUT localhost:9200/_bulk -data-binary @request;
Bulk 请求可以在URL中声明/index 或者/——index/_type
Bulk 一次可以最大处理多少数据量
bulk会把将要处理的数据载入到内存中,所以数据量是有限的,最佳的数据量不是一个确定的值,它取决于你的机器性能
一般建议1000-5000个文档,
Eg:
{index:{"_index":“test”,"_type":“emp”,"_id":“3”}}
{“name”:“lisi”,“age”:40}
{delete:{"_index":“test”,"_type":“emp”,"_id":“1”}}

curl -XPOST ‘localhost:9200/_bulk’ -H “Content-type:application/json” --data-binary @request
返回结果:
{“
took “:24,”
errors “:false,”
items “:[{”
index “:{”
_index “:”
test “,”
_type “:”
emp “,”
_id “:”
3 “,”
_version “:1,”
result “:”
created “,”
_shards “:{”
total “:2,”
successful “:1,”
failed “:0},”
_seq_no “:0,”
_primary_term “:1,”
status “:201}},{”
delete “:{”
_index “:”
test “,”
_type “:”
emp “,”
_id “:”
1 “,”
_version “:6,”
result “:”
deleted “,”
_shards “:{”
total “:2,”
successful “:1,”
failed “:0},”
_seq_no “:5,”
_primary_term “:1,”
status “:200}}]}
ES的版本控制:
普通关系型数据库使用的是(悲观并发控制(PCC))
当我们在读取一个数据前先锁定这一行,然后确保只有读取到数据的这个线程可以修改这一行数据
ES使用(乐观并发控制(OCC))
ES不会阻止某一数据的访问,然而没如果基础数据在我们呢读取和写入的间隔中发生了变化,就会失败,这时候由程序来决定如何处理这个冲突。他可以重新读取新数据来进行更新,或者将这一情况直接反馈给用户。
curl -XPUT ‘localhost:9200/test/emp/3?version=1’ -d’{“name”:“lisi”,“age”:34}’ -H “Content-type:application/json”
在读取的和修改的过程中数据发生了变化执行后的结果:
{“error”:{“root_cause”:[{“type”:“version_conflict_engine_exception”,“reason”:"[emp][3]: version conflict, current version [2] is different than the one provided [1]",“index_uuid”:“15m_n4rTQkiLAmGV48SerQ”,“shard”:“4”,“index”:“test”}],“type”:“version_conflict_engine_exception”,“reason”:"[emp][3]: version conflict, current version [2] is different than the one provided [1]",“index_uuid”:“15m_n4rTQkiLAmGV48SerQ”,“shard”:“4”,“index”:“test”},“status”:409}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值