elasticsearch升级到7.x,集成RestHighLevelClient客户端

This commit is contained in:
xiongxiaoyang 2021-01-03 13:42:40 +08:00
parent ab741ec6bf
commit e4822979e2
7 changed files with 70 additions and 21 deletions

View File

@ -39,6 +39,12 @@
<version>${jest.version}</version> <version>${jest.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<!--aliyunOSS--> <!--aliyunOSS-->
<dependency> <dependency>
<groupId>com.aliyun.oss</groupId> <groupId>com.aliyun.oss</groupId>

View File

@ -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));
}
}

View File

@ -2,19 +2,11 @@ package com.java2nb.novel.core.schedule;
import com.java2nb.novel.core.cache.CacheKey; import com.java2nb.novel.core.cache.CacheKey;
import com.java2nb.novel.core.cache.CacheService; import com.java2nb.novel.core.cache.CacheService;
import com.java2nb.novel.core.utils.BeanUtil;
import com.java2nb.novel.entity.Book; import com.java2nb.novel.entity.Book;
import com.java2nb.novel.service.BookService; import com.java2nb.novel.service.BookService;
import com.java2nb.novel.service.SearchService; 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.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; 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.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -68,8 +60,6 @@ public class BookToEsSchedule {
} }
cacheService.setObject(CacheKey.ES_LAST_UPDATE_TIME, lastDate); cacheService.setObject(CacheKey.ES_LAST_UPDATE_TIME, lastDate);
} catch (Exception e) { } catch (Exception e) {

View File

@ -600,6 +600,7 @@ public class BookServiceImpl implements BookService {
return bookMapper.selectMany(select(book.allColumns()) return bookMapper.selectMany(select(book.allColumns())
.from(book) .from(book)
.where(updateTime, isGreaterThan(startDate)) .where(updateTime, isGreaterThan(startDate))
.and(lastIndexId,isNotNull())
.orderBy(updateTime) .orderBy(updateTime)
.limit(limit) .limit(limit)
.build() .build()

View File

@ -11,11 +11,19 @@ import com.java2nb.novel.search.BookSP;
import com.java2nb.novel.service.SearchService; import com.java2nb.novel.service.SearchService;
import com.java2nb.novel.vo.EsBookVO; import com.java2nb.novel.vo.EsBookVO;
import io.searchbox.client.JestClient; 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.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; 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.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder;
@ -37,12 +45,12 @@ import java.util.Map;
@Slf4j @Slf4j
public class SearchServiceImpl implements SearchService { public class SearchServiceImpl implements SearchService {
private final String INDEX = "novel";
private final String TYPE = "book";
private final JestClient jestClient; private final JestClient jestClient;
private final RestHighLevelClient restHighLevelClient;
private final String INDEX = "novel";
@Override @Override
@SneakyThrows @SneakyThrows
@ -51,10 +59,13 @@ public class SearchServiceImpl implements SearchService {
EsBookVO esBookVO = new EsBookVO(); EsBookVO esBookVO = new EsBookVO();
BeanUtils.copyProperties(book, esBookVO, "lastIndexUpdateTime"); BeanUtils.copyProperties(book, esBookVO, "lastIndexUpdateTime");
esBookVO.setLastIndexUpdateTime(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(book.getLastIndexUpdateTime())); 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); searchSourceBuilder.query(boolQueryBuilder);
Count count = new Count.Builder().addIndex(INDEX).addType(TYPE) Count count = new Count.Builder().addIndex(INDEX)
.query(searchSourceBuilder.toString()).build(); .query(searchSourceBuilder.toString()).build();
CountResult results = jestClient.execute(count); CountResult results = jestClient.execute(count);
Double total = results.getCount(); Double total = results.getCount();
@ -130,7 +141,7 @@ public class SearchServiceImpl implements SearchService {
searchSourceBuilder.size(pageSize); searchSourceBuilder.size(pageSize);
// 构建Search对象 // 构建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()); log.debug(search.toString());
SearchResult result; SearchResult result;
result = jestClient.execute(search); result = jestClient.execute(search);

View File

@ -23,7 +23,7 @@ spring:
#是否开启搜索引擎1开启0不开启 #是否开启搜索引擎1开启0不开启
enable: 0 enable: 0
jest: jest:
uris: http://127.0.0.1:9200 uris: http://192.168.0.105:9200
#thymeleaf模版路径配置 #thymeleaf模版路径配置
thymeleaf: thymeleaf:

View File

@ -37,7 +37,7 @@
<orderbyhelper.version>1.0.2</orderbyhelper.version> <orderbyhelper.version>1.0.2</orderbyhelper.version>
<commons-lang3.version>3.4</commons-lang3.version> <commons-lang3.version>3.4</commons-lang3.version>
<jjwt.version>0.9.0</jjwt.version> <jjwt.version>0.9.0</jjwt.version>
<elasticsearch.version>6.2.2</elasticsearch.version> <elasticsearch.version>7.9.3</elasticsearch.version>
<jest.version>6.3.1</jest.version> <jest.version>6.3.1</jest.version>
<redis.version>1.4.1.RELEASE</redis.version> <redis.version>1.4.1.RELEASE</redis.version>
<redisson.version>3.12.5</redisson.version> <redisson.version>3.12.5</redisson.version>