從零開始學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

}}

分享就到這裡啦,喜歡的朋友們點贊,收藏,加關注哦!領取資料後臺私聊小編:即可免費領取!