From e4822979e2fa46fe0cdce48693de635964d7b935 Mon Sep 17 00:00:00 2001 From: xiongxiaoyang <773861846@qq.com> Date: Sun, 3 Jan 2021 13:42:40 +0800 Subject: [PATCH] =?UTF-8?q?elasticsearch=E5=8D=87=E7=BA=A7=E5=88=B07.x?= =?UTF-8?q?=EF=BC=8C=E9=9B=86=E6=88=90RestHighLevelClient=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- novel-front/pom.xml | 6 +++ .../java2nb/novel/core/config/EsConfig.java | 41 +++++++++++++++++++ .../novel/core/schedule/BookToEsSchedule.java | 10 ----- .../novel/service/impl/BookServiceImpl.java | 1 + .../novel/service/impl/SearchServiceImpl.java | 29 +++++++++---- .../src/main/resources/application.yml | 2 +- pom.xml | 2 +- 7 files changed, 70 insertions(+), 21 deletions(-) create mode 100644 novel-front/src/main/java/com/java2nb/novel/core/config/EsConfig.java diff --git a/novel-front/pom.xml b/novel-front/pom.xml index 51542f8..df452e2 100644 --- a/novel-front/pom.xml +++ b/novel-front/pom.xml @@ -39,6 +39,12 @@ ${jest.version} + + org.elasticsearch.client + elasticsearch-rest-high-level-client + ${elasticsearch.version} + + com.aliyun.oss diff --git a/novel-front/src/main/java/com/java2nb/novel/core/config/EsConfig.java b/novel-front/src/main/java/com/java2nb/novel/core/config/EsConfig.java new file mode 100644 index 0000000..494aafe --- /dev/null +++ b/novel-front/src/main/java/com/java2nb/novel/core/config/EsConfig.java @@ -0,0 +1,41 @@ +package com.java2nb.novel.core.config; + + +import org.apache.http.HttpHost; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestHighLevelClient; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * elasticsearch搜索引擎配置 + * @author xiongxiaoyang + */ +@Configuration +@ConditionalOnProperty(prefix = "spring.elasticsearch", name = "enable", havingValue = "1") +public class EsConfig { + + @Value("${spring.elasticsearch.jest.uris}") + private String esUris; + + @Bean + public RestHighLevelClient esClient(){ + + + String[] uris = esUris.split(","); + HttpHost[] hosts = new HttpHost[uris.length]; + for(int i = 0 ; i < uris.length ; i++){ + String uri = uris[i]; + String scheme = uri.substring(0,uri.indexOf(":")).trim(); + String hostname = uri.substring(uri.indexOf("://")+3,uri.lastIndexOf(":")).trim(); + Integer port = Integer.parseInt(uri.substring(uri.lastIndexOf(":")+1).trim()); + hosts[i] = new HttpHost(hostname,port,scheme); + } + + return new RestHighLevelClient( + RestClient.builder(hosts)); + + } +} diff --git a/novel-front/src/main/java/com/java2nb/novel/core/schedule/BookToEsSchedule.java b/novel-front/src/main/java/com/java2nb/novel/core/schedule/BookToEsSchedule.java index a179255..8300e2b 100644 --- a/novel-front/src/main/java/com/java2nb/novel/core/schedule/BookToEsSchedule.java +++ b/novel-front/src/main/java/com/java2nb/novel/core/schedule/BookToEsSchedule.java @@ -2,19 +2,11 @@ package com.java2nb.novel.core.schedule; import com.java2nb.novel.core.cache.CacheKey; import com.java2nb.novel.core.cache.CacheService; -import com.java2nb.novel.core.utils.BeanUtil; import com.java2nb.novel.entity.Book; import com.java2nb.novel.service.BookService; import com.java2nb.novel.service.SearchService; -import com.java2nb.novel.vo.EsBookVO; -import io.searchbox.client.JestClient; -import io.searchbox.core.DocumentResult; -import io.searchbox.core.Index; import lombok.RequiredArgsConstructor; -import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -68,8 +60,6 @@ public class BookToEsSchedule { } - - cacheService.setObject(CacheKey.ES_LAST_UPDATE_TIME, lastDate); } catch (Exception e) { diff --git a/novel-front/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java b/novel-front/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java index baae58b..8278be7 100644 --- a/novel-front/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java +++ b/novel-front/src/main/java/com/java2nb/novel/service/impl/BookServiceImpl.java @@ -600,6 +600,7 @@ public class BookServiceImpl implements BookService { return bookMapper.selectMany(select(book.allColumns()) .from(book) .where(updateTime, isGreaterThan(startDate)) + .and(lastIndexId,isNotNull()) .orderBy(updateTime) .limit(limit) .build() diff --git a/novel-front/src/main/java/com/java2nb/novel/service/impl/SearchServiceImpl.java b/novel-front/src/main/java/com/java2nb/novel/service/impl/SearchServiceImpl.java index 2bdd404..7d7a35c 100644 --- a/novel-front/src/main/java/com/java2nb/novel/service/impl/SearchServiceImpl.java +++ b/novel-front/src/main/java/com/java2nb/novel/service/impl/SearchServiceImpl.java @@ -11,11 +11,19 @@ import com.java2nb.novel.search.BookSP; import com.java2nb.novel.service.SearchService; import com.java2nb.novel.vo.EsBookVO; import io.searchbox.client.JestClient; -import io.searchbox.core.*; +import io.searchbox.core.Count; +import io.searchbox.core.CountResult; +import io.searchbox.core.Search; +import io.searchbox.core.SearchResult; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.builder.SearchSourceBuilder; @@ -37,12 +45,12 @@ import java.util.Map; @Slf4j public class SearchServiceImpl implements SearchService { - private final String INDEX = "novel"; - - private final String TYPE = "book"; - private final JestClient jestClient; + private final RestHighLevelClient restHighLevelClient; + + private final String INDEX = "novel"; + @Override @SneakyThrows @@ -51,10 +59,13 @@ public class SearchServiceImpl implements SearchService { EsBookVO esBookVO = new EsBookVO(); BeanUtils.copyProperties(book, esBookVO, "lastIndexUpdateTime"); esBookVO.setLastIndexUpdateTime(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(book.getLastIndexUpdateTime())); - Index action = new Index.Builder(esBookVO).index(INDEX).type(TYPE).id(book.getId().toString()).build(); - jestClient.execute(action); + IndexRequest request = new IndexRequest(INDEX); + request.id(book.getId()+""); + request.source(new ObjectMapper().writeValueAsString(esBookVO), XContentType.JSON); + IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT); + log.debug(index.getResult().toString()); } @@ -102,7 +113,7 @@ public class SearchServiceImpl implements SearchService { searchSourceBuilder.query(boolQueryBuilder); - Count count = new Count.Builder().addIndex(INDEX).addType(TYPE) + Count count = new Count.Builder().addIndex(INDEX) .query(searchSourceBuilder.toString()).build(); CountResult results = jestClient.execute(count); Double total = results.getCount(); @@ -130,7 +141,7 @@ public class SearchServiceImpl implements SearchService { searchSourceBuilder.size(pageSize); // 构建Search对象 - Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(INDEX).addType(TYPE).build(); + Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(INDEX).build(); log.debug(search.toString()); SearchResult result; result = jestClient.execute(search); diff --git a/novel-front/src/main/resources/application.yml b/novel-front/src/main/resources/application.yml index 2b331cc..bd6fe69 100644 --- a/novel-front/src/main/resources/application.yml +++ b/novel-front/src/main/resources/application.yml @@ -23,7 +23,7 @@ spring: #是否开启搜索引擎,1:开启,0:不开启 enable: 0 jest: - uris: http://127.0.0.1:9200 + uris: http://192.168.0.105:9200 #thymeleaf模版路径配置 thymeleaf: diff --git a/pom.xml b/pom.xml index 65f22c9..37de895 100644 --- a/pom.xml +++ b/pom.xml @@ -37,7 +37,7 @@ 1.0.2 3.4 0.9.0 - 6.2.2 + 7.9.3 6.3.1 1.4.1.RELEASE 3.12.5