您的位置 首页 java

从零开始学java之批量获取文档数据

一、文档批量操作

这里多个文档是指,批量操作多个文档,搜索查询文档将在之后的章节讲解

1.批量获取文档数据

批量获取文档数据是通过_mget的API来实现的

(1)在URL中不指定index和type

  • 请求方式:GET
  • 请求地址:_mget
  • 功能说明 : 可以通过ID批量获取不同index和type的数据

请求参数:

  • docs : 文档数组参数
  • _index : 指定index
  • _type : 指定type
  • _id : 指定id
  • _source : 指定要查询的字段

1 GET _mget

2 {

3 “docs” : [

4 {

5 “_index” : “es_db” ,

6 “_type” : “_doc” ,

7 “_id” : 1

8 },

9 {

10 “_index” : “es_db” ,

11 “_type” : “_doc” ,

12 “_id” : 2

13 }

14 ]

15 }

响应结果如下:

1 {

2 “docs” : [ 3 {

4 “_index” : “es_db” ,

5 “_type” : “_doc” ,

6 “_id” : “1” ,

7 “_version” : 3 ,

8 “_seq_no” : 7 ,

9 “_ primary _term” : 1 ,

10 “found” : true ,

11 “_source” : {

12 “name” : “张三666” ,

13 “sex” : 1 ,

14 “age” : 25 ,

15 “address” : “广州天河公园” ,

16 “remark” : “java developer”

17 }

18 },

19 {

20 “_index” : “es_db” ,

21 “_type” : “_doc” ,

22 “_id” : “2” ,

23 “_version” : 1 ,

24 “_seq_no” : 1 ,

25 “_primary_term” : 1 ,

26 “found” : true ,

27 “_source” : {

28 “name” : “李四” ,

29 “sex” : 1 ,

30 “age” : 28 ,

31 “address” : “广州荔湾大厦” ,

32 “remark” : “java assistant”

33 }

34 }

35 ]

36 }

(2)在URL中指定index

  • 请求方式:GET
  • 请求地址:/{{indexName}}/_mget
  • 功能说明 : 可以通过ID批量获取不同index和type的数据请求参数:

docs : 文档数组参数

  • _index : 指定index
  • _type : 指定type
  • _id : 指定id
  • _source : 指定要查询的字段

1 GET / user / _mget

2 {

3 “docs” : [

4 {

5 “_type” : “_doc” ,

6 “_id” : 3

7 },

8 {

9 “_type” : “_doc” ,

10 “_id” : 4

11 }

12 ]

13 }

(3)在URL中指定index和type

  • 请求方式:GET
  • 请求地址:/{{indexName}}/{{typeName}}/_mget
  • 功能说明 : 可以通过ID批量获取不同index和type的数据

请求参数:

  • docs : 文档数组参数
  • _index : 指定index
  • _type : 指定type
  • _id : 指定id
  • _source : 指定要查询的字段

1 GET / es_db / _doc / _mget

2 { 3 “docs” : [

4 {

5 “_id” : 1

6 },

7 {

8 “_id” : 2

9 }

10 ]

11 }

2.批量操作文档数据

批量对文档进行写操作是通过_bulk的API来实现的

  • 请求方式:POST
  • 请求地址:_bulk
  • 请求参数:通过_bulk操作文档,一般至少有两行参数(或偶数行参数)
  • 第一行参数为指定操作的类型及操作的对象

(index,type和id)

  • 第二行参数才是操作的数据

参数类似于:

1 { “actionName” :{ “_index” : “indexName” , “_type” : “typeName” , “_id” : “id” }}

2 { “field1” : “value1” , “field2” : “value2” }

  • actionName:表示操作类型,主要有create,index,delete和update

(1)批量创建文档create

1 POST _bulk

2 { “create” :{ “_index” : “article” , “_type” : “_doc” , “_id” : 3 }}

3 { “id” : 3 , “title” : “老师1” , “content” : “老师666” , “tags” :[ “java” , “面向对

象” ], “create_time” : 155402530 }

4 { “create” :{ “_index” : “article” , “_type” : “_doc” , “_id” : 4 }}

5 { “id” : 4 , “title” : “老师2” , “content” : “老师NB” , “tags” :[ “java” , “面向对

象” ], “create_time” : 15542530 }

(2)普通创建或全量替换index

1 POST _bulk

2 { “index” :{ “_index” : “article” , “_type” : “_doc” , “_id” : 3 }}

3 { “id” : 3 , “title” : “老师(一)” , “content” : “老师666” , “tags” :[ “j

ava” , “面向对象” ], “create_time” : 1552530 }

4 { “index” :{ “_index” : “article” , “_type” : “_doc” , “_id” : 4 }} 5 { “id” : 4 , “title” : “老师(二)” , “content” : “老师NB” , “tags” :[ “java” , “面向对象” ], “create_time” : 1552530 }

  • 如果原文档不存在,则是创建
  • 如果原文档存在,则是替换(全量修改原文档)

(3)批量删除delete

1 POST _bulk

2 { “delete” :{ “_index” : “article” , “_type” : “_doc” , “_id” : 3 }}

3 { “delete” :{ “_index” : “article” , “_type” : “_doc” , “_id” : 4 }}

(4)批量修改update

1 POST _bulk

2 { “update” :{ “_index” : “article” , “_type” : “_doc” , “_id” : 3 }}

3 { “doc” :{ “title” : “ES大法必修内功” }}

4 { “update” :{ “_index” : “article” , “_type” : “_doc” , “_id” : 4 }}

5 { “doc” :{ “create_time” : 15508 }}

分享就到这里啦,喜欢的朋友们点赞,收藏,加关注哦!领取资料后台私聊小编:即可免费领取!

文章来源:智云一二三科技

文章标题:从零开始学java之批量获取文档数据

文章地址:https://www.zhihuclub.com/186818.shtml

关于作者: 智云科技

热门文章

网站地图