add: nutzboot-starter-elasticsearch-rest ES高级客户端-官方推荐; elasticsearch: 6.3.2->7.14.0

This commit is contained in:
王庆华 2021-08-29 11:09:18 +08:00
parent e5b9212190
commit a082d52787
17 changed files with 547 additions and 10 deletions

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nutzboot-demo-simple</artifactId>
<groupId>org.nutz</groupId>
<version>2.5.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nutzboot-demo-simple-elasticsearch-rest</artifactId>
<dependencies>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-elasticsearch-rest</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutzboot-starter-test-junit4</artifactId>
<version>${nutzboot.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,24 @@
package io.nutz.demo.simple;
import io.nutz.demo.simple.service.UserService;
import org.nutz.boot.NbApp;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
/**
* Created by 王庆华 on 2021/8/28.
*/
@IocBean
public class MainLauncher {
private final static Log log = Logs.get();
@Inject
private UserService userService;
public static void main(String[] args) throws Exception {
new NbApp().setPrintProcDoc(true).run();
}
}

View File

@ -0,0 +1,54 @@
package io.nutz.demo.simple.bean;
public class User {
private String id;
private String name;
private String email;
private Long createAt;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getCreateAt() {
return createAt;
}
public void setCreateAt(Long createAt) {
this.createAt = createAt;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", email='" + email + '\'' +
", createAt=" + createAt +
'}';
}
}

View File

@ -0,0 +1,159 @@
package io.nutz.demo.simple.service;
import io.nutz.demo.simple.bean.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
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.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
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.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@IocBean(create = "init")
public class UserService {
private final static Log log = Logs.get();
@Inject
private RestHighLevelClient client;
private String index = User.class.getSimpleName().toLowerCase();
public void init() throws Exception {
boolean exists = client.indices().exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
if (!exists) {
createIndex();
}
}
public void createIndex() throws IOException {
final CreateIndexResponse createIndexResponse;
createIndexResponse = client.indices().create(new CreateIndexRequest(index), RequestOptions.DEFAULT);
log.debug("createIndex ack:" + createIndexResponse.isAcknowledged());
}
public void deleteIndex() throws IOException {
final AcknowledgedResponse response = client.indices().delete(new DeleteIndexRequest(index), RequestOptions.DEFAULT);
log.debug("deleteIndex ack:" + response.isAcknowledged());
}
public boolean addUser(User user) throws IOException {
final IndexRequest indexRequest = new IndexRequest();
indexRequest.index(index)
.id(user.getId() != null ? user.getId() : null);
indexRequest.source(Json.toJson(user), XContentType.JSON);
IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
log.debug("addUser result:" + index.getResult());
return index.getResult().name().equals("CREATED");
}
public void batchInsert(List<User> users) throws IOException {
BulkRequest bulkRequest = new BulkRequest();
users.forEach(user -> {
bulkRequest.add(new IndexRequest().index(index).id(user.getId() != null ? user.getId() : null)
.source(Json.toJson(user), XContentType.JSON));
});
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
log.debug(Json.toJson(bulkResponse.getItems()));
}
public boolean deleteUser(String userId) throws IOException {
final DeleteResponse response = client.delete(new DeleteRequest(index, userId), RequestOptions.DEFAULT);
return response.getResult().name().equals("DELETED");
}
public void batchDeleteUser(String[] userIds) throws IOException {
BulkRequest bulkRequest = new BulkRequest();
for (String userId : userIds) {
bulkRequest.add(new DeleteRequest().index(index).id(userId));
}
final BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
log.debug(Json.toJson(bulkResponse.getItems()));
}
public boolean updateUser(User user) throws IOException {
UpdateRequest request = new UpdateRequest();
request.index(index).id(user.getId());
request.doc(new IndexRequest().source(Json.toJson(user), XContentType.JSON));
final UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
log.debug("updateUser:" + response.getResult());
return response.getResult().name().equals("UPDATED");
}
public User fetchUser(String userId) throws IOException {
final GetResponse documentFields = client.get(new GetRequest(index, userId), RequestOptions.DEFAULT);
return Json.fromJson(User.class, documentFields.getSourceAsString());
}
public List<User> query(String name, String email) throws IOException {
SearchRequest request = new SearchRequest();
request.indices("user");
final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
final BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchQuery("name", name))
.must(QueryBuilders.matchQuery("email", email));
sourceBuilder.query(boolQueryBuilder);
request.source(sourceBuilder);
final SearchResponse response = client.search(request, RequestOptions.DEFAULT);
final SearchHits hits = response.getHits();
List<User> users = new ArrayList<>();
for (SearchHit hit : hits) {
users.add(Json.fromJson(User.class, hit.getSourceAsString()));
}
return users;
}
public List<User> queryPage(String name, Integer pageNumber, Integer pageSize) throws IOException {
SearchRequest request = new SearchRequest();
request.indices(index);
final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(
QueryBuilders.termQuery("name", name));
sourceBuilder.from((pageNumber - 1) * pageSize);
sourceBuilder.size(pageSize);
request.source(sourceBuilder);
final SearchResponse response = client.search(request, RequestOptions.DEFAULT);
final SearchHits hits = response.getHits();
List<User> users = new ArrayList<>();
for (SearchHit hit : hits) {
users.add(Json.fromJson(User.class, hit.getSourceAsString()));
}
return users;
}
}

