mirror of
https://github.com/201206030/novel-cloud.git
synced 2025-04-27 01:40:50 +00:00
elasticsearch升级到7.x,集成RestHighLevelClient客户端
This commit is contained in:
parent
b4c3d749df
commit
67ec89188c
@ -35,7 +35,6 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.redisson</groupId>
|
<groupId>org.redisson</groupId>
|
||||||
<artifactId>redisson-spring-boot-starter</artifactId>
|
<artifactId>redisson-spring-boot-starter</artifactId>
|
||||||
<version>${redisson.version}</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- 分库分表-->
|
<!-- 分库分表-->
|
||||||
@ -44,13 +43,11 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.shardingsphere</groupId>
|
<groupId>io.shardingsphere</groupId>
|
||||||
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
|
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
|
||||||
<version>${sharding.jdbc.version}</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.shardingsphere</groupId>
|
<groupId>io.shardingsphere</groupId>
|
||||||
<artifactId>sharding-jdbc-spring-namespace</artifactId>
|
<artifactId>sharding-jdbc-spring-namespace</artifactId>
|
||||||
<version>${sharding.jdbc.version}</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
@ -27,10 +27,13 @@
|
|||||||
<artifactId>spring-cloud-netflix-hystrix</artifactId>
|
<artifactId>spring-cloud-netflix-hystrix</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.elasticsearch.client</groupId>
|
||||||
|
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.searchbox</groupId>
|
<groupId>io.searchbox</groupId>
|
||||||
<artifactId>jest</artifactId>
|
<artifactId>jest</artifactId>
|
||||||
<version>${jest.version}</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -42,7 +45,6 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.redisson</groupId>
|
<groupId>org.redisson</groupId>
|
||||||
<artifactId>redisson-spring-boot-starter</artifactId>
|
<artifactId>redisson-spring-boot-starter</artifactId>
|
||||||
<version>${redisson.version}</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package com.java2nb.novel;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
@ -14,6 +15,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
|
|||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableFeignClients
|
@EnableFeignClients
|
||||||
@EnableScheduling
|
@EnableScheduling
|
||||||
|
@ServletComponentScan
|
||||||
public class SearchApplication {
|
public class SearchApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.java2nb.novel.search.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
|
||||||
|
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));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -17,6 +17,11 @@ 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;
|
||||||
@ -42,12 +47,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";
|
||||||
|
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@Override
|
@Override
|
||||||
@ -57,9 +62,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());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,7 +124,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();
|
||||||
@ -143,7 +152,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);
|
||||||
|
19
pom.xml
19
pom.xml
@ -52,7 +52,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.10.6</jjwt.version>
|
<jjwt.version>0.10.6</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>
|
||||||
@ -191,6 +191,11 @@
|
|||||||
<artifactId>spring-boot-starter-redis</artifactId>
|
<artifactId>spring-boot-starter-redis</artifactId>
|
||||||
<version>${redis.version}</version>
|
<version>${redis.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.redisson</groupId>
|
||||||
|
<artifactId>redisson-spring-boot-starter</artifactId>
|
||||||
|
<version>${redisson.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!--mysql驱动-->
|
<!--mysql驱动-->
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -274,6 +279,18 @@
|
|||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--elasticsearch -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.elasticsearch.client</groupId>
|
||||||
|
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||||
|
<version>${elasticsearch.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.searchbox</groupId>
|
||||||
|
<artifactId>jest</artifactId>
|
||||||
|
<version>${jest.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!--集成logstash-->
|
<!--集成logstash-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.logstash.logback</groupId>
|
<groupId>net.logstash.logback</groupId>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user