ElasticSearch-(四)Java使用ElasticSearch

本文最后更新于:February 26, 2022 pm

Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎。Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

目录

创建一个maven项目。并导入依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

索引(数据库)

创建索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.tothefor.ES;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;

import java.io.IOException;

public class EsTest_client {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http")) //ip,端口,请求方式
);

//创建索引
CreateIndexRequest request = new CreateIndexRequest("loong"); //索引
//获取创建的response
CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);
//获取响应状态,是否创建成功
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("添加状态" + acknowledged); //true

esClient.close();
}
}

查询索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.tothefor.ES;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

public class EsTest_client {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http")) //ip,端口,请求方式
);

GetIndexRequest request = new GetIndexRequest("loong"); //指定索引
GetIndexResponse getIndexResponse = esClient.indices().get(request, RequestOptions.DEFAULT);
//响应状态
System.out.println(getIndexResponse);
System.out.println(getIndexResponse.getMappings());
System.out.println(getIndexResponse.getSettings());
System.out.println(getIndexResponse.getAliases());

esClient.close();
}
}

删除索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.tothefor.ES;

import org.apache.http.HttpHost;
import org.apache.ibatis.annotations.Delete;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;

import java.io.IOException;

public class EsTest_client {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http")) //ip,端口,请求方式
);

DeleteIndexRequest request = new DeleteIndexRequest("loong");
AcknowledgedResponse response = esClient.indices().delete(request, RequestOptions.DEFAULT);
//响应状态
System.out.println(response.isAcknowledged()); //true

esClient.close();
}
}

文档(数据)

添加文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.tothefor.ES;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tothefor.pojo.dto.ESUser;
import com.tothefor.pojo.entity.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class EsTest_client {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http")) //ip,端口,请求方式
);

//插入数据
IndexRequest request = new IndexRequest();
request.index("loong").id("1007"); //指定索引,并指定文档的id

//这种也可以
// IndexRequest request = new IndexRequest("loong");//指定索引
// request.id("1008"); //指定文档的id

//自定义类
ESUser user = new ESUser();
user.setName("墨水记忆7777");
user.setAge(23);
user.setPhone("165632536");

//数据转Json,好像在ElasticSearch8.x中不再需要转json了
ObjectMapper mapper = new ObjectMapper();
String userJson = mapper.writeValueAsString(user);

//发送数据
request.source(userJson, XContentType.JSON);

//响应状态
IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);

System.out.println(response.getResult()); //CREATED

esClient.close();
}
}

注意:在用PostMan测试时发现,如果是密码属性(即自定义类中有密码password属性,在查询时不会显示出来),则不会显示。

修改文档

这里是局部修改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.tothefor.ES;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tothefor.pojo.dto.ESUser;
import com.tothefor.pojo.entity.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class EsTest_client {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http")) //ip,端口,请求方式
);

//修改数据
UpdateRequest request = new UpdateRequest();
request.index("loong").id("1008"); //指定索引,并指定文档的id。这里就必须使用这种方式了,不能像添加时可以有两种方式

request.doc(XContentType.JSON,"age",13);

UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);

System.out.println(response.getResult()); //UPDATED

esClient.close();
}
}

查询文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.tothefor.ES;

import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;

public class EsTest_client {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http")) //ip,端口,请求方式
);

//查询数据
GetRequest request = new GetRequest();
request.index("loong").id("1008"); //指定索引和文档id

//也可以使用以下方式
// GetRequest request = new GetRequest("loong"); //指定索引
// request.id("1008"); //指定文档id

GetResponse response = esClient.get(request, RequestOptions.DEFAULT);

System.out.println(response.getSourceAsString()); //更多方法可自行测试

esClient.close();
}
}

删除文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.tothefor.ES;

import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;

public class EsTest_client {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http")) //ip,端口,请求方式
);

//删除数据
DeleteRequest request = new DeleteRequest();
request.index("loong").id("1001"); //指定索引和文档id

//也可以使用以下方式
// DeleteRequest request = new DeleteRequest("loong");//指定索引
// request.id("1002");//指定文档id

DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);

System.out.println(response.toString());

esClient.close();
}
}

批量操作

批量添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.tothefor.ES;

import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class EsTest_client {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http")) //ip,端口,请求方式
);

//批量添加数据
BulkRequest request = new BulkRequest();

request.add(new IndexRequest().index("loong").id("11").source(XContentType.JSON,"name","loong11"));
request.add(new IndexRequest().index("loong").id("12").source(XContentType.JSON,"name","loong12"));
request.add(new IndexRequest().index("loong").id("13").source(XContentType.JSON,"name","loong13"));

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

System.out.println(response.getTook());
System.out.println(response.getItems());


esClient.close();
}
}

批量删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.tothefor.ES;

import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

public class EsTest_client {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http")) //ip,端口,请求方式
);

//批量删除数据
BulkRequest request = new BulkRequest();

request.add(new DeleteRequest().index("loong").id("11"));
request.add(new DeleteRequest().index("loong").id("12"));
request.add(new DeleteRequest().index("loong").id("13"));

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

System.out.println(response.getTook());
System.out.println(response.getItems());


esClient.close();
}
}