View File

@ -0,0 +1,7 @@
log4j.rootLogger=debug,Console
log4j.logger.org.nutz=debug
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss.SSS}] %5p [%t] --- %c{1}: %m%n

View File

@ -0,0 +1,6 @@
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout
rootLogger.level = info
rootLogger.appenderRef.console.ref = console

View File

@ -0,0 +1,152 @@
package io.nutz.demo.simple.service;
import io.nutz.demo.simple.MainLauncher;
import io.nutz.demo.simple.bean.User;
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.nutz.boot.NbApp;
import org.nutz.boot.test.junit4.NbJUnit4Runner;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.lang.Lang;
import org.nutz.lang.Times;
import org.nutz.lang.random.R;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import java.io.IOException;
import java.util.List;
@IocBean
@RunWith(NbJUnit4Runner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class UserServiceTest {
Log log = Logs.get();
@Inject
private UserService userService;
@Test
public void test01_AddUser() {
User user = new User();
user.setId(R.UU32());
user.setName("wendal");
user.setEmail("wendal@gmail.com");
user.setCreateAt(Times.getTS());
try {
Assert.assertTrue(userService.addUser(user));
} catch (IOException e) {
log.error(e);
}
}
@Test
public void test02_batchInsert() {
try {
User user1 = new User();
user1.setId("1001");
user1.setName("wendal");
user1.setEmail("wendal@gmail.com");
user1.setCreateAt(Times.getTS());
User user2 = new User();
user2.setId("1002");
user2.setName("wizzer");
user2.setEmail("wizzer@gmail.com");
user2.setCreateAt(Times.getTS());
User user3 = new User();
user3.setId("1003");
user3.setName("Eggsblue");
user3.setEmail("topcodermydream@gmail.com");
user3.setCreateAt(Times.getTS());
userService.batchInsert(Lang.list(user1, user2, user3));
} catch (IOException e) {
log.error(e);
}
}
@Test
public void test03_fetchUser() {
try {
final User user = userService.fetchUser("1002");
log.debug("user:" + user);
Assert.assertNotNull(user);
} catch (IOException e) {
log.error(e);
}
}
@Test
public void test04_query() {
try {
final List<User> list = userService.query("wendal", "wendal@gmail.com");
log.info("query:" + Json.toJson(list));
Assert.assertNotNull(list);
} catch (IOException e) {
log.error(e);
}
}
@Test
public void test05_queryPage() {
try {
final List<User> list = userService.queryPage("wendal", 1, 10);
log.info("queryPage:" + Json.toJson(list));
Assert.assertNotNull(list);
} catch (IOException e) {
log.error(e);
}
}
@Test
public void test06_deleteUser() {
try {
Assert.assertTrue(userService.deleteUser("1003"));
} catch (IOException e) {
log.error(e);
}
}
@Test
public void test07_updateUser() {
try {
User user = new User();
user.setId("1001");
user.setName("Eggsblue-1");
user.setEmail("1719411461@qq.com");
Assert.assertTrue(userService.updateUser(user));
} catch (IOException e) {
log.error(e);
}
}
@Test
public void test08_batchDeleteUser() {
try {
userService.batchDeleteUser(Lang.array("1002", "1003"));
} catch (IOException e) {
log.error(e);
}
}
@After
public void after() {
try {
userService.deleteIndex();
} catch (IOException e) {
log.error(e);
}
}
public static NbApp createNbApp() {
NbApp nb = new NbApp().setMainClass(MainLauncher.class);
nb.getAppContext().setMainPackage("io.nutz");
return nb;
}
}

View File

@ -20,15 +20,16 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.0</version>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.2</version>
<version>2.11.1</version>
</dependency>
</dependencies>

View File

@ -132,7 +132,7 @@ public class UserService {
log.debug("srb:::\r\n" + srb.toString());
SearchResponse response = srb.execute().actionGet();
SearchHits hits = response.getHits();
page.setTotalCount((int) hits.getTotalHits());
page.setTotalCount(hits.getHits().length);
List<Map<String, Object>> list = new ArrayList<>();
hits.forEach(searchHit -> {
Map<String, Object> source = searchHit.getSourceAsMap();
@ -166,7 +166,7 @@ public class UserService {
log.debug("srb:::\r\n" + srb.toString());
SearchResponse response = srb.execute().actionGet();
SearchHits hits = response.getHits();
if (hits.getTotalHits() > 0)
if (hits.getHits().length > 0)
return hits.getAt(0).getSourceAsMap();
return null;
}

View File

@ -2,16 +2,15 @@ package io.nutz.demo.simple.utils;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -69,8 +68,7 @@ public class ElasticsearchUtil {
* @return true/false
*/
public boolean deleteIndex(String indexName) {
DeleteIndexResponse response =
getClient().admin().indices().prepareDelete(indexName).execute().actionGet();
final AcknowledgedResponse response = getClient().admin().indices().prepareDelete(indexName).execute().actionGet();
return response.isAcknowledged();
}
@ -93,7 +91,7 @@ public class ElasticsearchUtil {
*/
public boolean putMapping(String indexName, String type, XContentBuilder mapping) {
PutMappingRequest mappingRequest = Requests.putMappingRequest(indexName).type(type).source(mapping);
PutMappingResponse response = getClient().admin().indices().putMapping(mappingRequest).actionGet();
AcknowledgedResponse response = getClient().admin().indices().putMapping(mappingRequest).actionGet();
return response.isAcknowledged();
}

View File

@ -66,6 +66,7 @@
<module>nutzboot-demo-simple-jetty-nacos-config-client</module>
<module>nutzboot-demo-simple-webjars</module>
<module>nutzboot-demo-simple-jasypt</module>
<module>nutzboot-demo-simple-elasticsearch-rest</module>
</modules>

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nutzboot-starter</artifactId>
<groupId>org.nutz</groupId>
<version>2.5.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nutzboot-starter-elasticsearch-rest</artifactId>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,61 @@
package org.nutz.boot.starter.elasticsearch.rest;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.nutz.boot.annotation.PropDoc;
import org.nutz.boot.starter.ServerFace;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import java.util.ArrayList;
import java.util.List;
/**
* Created by 王庆华 on 2021/8/28.
*/
@IocBean
public class ElasticsearchStarter implements ServerFace {
private final static Log log = Logs.get();
@Inject("refer:$ioc")
protected Ioc ioc;
@Inject
protected PropertiesProxy conf;
protected static final String PRE = "elasticsearch.";
@PropDoc(value = "Elasticsearch的主机地址", defaultValue = "127.0.0.1:9200", need = true)
public static final String PROP_HOST = PRE + "host";
public void start() throws Exception {
ioc.get(RestHighLevelClient.class, "elasticsearchClient");
}
@IocBean(name = "elasticsearchClient", depose = "close")
public RestHighLevelClient getElasticsearchClient() {
log.debug("loading elasticsearchClient...");
final String hosts_str = conf.get(PROP_HOST, "127.0.0.1:9200");
List<HttpHost> httpHostList = new ArrayList<>();
if (hosts_str.indexOf(",") != -1) {
for (String ht : hosts_str.split(",")) {
httpHostList.add(new HttpHost(ht.split(":")[0], Integer.valueOf(ht.split(":")[1])));
}
} else {
httpHostList.add(new HttpHost(hosts_str.split(":")[0], Integer.valueOf(hosts_str.split(":")[1])));
}
HttpHost[] httpHosts = new HttpHost[httpHostList.size()];
for (int i = 0; i < httpHostList.size(); i++) {
httpHosts[i] = httpHostList.get(i);
}
final RestClientBuilder builder = RestClient.builder(httpHosts);
return new RestHighLevelClient(builder);
}
}

View File

@ -0,0 +1 @@
org.nutz.boot.starter.elasticsearch.rest.ElasticsearchStarter

View File

@ -85,6 +85,7 @@
<module>nutzboot-starter-swagger3</module>
<module>nutzboot-starter-jasypt</module>
<module>nutzboot-starter-test</module>
<module>nutzboot-starter-elasticsearch-rest</module>
</modules>
<dependencies>
<dependency>

View File

@ -40,7 +40,7 @@
<HikariCP.version>3.2.0</HikariCP.version>
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<quartz.version>2.3.2</quartz.version>
<elasticsearch.version>6.3.2</elasticsearch.version>
<elasticsearch.version>7.14.0</elasticsearch.version>
<commons.email.version>1.4</commons.email.version>
<commons.net.version>3.6</commons.net.version>
<javassist.version>3.24.0-GA</javassist.version>