java中es怎么使用_ElasticSearch学习03--使用Java连接ES

ElasticSearch学习03--使用Java连接ES

1.创建项目并测试连接

1.1 新建一个spring boot的项目,在pom.xml文件中添加如下引用:

1 dependencies

2 dependency

3 groupIdorg.springframework.boot/groupId

4 artifactIdspring-boot-starter-web/artifactId

5 /dependency

6 !-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch --

7 dependency

8 groupIdorg.elasticsearch/groupId

9 artifactIdelasticsearch/artifactId

10 version7.3.2/version

11 /dependency

12 !-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client --

13 dependency

14 groupIdorg.elasticsearch.client/groupId

15 artifactIdelasticsearch-rest-high-level-client/artifactId

16 version7.3.2/version

17 /dependency

18 dependency

19 groupIdjunit/groupId

20 artifactIdjunit/artifactId

21 version4.12/version

22 /dependency

23 dependency

24 groupIdmysql/groupId

25 artifactIdmysql-connector-java/artifactId

26 scoperuntime/scope

27 /dependency

28 dependency

29 groupIdorg.projectlombok/groupId

30 artifactIdlombok/artifactId

31 optionaltrue/optional

32 /dependency

33 dependency

34 groupIdorg.springframework.boot/groupId

35 artifactIdspring-boot-starter-test/artifactId

36 scopetest/scope

37 /dependency

38 /dependencies

1.2 新建一个工具类,连接ES服务

1 package com.demo.esdemo.utils;

2

3 import org.apache.http.HttpHost;

4 import org.elasticsearch.client.RestClient;

5 import org.elasticsearch.client.RestClientBuilder;

6 import org.elasticsearch.client.RestHighLevelClient;

7

8 public class ESClient {

9 public static RestHighLevelClient getClient() {

10 //创建HttpHost

11 HttpHost host = new HttpHost("localhost", 9200);

12

13

14 // 创建RestClientBuilder

15 RestClientBuilder builder = RestClient.builder(host);

16

17 // 创建RestHighLevelClient

18 RestHighLevelClient client = new RestHighLevelClient(builder);

19

20 return client;

21 }

22 }

1.3 测试连接

1 @Test

2 void testConnect() {

3 RestHighLevelClient client = ESClient.getClient();

4 System.out.println("ok");

5 }

1.4 运行测试

打印"ok",测试通过。

2.使用Java操作索引

2.1 使用Java创建索引

1 String index = "person";

2 String type = "doc";

3

4

5 @Test

6 void createIndex() throws IOException {

7 //settings设置

8 Settings.Builder settings = Settings.builder()

9 .put("number_of_shards",3)

10 .put("number_of_replicas",1);

11

12

13 //mappings设置

14 XContentBuilder mappings = JsonXContent.contentBuilder()

15 .startObject()

16 .startObject("properties")

17 .startObject("name")

18 .field("type","text")

19 .endObject()

20 .startObject("age")

21 .field("type","integer")

22 .endObject()

23 .startObject("birthday")

24 .field("type","date")

25 .field("format","yyyy-MM-dd")

26 .endObject()

27 .endObject()

28 .endObject();

29

30 //将settings和mappings封装到Resquest对象中

31 CreateIndexRequest request = new CreateIndexRequest(index)

32 .settings(settings)

33 .mapping(mappings);

34

35 //通过client连接es,并创建索引

36 RestHighLevelClient client = ESClient.getClient();

37 CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);

38

39 //输出

40 System.out.println(response);

41 }

2.2 检查索引是否存在

1 String index = "person";

2 String type = "doc";

3

4 @Test

5 void exists() throws IOException {

6 //准备request对象

7 GetIndexRequest request = new GetIndexRequest(index);

8

9 //通过client连接es

10 RestHighLevelClient client = ESClient.getClient();

11 boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);

12

13 //输出

14 System.out.println(exists);

15 }

2.3 删除索引

1 String index = "person";

2 String type = "doc";

3

4 @Test

5 void delete() throws IOException {

6 //准备request对象

7

8 DeleteIndexRequest request = new DeleteIndexRequest(index);

9

10 //通过client连接es

11 RestHighLevelClient client = ESClient.getClient();

12 AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);

13

14 //输出

15 System.out.println(delete.isAcknowledged());

16 }

3.使用Java操作文档

3.1 添加文档

1 String index = "person";

2 String type = "doc";

3

4 @Test

5 void createDoc() throws IOException {

6 //准备一个json数据

7 Person person = new Person(1,"张三",18,new Date());

8 ObjectMapper mapper = new ObjectMapper();

9 String json = mapper.writeValueAsString(person);

10

11 //准备request对象

12 IndexRequest request = new IndexRequest(index,"_doc","1");

13 request.source(json, XContentType.JSON);

14

15 //通过client对象连接es

16 RestHighLevelClient client = ESClient.getClient();

17 IndexResponse response = client.index(request, RequestOptions.DEFAULT);

18

19 //输出

20 System.out.println(response.getResult());

21 }

3.2 修改与删除文档

1 String index = "person";

2 String type = "doc";

3

4 @Test

5 void updateDoc() throws IOException {

6 //创建一个map,指定需要修改的内容

7 MapString,Object doc = new HashMap();

8 doc.put("name","李四");

9

10 //request对象

11 UpdateRequest request = new UpdateRequest(index,"1");

12 request.doc(doc);

13

14 //连接es

15 RestHighLevelClient client = ESClient.getClient();

16 UpdateResponse update = client.update(request, RequestOptions.DEFAULT);

17

18 //输出

19 System.out.println(update.getResult());

20 }

