Elasticsearch系列(四)----Elasticsearch索引文档的增删改查

487 阅读4分钟
原文链接: blog.csdn.net

1、RESTful接口格式



1.1、RESTful接口URL的格式:

[html] view plain copy print?
  1. http://localhost:9200/<index>/<type >/[<id>]  
http://localhost:9200/<index>/<type>/[<id>]

其中index、type是必须提供的。

id是可选的,不提供es会自动生成。

index、type将信息进行分层,利于管理。

index可以理解为数据库;type理解为数据表;id相当于数据库表中记录的主键,是唯一的。


1.2、HTTP客户端


操作REST API常用的有通过CRUL命令,Chrome Sense插件,httpie,kibana中的Dev Tools,head等,下面一一介绍下.


A)CURL命令


在windows下可以安装curl.exe来执行curl命令,或者安装GIT,它自带了一个BASH




2)Sense插件




3)httpie




4)Dev Tools





5)Head




1.3、样例数据

A)下载样例数据


可以使用ES官网提供的数据:download.csdn.net/download/u0…

可以使用在线生成工具:beta.json-generator.com/N1BZnORDQ 生成JSON的数据


B)加载样例数据集


下载样例数据集链接,解压数据到指定目录,然后


 ● 创建一个索引库:


[html] view plain copy print?
  1. curl -XPUT 'localhost:9200/fendo'  
curl -XPUT 'localhost:9200/fendo'



● 导入数据:


绝对路径:

[html] view plain copy print?
  1. curl -XPOST 'localhost:9200/fendo/account/_bulk?pretty' --data-binary "@/g/Developer/Elasticsearch5.5.1/accounts.json"  
curl -XPOST 'localhost:9200/fendo/account/_bulk?pretty' --data-binary "@/g/Developer/Elasticsearch5.5.1/accounts.json"



相对路径:
[html] view plain copy print?
  1. curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"  
curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"


注意:

1 需要在accounts.json所在的目录运行curl命令。

2 localhost:9200是ES得访问地址和端口

3 bank是索引的名称

4 account是类型的名称

5 索引和类型的名称在文件中如果有定义,可以省略;如果没有则必须要指定

6 _bulk是rest得命令,可以批量执行多个操作(操作是在json文件中定义的,原理可以参考之前的翻译)

7 pretty是将返回的信息以可读的JSON形式返回。


2、索引文档的创建


格式:

[html] view plain copy print?
  1. http://localhost:9200/<index>/<type >/id  
http://localhost:9200/<index>/<type>/id


例如:

[html] view plain copy print?
  1. curl -XPUT "http://localhost:9200/fendo/es/1" -d '{"first_name":"fendo"}'  
curl -XPUT "http://localhost:9200/fendo/es/1" -d '{"first_name":"fendo"}'

索引名字是:fendo;
索引的类型是:es;
本记录的id是:1

通过CURL命令:



通过sense




返回的信息可以看到创建是成功的,并且版本号是1;ES会对记录修改进行版本跟踪,第一次创建记录为1,同一条记录每修改一次就追加1。


至此一条记录就提交到ES中建立了索引,注意HTTP的方法是PUT,不要选择错了。


3、索引文档的查询



ES提供了两种搜索的方式:请求参数方式 和 请求体方式。


A)请求参数方式

[html] view plain copy print?
  1. curl 'localhost:9200/fendo/_search?q=*&pretty'  
curl 'localhost:9200/fendo/_search?q=*&pretty'

其中fendo是查询的索引名称,q后面跟着搜索的条件:q=*表示查询所有的内容


B)请求体方式(推荐这种方式)


[html] view plain copy print?
  1. curl -XPOST 'localhost:9200/fendo/_search?pretty' -d '  
  2. {  
  3.   "query": { "match_all": {} }  
  4. }'  
curl -XPOST 'localhost:9200/fendo/_search?pretty' -d '
{
  "query": { "match_all": {} }
}'

这种方式会把查询的内容放入body中,会造成一定的开销,但是易于理解。在平时的练习中,推荐这种方式。


根据索引时的ID查询的文档的RESTful接口如下

[html] view plain copy print?
  1. curl -XGET "http://127.0.0.1:9200/fendo/es/1"  
curl -XGET "http://127.0.0.1:9200/fendo/es/1"

HTTP方法采用GET的形式。


用head看的更直观些:


返回的内容大致如下:

took:是查询花费的时间,毫秒单位

time_out:标识查询是否超时

_shards:描述了查询分片的信息,查询了多少个分片、成功的分片数量、失败的分片数量等

hits:搜索的结果,total是全部的满足的文档数目,hits是返回的实际数目(默认是10)

_score是文档的分数信息,与排名相关度有关。

4、索引文档的更新


根据索引时的ID更新的文档的内容其RESTful接口如下


[html] view plain copy print?
  1. curl -XPUT "http://localhost:9200/fendo/es/1" -d '{"first_name":"fk"}'  
curl -XPUT "http://localhost:9200/fendo/es/1" -d '{"first_name":"fk"}'


HTTP方法采用PUT或POST形式。



将名字由“fendo”改成“fk”;


结果中的version字段已经成了2,因为我们这是是修改,索引版本递增;created字段是false,表示这次不是新建而是更新。


更新接口与创建接口完全一样,ES会查询记录是否存在,如果不存在就是创建,存在就是更新操作。


5、索引文档的删除


根据索引时的ID更新的文档的内容其RESTful接口如下

[html] view plain copy print?
  1. curl -XDELETE "http://localhost:9200/fendo/es/1"  
curl -XDELETE "http://localhost:9200/fendo/es/1"



HTTP方法采用DELETE的形式。


删除过后,再通过查询接口去查询将得不到结果。