3.3 删除文档

1 String index = "person";

2 String type = "doc";

3

4 @Test

5 void deleteDoc() throws IOException {

6 //request对象

7 DeleteRequest request = new DeleteRequest(index,"1");

8

9 //client执行

10 RestHighLevelClient client = ESClient.getClient();

11 DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);

12

13 //输出

14 System.out.println(delete.getResult());

15 }

4.批量操作文档

4.1 批量添加文档

1 String index = "person";

2 String type = "doc";

3

4 @Test

5 void bulkCreateDoc() throws IOException {

6 //准备一个json数据

7 Person person1 = new Person(1,"张三",18,new Date());

8 Person person2 = new Person(2,"李四",20,new Date());

9 Person person3 = new Person(3,"王五",24,new Date());

10 ObjectMapper mapper = new ObjectMapper();

11 String json1 = mapper.writeValueAsString(person1);

12 String json2 = mapper.writeValueAsString(person2);

13 String json3 = mapper.writeValueAsString(person3);

14

15 //准备request对象

16 BulkRequest request = new BulkRequest();

17 request.add(new IndexRequest(index,"_doc",person1.getId().toString()).source(json1,XContentType.JSON));

18 request.add(new IndexRequest(index,"_doc",person2.getId().toString()).source(json2,XContentType.JSON));

19 request.add(new IndexRequest(index,"_doc",person3.getId().toString()).source(json3,XContentType.JSON));

20

21

22 //通过client对象连接es

23 RestHighLevelClient client = ESClient.getClient();

24 BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);

25

26 //输出

27 System.out.println(response);

28 }

4.2 批量删除文档

1 String index = "person";

2 String type = "_doc";

3

4 @Test

5 void buldDeleteDoc() throws IOException {

6 BulkRequest request = new BulkRequest();

7 request.add(new DeleteRequest(index,type,"1"));

8 request.add(new DeleteRequest(index,type,"2"));

9 request.add(new DeleteRequest(index,type,"3"));

10

11 RestHighLevelClient client = ESClient.getClient();

12 BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);

13

14 System.out.println(response);

15 }

ElasticSearch学习03--使用Java连接ES 相关文章

在Vue.js学习过程中学习到的数组方法

在Vue.js学习过程中学习到的JavaScript基础 关于数组中的一些方法 // 1、push() 方法 往后添加一个或多个,新元素// this.letters.push('aaa')// 2、pop() : 删除数组中最后一个元素// this.letters.pop();// 3、shift() : 删除数组中第一个元素// this.lett

Markdowm简单语法

Markdown学习(1级标题) 标题(2级标题) 三级标题 四级标题 标题 //语法格式:n*#+空格+标题名 表示n级标题(最多六级标题) 字体 Hello,World! -------//语法格式:内容两边两星号表示加粗 hello,world ---------//语法格式:内容两边一星号表示斜体 hello

2021.2.3学习编程第二天

打开CMD的方式 开始+系统+命令提示符 Win键+R 输入cmd打开控制台 在任意的文件夹下面,按住shift键鼠标右键点击,在此处打开命令行窗口 资源管理器的地址栏前面加上cmd 路径 常用的Dos命令 #盘符切换 #查看当前目录下的所有文件 dir #切换目录 cd change dir

逻辑运算符,位运算符学习笔记

一. package operation; //逻辑运算符 public class Demo05 { public static void main(String[] args) { // 与(and) 或(or) 非(取反) boolean a = true; boolean b = false; System.out.println("a b:" +(ab));//逻辑与运算,两个都为真,结果才为tru

基本运算符学习笔记

一. package operation; public class Demo01 { public static void main(String[] args) { // 二元运算符(a+b) int a = 10; int b = 20; int c = 15; int d = 25; System.out.println(a+b); System.out.println(a-b); System.out.println(a*b); System.ou

DAY1 学习markdown

Markdown学习 标题 井号+空格 ①多少个井号 对应 几级标题 ② 最多 六级标题 三级标题 四级标题 字体 Hallo world! (粗体:字的两边+** / Ctrl+B) Hallo world! (斜体:字的两边+* / Ctrl+I) Hallo world! (粗体+斜体:字的两边+ ***) Hallo world!

变量常量作用域学习笔记

一. package base; public class Demo05 { public static void main(String[] args) { //操作较大数值的时候注意溢出问题!! //jdk7新特性(数字之间可以用下划线分割) int money = 10_0000_0000; int years = 20; int total = money*years; System.out.pr

学习笔记08.逻辑运算与位运算

逻辑运算与位运算 逻辑运算符 逻辑运算符,即 与(and) 或||(or) 非!(取反) System.out.println("a b:"+(ab)); //逻辑与运算:两个变量都为真,结果才为trueSystem.out.println("a || b:"+(a||b)); //逻辑或运算:两个变量有一个为真,则结果为trueSys

类型转换学习笔记

package base; public class Demo04 { public static void main(String[] args) { int i =130; byte b = (byte)i;// 内存溢出,130数值超出byte最大值 System.out.println(i); System.out.println(b); //强制转换(byte)i--(类型)变量名 高--低 System.ou

C语言入门学习(第六天——分支结构)

这一次来看一下if、switch语句 有了判断语句的加持,程序的变化将增加很多。 一、if语句 ①单行if形式(不推荐) if (表达式) 语句;//只能输入1行语句 ②基本形式 if (表达式)//这个表达式将返回一个逻辑值{ ……//若为真,将执行这一段语句块}…… ③简单附


版权声明:本文为weixin_39777967原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。