更新es的远程连接
parent
bdf374f035
commit
03cb49cc0d
|
@ -2,12 +2,12 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.bawei</groupId>
|
|
||||||
<artifactId>base-es</artifactId>
|
<artifactId>base-es</artifactId>
|
||||||
|
<groupId>com.bawei</groupId>
|
||||||
<version>3.6.0</version>
|
<version>3.6.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>base-es-common</artifactId>
|
<artifactId>base-es-common</artifactId>
|
||||||
|
|
||||||
|
@ -16,53 +16,13 @@
|
||||||
<maven.compiler.target>8</maven.compiler.target>
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
<dependencies>
|
|
||||||
<!-- 项目公共核心依赖 -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.bawei</groupId>
|
|
||||||
<artifactId>bawei-common-core</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.elasticsearch.client</groupId>
|
|
||||||
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<!-- SpringCloud Alibaba Nacos -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.alibaba.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- SpringCloud Alibaba Nacos Config -->
|
<dependencies>
|
||||||
<dependency>
|
|
||||||
<groupId>com.alibaba.cloud</groupId>
|
|
||||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- SpringCloud Alibaba Sentinel -->
|
<!-- 项目公共核心依赖 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba.cloud</groupId>
|
<groupId>com.bawei</groupId>
|
||||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
<artifactId>bawei-common-core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
<!-- SpringBoot Actuator -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
|
|
||||||
<!-- Minio -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>io.minio</groupId>
|
|
||||||
<artifactId>minio</artifactId>
|
|
||||||
<version>${minio.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- BaWei Common Swagger -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.bawei</groupId>
|
|
||||||
<artifactId>bawei-common-swagger</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -1,36 +0,0 @@
|
||||||
package com.bawei.es.config;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.extern.log4j.Log4j2;
|
|
||||||
import org.apache.http.HttpHost;
|
|
||||||
import org.elasticsearch.client.RestClient;
|
|
||||||
import org.elasticsearch.client.RestClientBuilder;
|
|
||||||
import org.elasticsearch.client.RestHighLevelClient;
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @BelongsProject: day20k-6.12
|
|
||||||
* @BelongsPackage: com.bwie.es.conifg
|
|
||||||
* @Author: LiFan
|
|
||||||
* @CreateTime: 2023-06-12 14:33
|
|
||||||
* @Description: //TODO
|
|
||||||
* @Version: 1.0
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
@ConfigurationProperties(prefix = "es")
|
|
||||||
@Data
|
|
||||||
@Log4j2
|
|
||||||
public class InitEsRestHighLevelClient {
|
|
||||||
private String host;
|
|
||||||
private Integer port;
|
|
||||||
private String scheme;
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public RestHighLevelClient RestHighLevelClient() {
|
|
||||||
return new RestHighLevelClient(
|
|
||||||
RestClient.builder(new HttpHost(host,port,scheme))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.bawei.es.domain;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: 31966
|
||||||
|
* @Project: mall-product
|
||||||
|
* @Package: com.bawei.es.domain
|
||||||
|
* @ClassName: CommonMap
|
||||||
|
* @Description: 请在此处填写具体描述
|
||||||
|
* @Date: 2024/4/23 14:29
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class CommonMap {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 个数
|
||||||
|
*/
|
||||||
|
private long total;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询数据
|
||||||
|
*/
|
||||||
|
private Map<String,Object> objectMap;
|
||||||
|
}
|
|
@ -1,19 +0,0 @@
|
||||||
package com.bawei.es.domain;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 文档类
|
|
||||||
*
|
|
||||||
* @program: mall_cloud
|
|
||||||
* @ClassName: DocumentClass
|
|
||||||
* @author: Gyc
|
|
||||||
* @create: 2024-04-24 20:27
|
|
||||||
**/
|
|
||||||
@Data
|
|
||||||
public class DocumentClass {
|
|
||||||
private String indexName;
|
|
||||||
private String shopName;
|
|
||||||
private Page page;
|
|
||||||
private Sort sort;
|
|
||||||
}
|
|
|
@ -1,21 +0,0 @@
|
||||||
package com.bawei.es.domain;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 分页实体类
|
|
||||||
*
|
|
||||||
* @program: mall_cloud
|
|
||||||
* @ClassName: Page
|
|
||||||
* @author: Gyc
|
|
||||||
* @create: 2024-04-24 19:41
|
|
||||||
**/
|
|
||||||
@Data
|
|
||||||
@AllArgsConstructor
|
|
||||||
@NoArgsConstructor
|
|
||||||
public class Page {
|
|
||||||
private Integer pageNum=1;
|
|
||||||
private Integer pageSize=5;
|
|
||||||
}
|
|
|
@ -1,15 +0,0 @@
|
||||||
package com.bawei.es.domain;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @program: mall_cloud
|
|
||||||
* @ClassName: Sort
|
|
||||||
* @author: Gyc
|
|
||||||
* @create: 2024-04-24 20:28
|
|
||||||
**/
|
|
||||||
@Data
|
|
||||||
public class Sort {
|
|
||||||
private String sortName;
|
|
||||||
private String sortOrder;
|
|
||||||
}
|
|
|
@ -2,12 +2,12 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.bawei</groupId>
|
|
||||||
<artifactId>base-es</artifactId>
|
<artifactId>base-es</artifactId>
|
||||||
|
<groupId>com.bawei</groupId>
|
||||||
<version>3.6.0</version>
|
<version>3.6.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>base-es-remote</artifactId>
|
<artifactId>base-es-remote</artifactId>
|
||||||
|
|
||||||
|
@ -17,4 +17,12 @@
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- 文件服务公共依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bawei</groupId>
|
||||||
|
<artifactId>base-es-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.bawei.es.remote.api;
|
||||||
|
|
||||||
|
import com.bawei.common.core.constant.ServiceNameConstants;
|
||||||
|
import com.bawei.common.core.domain.R;
|
||||||
|
import com.bawei.es.remote.factory.RemoteDocumentFallbackFactory;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件服务
|
||||||
|
*
|
||||||
|
* @author bawei
|
||||||
|
*/
|
||||||
|
@FeignClient(contextId = "remoteDocumentService", value = ServiceNameConstants.ES_SERVICE, fallbackFactory = RemoteDocumentFallbackFactory.class)
|
||||||
|
public interface RemoteDocumentService
|
||||||
|
{
|
||||||
|
@PostMapping("/doc/{indexName}")
|
||||||
|
R queryList(@PathVariable String indexName, @RequestBody Map<String,Object> conditions);
|
||||||
|
|
||||||
|
@PutMapping("/doc/{indexName}")
|
||||||
|
R batchDocument(@PathVariable String indexName, @RequestBody List<String> dataJson);
|
||||||
|
|
||||||
|
@DeleteMapping("/doc/{indexName}")
|
||||||
|
R batchDeleteDocument(@PathVariable String indexName, @RequestBody List<String> dataJson);
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.bawei.es.remote.api;
|
||||||
|
|
||||||
|
import com.bawei.common.core.constant.ServiceNameConstants;
|
||||||
|
import com.bawei.common.core.domain.R;
|
||||||
|
import com.bawei.es.remote.factory.RemoteIndexFallbackFactory;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件服务
|
||||||
|
*
|
||||||
|
* @author bawei
|
||||||
|
*/
|
||||||
|
@FeignClient(contextId = "remoteIndexService", value = ServiceNameConstants.ES_SERVICE, fallbackFactory = RemoteIndexFallbackFactory.class)
|
||||||
|
public interface RemoteIndexService
|
||||||
|
{
|
||||||
|
@GetMapping("/index/{indexName}")
|
||||||
|
public R exist(@PathVariable String indexName);
|
||||||
|
|
||||||
|
@GetMapping("/index/getMapping/{indexName}")
|
||||||
|
public R getMapping(@PathVariable String indexName);
|
||||||
|
|
||||||
|
@PostMapping("/index/{indexName}")
|
||||||
|
public R index(@PathVariable String indexName,@RequestBody Map<String,Object> mappings);
|
||||||
|
|
||||||
|
@PutMapping("/index/{indexName}")
|
||||||
|
public R updateIndex(@PathVariable String indexName,@RequestBody Map<String,Object> mappings);
|
||||||
|
|
||||||
|
@DeleteMapping("/index/{indexName}")
|
||||||
|
public R deleteIndex(@PathVariable String indexName);
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.bawei.es.remote.factory;
|
||||||
|
|
||||||
|
import com.bawei.common.core.domain.R;
|
||||||
|
import com.bawei.es.remote.api.RemoteDocumentService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件服务降级处理
|
||||||
|
*
|
||||||
|
* @author bawei
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class RemoteDocumentFallbackFactory implements FallbackFactory<RemoteDocumentService>
|
||||||
|
{
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(RemoteDocumentFallbackFactory.class);
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RemoteDocumentService create(Throwable cause) {
|
||||||
|
return new RemoteDocumentService() {
|
||||||
|
@Override
|
||||||
|
public R queryList(String indexName, Map<String, Object> conditions) {
|
||||||
|
return R.fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public R batchDocument(String indexName, List<String> dataJson) {
|
||||||
|
return R.fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public R batchDeleteDocument(String indexName, List<String> dataJson) {
|
||||||
|
return R.fail();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
package com.bawei.es.remote.factory;
|
||||||
|
|
||||||
|
import com.bawei.common.core.domain.R;
|
||||||
|
import com.bawei.es.remote.api.RemoteIndexService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件服务降级处理
|
||||||
|
*
|
||||||
|
* @author bawei
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class RemoteIndexFallbackFactory implements FallbackFactory<RemoteIndexService>
|
||||||
|
{
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(RemoteIndexFallbackFactory.class);
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RemoteIndexService create(Throwable cause) {
|
||||||
|
return new RemoteIndexService() {
|
||||||
|
@Override
|
||||||
|
public R exist(String indexName) {
|
||||||
|
return R.fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public R getMapping(String indexName) {
|
||||||
|
return R.fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public R index(String indexName, Map<String, Object> mappings) {
|
||||||
|
return R.fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public R updateIndex(String indexName, Map<String, Object> mappings) {
|
||||||
|
return R.fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public R deleteIndex(String indexName) {
|
||||||
|
return R.fail();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -1 +1,2 @@
|
||||||
|
com.bawei.es.remote.factory.RemoteIndexFallbackFactory
|
||||||
|
com.bawei.es.remote.factory.RemoteDocumentFallbackFactory
|
||||||
|
|
|
@ -2,12 +2,12 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.bawei</groupId>
|
|
||||||
<artifactId>base-es</artifactId>
|
<artifactId>base-es</artifactId>
|
||||||
|
<groupId>com.bawei</groupId>
|
||||||
<version>3.6.0</version>
|
<version>3.6.0</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<artifactId>base-es-server</artifactId>
|
<artifactId>base-es-server</artifactId>
|
||||||
|
|
||||||
|
@ -16,15 +16,54 @@
|
||||||
<maven.compiler.target>8</maven.compiler.target>
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
|
<!-- SpringCloud Alibaba Nacos -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- SpringCloud Alibaba Nacos Config -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- SpringCloud Alibaba Sentinel -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- SpringBoot Actuator -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- BaWei Common Swagger -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bawei</groupId>
|
||||||
|
<artifactId>bawei-common-swagger</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.bawei</groupId>
|
<groupId>com.bawei</groupId>
|
||||||
<artifactId>base-es-common</artifactId>
|
<artifactId>bawei-common-security</artifactId>
|
||||||
<version>3.6.0</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.elasticsearch.client</groupId>
|
||||||
|
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- BaWei Common DataScope -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bawei</groupId>
|
||||||
|
<artifactId>base-es-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
|
@ -5,20 +5,13 @@ import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
|
|
||||||
/**
|
|
||||||
* es服务
|
|
||||||
*
|
|
||||||
* @program: mall_cloud
|
|
||||||
* @ClassName: BaWeiEsApplication
|
|
||||||
* @author: Gyc
|
|
||||||
* @create: 2024-04-22 20:39
|
|
||||||
**/
|
|
||||||
@EnableCustomSwagger2
|
@EnableCustomSwagger2
|
||||||
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
|
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
|
||||||
public class BaWeiEsApplication {
|
public class BaWeiEsApplication {
|
||||||
public static void main(String[] args)
|
|
||||||
{
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(BaWeiEsApplication.class, args);
|
SpringApplication.run(BaWeiEsApplication.class, args);
|
||||||
System.out.println("(♥◠‿◠)ノ゙ es服务模块启动成功 ლ(´ڡ`ლ)゙ ");
|
System.out.println("(♥◠‿◠)ノ゙ es服务模块启动成功 ლ(´ڡ`ლ)゙ ");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
package com.bawei.es.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.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: 31966
|
||||||
|
* @Project: mall-product
|
||||||
|
* @Package: com.bwie.es.config
|
||||||
|
* @ClassName: ElasticSearchConfig
|
||||||
|
* @Description: 请在此处填写具体描述
|
||||||
|
* @Date: 2024/4/22 20:50
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
public class ElasticSearchConfig {
|
||||||
|
|
||||||
|
@Value("${es.host}")
|
||||||
|
private String host;
|
||||||
|
|
||||||
|
@Value("${es.port}")
|
||||||
|
private int port;
|
||||||
|
|
||||||
|
@Value("${es.scheme}")
|
||||||
|
private String scheme;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RestHighLevelClient restHighLevelClient(){
|
||||||
|
return new RestHighLevelClient(
|
||||||
|
RestClient.builder(new HttpHost(host, port, scheme)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHost() {
|
||||||
|
return host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHost(String host) {
|
||||||
|
this.host = host;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPort() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPort(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getScheme() {
|
||||||
|
return scheme;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScheme(String scheme) {
|
||||||
|
this.scheme = scheme;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,57 +0,0 @@
|
||||||
package com.bawei.es.controller;
|
|
||||||
|
|
||||||
import com.bawei.common.core.domain.R;
|
|
||||||
import com.bawei.es.domain.DocumentClass;
|
|
||||||
import com.bawei.es.service.DocService;
|
|
||||||
import io.swagger.annotations.Api;
|
|
||||||
import io.swagger.annotations.ApiOperation;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.util.CollectionUtils;
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@RestController("/doc")
|
|
||||||
@Api(value = "es文档操作",tags = "首页")
|
|
||||||
public class DocController {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private DocService docService;
|
|
||||||
|
|
||||||
@ApiOperation(value = "文档查询接口")
|
|
||||||
@PostMapping("/search")
|
|
||||||
public R search(@RequestBody DocumentClass documentClass) {
|
|
||||||
return R.ok(docService.search(documentClass));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@ApiOperation(value = "增加es文档")
|
|
||||||
@PostMapping("/createDoc/{indexName}")
|
|
||||||
public R createDoc(@PathVariable String indexName, @RequestBody Map<String,Object> document){
|
|
||||||
if (document!=null && !CollectionUtils.isEmpty(document)){
|
|
||||||
return R.ok(docService.createDoc(indexName,document));
|
|
||||||
}
|
|
||||||
throw new RuntimeException("添加失败,文档不可为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@ApiOperation(value = "修改es文档")
|
|
||||||
@PutMapping("/putDoc/{indexName}")
|
|
||||||
public R putDoc(@PathVariable String indexName,@RequestBody Map<String,Object> document){
|
|
||||||
if (document!=null && !CollectionUtils.isEmpty(document)){
|
|
||||||
return R.ok(docService.putDoc(indexName,document));
|
|
||||||
}
|
|
||||||
throw new RuntimeException("修改失败,文档不可为空");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@ApiOperation(value = "删除es文档")
|
|
||||||
@DeleteMapping("/deleteDoc/{indexName}/{id}")
|
|
||||||
public R deleteDoc(@PathVariable String indexName, @PathVariable String id){
|
|
||||||
if (id!=null){
|
|
||||||
return R.ok(docService.deleteDoc(indexName,id));
|
|
||||||
}
|
|
||||||
throw new RuntimeException("文档ID不能为空");
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
package com.bawei.es.controller;
|
||||||
|
|
||||||
|
import com.bawei.common.core.domain.R;
|
||||||
|
import com.bawei.es.service.DocumentService;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: 31966
|
||||||
|
* @Project: mall-product
|
||||||
|
* @Package: com.bwie.es.controller
|
||||||
|
* @ClassName: DocumentController
|
||||||
|
* @Description: 请在此处填写具体描述
|
||||||
|
* @Date: 2024/4/22 20:57
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/doc")
|
||||||
|
@Api(tags = "es文档接口")
|
||||||
|
public class DocumentController {
|
||||||
|
|
||||||
|
@Resource(name = "documentServiceImpl")
|
||||||
|
private DocumentService documentService;
|
||||||
|
|
||||||
|
@PostMapping("/{indexName}")
|
||||||
|
@ApiOperation(value = "条件查询文档数据")
|
||||||
|
public R queryList(@PathVariable String indexName, @RequestBody Map<String,Object> conditions){
|
||||||
|
return documentService.queryList(indexName,conditions);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{indexName}")
|
||||||
|
@ApiOperation(value = "批量操作数据")
|
||||||
|
public R batchDocument(@PathVariable String indexName, @RequestBody Map<String,Object> dataJson){
|
||||||
|
documentService.batchOperation(indexName,dataJson);
|
||||||
|
return R.ok(null, "批量操作成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{indexName}")
|
||||||
|
@ApiOperation(value = "批量删除")
|
||||||
|
public R batchDeleteDocument(@PathVariable String indexName, @RequestBody List<String> dataJson){
|
||||||
|
documentService.batchDeletion(indexName,dataJson);
|
||||||
|
return R.ok(null, "批量删除成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -4,44 +4,55 @@ import com.bawei.common.core.domain.R;
|
||||||
import com.bawei.es.service.IndexService;
|
import com.bawei.es.service.IndexService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author gyc
|
* @Author: 31966
|
||||||
|
* @Project: mall-product
|
||||||
|
* @Package: com.bwie.es.controller
|
||||||
|
* @ClassName: IndexController
|
||||||
|
* @Description: 请在此处填写具体描述
|
||||||
|
* @Date: 2024/4/22 20:45
|
||||||
*/
|
*/
|
||||||
@RestController("/index")
|
@RestController
|
||||||
@Api(value = "es索引操作",tags = "首页")
|
@RequestMapping("/index")
|
||||||
|
@Api(tags = "es索引接口")
|
||||||
public class IndexController {
|
public class IndexController {
|
||||||
@Autowired
|
|
||||||
|
@Resource(name = "indexServiceImpl")
|
||||||
private IndexService indexService;
|
private IndexService indexService;
|
||||||
|
|
||||||
@ApiOperation(value = "根据索引名称查询该索引是否存在")
|
@GetMapping("/{indexName}")
|
||||||
@GetMapping("/index/{indexName}")
|
@ApiOperation(value = "查询es索引是否存在")
|
||||||
public R index(@PathVariable String indexName){
|
public R exist(@PathVariable String indexName){
|
||||||
return R.ok(indexService.exits(indexName));
|
return R.ok(indexService.exist(indexName),"es索引存在");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/getMapping/{indexName}")
|
||||||
@ApiOperation(value = "创建索引")
|
@ApiOperation(value = "获取索引映射")
|
||||||
@PostMapping("/create/{indexName}")
|
public R getMapping(@PathVariable String indexName){
|
||||||
public R create(@PathVariable String indexName, @RequestBody Map<String,Object> mappings){
|
return R.ok(indexService.getMapping(indexName),"索引映射存在");
|
||||||
return R.ok(indexService.create(indexName,mappings));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/{indexName}")
|
||||||
@ApiOperation(value = "更新索引")
|
@ApiOperation(value = "添加索引")
|
||||||
@PutMapping("/update/{indexName}")
|
public R index(@PathVariable String indexName,@RequestBody Map<String,Object> mappings){
|
||||||
public R update(@PathVariable String indexName,@RequestBody Map<String,Object> mappings){
|
return R.ok(indexService.index(indexName,mappings),"es索引添加成功");
|
||||||
return R.ok(indexService.update(indexName,mappings));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{indexName}")
|
||||||
|
@ApiOperation(value = "修改索引")
|
||||||
|
public R updateIndex(@PathVariable String indexName,@RequestBody Map<String,Object> mappings){
|
||||||
|
return R.ok(indexService.updataIndex(indexName,mappings),"es索引修改成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{indexName}")
|
||||||
@ApiOperation(value = "删除索引")
|
@ApiOperation(value = "删除索引")
|
||||||
@DeleteMapping("/delete/{indexName}")
|
public R deleteIndex(@PathVariable String indexName){
|
||||||
public R delete(@PathVariable String indexName){
|
return R.ok(indexService.deleteIndex(indexName),"es索引删除成功");
|
||||||
return R.ok(indexService.delete(indexName));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +0,0 @@
|
||||||
package com.bawei.es.service;
|
|
||||||
|
|
||||||
import com.bawei.common.core.domain.R;
|
|
||||||
import com.bawei.es.domain.DocumentClass;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public interface DocService {
|
|
||||||
R getDocByIndexName(String indexName, String id);
|
|
||||||
|
|
||||||
R createDoc(String indexName, Map<String, Object> document);
|
|
||||||
|
|
||||||
R putDoc(String indexName, Map<String, Object> document);
|
|
||||||
|
|
||||||
R deleteDoc(String indexName, String id);
|
|
||||||
|
|
||||||
HashMap<String,Object> search(DocumentClass documentClass);
|
|
||||||
}
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.bawei.es.service;
|
||||||
|
|
||||||
|
import com.bawei.common.core.domain.R;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: 31966
|
||||||
|
* @Project: mall-product
|
||||||
|
* @Package: com.bwie.es.service
|
||||||
|
* @ClassName: DocumentService
|
||||||
|
* @Description: 请在此处填写具体描述
|
||||||
|
* @Date: 2024/4/22 20:58
|
||||||
|
*/
|
||||||
|
public interface DocumentService {
|
||||||
|
R queryList(String indexName, Map<String, Object> conditions);
|
||||||
|
|
||||||
|
void batchOperation(String indexName, Map<String,Object> dataJson);
|
||||||
|
|
||||||
|
void batchDeletion(String indexName, List<String> dataJson);
|
||||||
|
}
|
|
@ -1,15 +1,23 @@
|
||||||
package com.bawei.es.service;
|
package com.bawei.es.service;
|
||||||
|
|
||||||
import com.bawei.common.core.domain.R;
|
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: 31966
|
||||||
|
* @Project: mall-product
|
||||||
|
* @Package: com.bwie.es.service
|
||||||
|
* @ClassName: EsService
|
||||||
|
* @Description: 请在此处填写具体描述
|
||||||
|
* @Date: 2024/4/22 20:56
|
||||||
|
*/
|
||||||
public interface IndexService {
|
public interface IndexService {
|
||||||
Boolean exits(String indexName);
|
boolean exist(String indexName);
|
||||||
|
|
||||||
R create(String indexName, Map<String, Object> mappings);
|
boolean index(String indexName, Map<String, Object> mappings);
|
||||||
|
|
||||||
R update(String indexName, Map<String, Object> mappings);
|
boolean updataIndex(String indexName, Map<String, Object> mappings);
|
||||||
|
|
||||||
R delete(String indexName);
|
boolean deleteIndex(String indexName);
|
||||||
|
|
||||||
|
Map<String, Object> getMapping(String indexName);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,181 +0,0 @@
|
||||||
package com.bawei.es.service.impl;
|
|
||||||
|
|
||||||
import com.bawei.common.core.domain.R;
|
|
||||||
import com.bawei.common.core.utils.StringUtils;
|
|
||||||
import com.bawei.es.domain.DocumentClass;
|
|
||||||
import com.bawei.es.service.DocService;
|
|
||||||
import org.elasticsearch.action.DocWriteResponse;
|
|
||||||
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.client.RequestOptions;
|
|
||||||
import org.elasticsearch.client.RestHighLevelClient;
|
|
||||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
||||||
import org.elasticsearch.index.query.QueryBuilder;
|
|
||||||
import org.elasticsearch.index.query.QueryBuilders;
|
|
||||||
import org.elasticsearch.search.SearchHit;
|
|
||||||
import org.elasticsearch.search.SearchHits;
|
|
||||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
||||||
import org.elasticsearch.xcontent.XContentType;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.w3c.dom.ranges.RangeException;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class DocServiceImpl implements DocService {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private IndexServiceImpl indexServiceImpl;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private RestHighLevelClient restHighLevelClient;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*根据索引名称和文档ID查询该索引文档
|
|
||||||
* @param indexName 索引名称
|
|
||||||
* @param id 文档ID
|
|
||||||
* @return 查询结果
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public R getDocByIndexName(String indexName, String id) {
|
|
||||||
//判断索引是否存在
|
|
||||||
if(!indexServiceImpl.exits(indexName)){
|
|
||||||
throw new RuntimeException("该索引不存在");
|
|
||||||
}
|
|
||||||
|
|
||||||
//构造获取文档请求
|
|
||||||
GetRequest getRequest = new GetRequest(indexName, id);
|
|
||||||
|
|
||||||
try {
|
|
||||||
GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
|
|
||||||
|
|
||||||
//判断文档是否存在
|
|
||||||
if (getResponse.isExists()){
|
|
||||||
//获取文档内容
|
|
||||||
Map<String, Object> source = getResponse.getSource();
|
|
||||||
|
|
||||||
return R.ok(source);
|
|
||||||
}else {
|
|
||||||
throw new RuntimeException("该文档不存在");
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException("获取文档异常,异常信息为: "+e.getMessage(),e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 增加es文档
|
|
||||||
* @param indexName 索引名称
|
|
||||||
* @param document 文档内容
|
|
||||||
* @return 添加结果
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public R createDoc(String indexName, Map<String, Object> document) {
|
|
||||||
if(!indexServiceImpl.exits(indexName)){
|
|
||||||
throw new RuntimeException("该索引不存在");
|
|
||||||
}
|
|
||||||
|
|
||||||
//构建一个索引请求,并指定索引名称
|
|
||||||
IndexRequest indexRequest = new IndexRequest(indexName);
|
|
||||||
indexRequest.source(document, XContentType.JSON);
|
|
||||||
|
|
||||||
try {
|
|
||||||
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
|
|
||||||
if (indexResponse.getResult()== DocWriteResponse.Result.CREATED || indexResponse.getResult() == DocWriteResponse.Result.UPDATED){
|
|
||||||
return R.ok("文档添加成功");
|
|
||||||
}else {
|
|
||||||
throw new RuntimeException("文档添加失败");
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException("索引请求处理异常,异常信息为: "+e.getMessage(),e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 修改es文档
|
|
||||||
* @param indexName 索引名称
|
|
||||||
* @param document 文档内容
|
|
||||||
* @return 修改结果
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public R putDoc(String indexName, Map<String, Object> document) {
|
|
||||||
if(!indexServiceImpl.exits(indexName)){
|
|
||||||
throw new RuntimeException("该索引不存在");
|
|
||||||
}
|
|
||||||
|
|
||||||
R r = this.putDoc(indexName, document);
|
|
||||||
if (r.isSuccess()){
|
|
||||||
return R.ok("修改成功");
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new RuntimeException("修改失败");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除es文档
|
|
||||||
* @param indexName 索引名称
|
|
||||||
* @param id 文档ID
|
|
||||||
* @return 删除结果
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public R deleteDoc(String indexName, String id) {
|
|
||||||
if(!indexServiceImpl.exits(indexName)){
|
|
||||||
throw new RuntimeException("该索引不存在");
|
|
||||||
}
|
|
||||||
|
|
||||||
//构造删除文档请求
|
|
||||||
DeleteRequest deleteRequest = new DeleteRequest(indexName, id);
|
|
||||||
|
|
||||||
try {
|
|
||||||
DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
|
|
||||||
if (deleteResponse.getResult() == DocWriteResponse.Result.DELETED){
|
|
||||||
return R.ok("删除成功");
|
|
||||||
}else {
|
|
||||||
throw new RuntimeException("删除失败");
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException("索引请求处理异常,异常信息为: "+e.getMessage(),e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public HashMap<String, Object> search(DocumentClass documentClass) {
|
|
||||||
if (StringUtils.isEmpty(documentClass.getIndexName())){
|
|
||||||
throw new RuntimeException("索引名称不能为空");
|
|
||||||
}
|
|
||||||
if (StringUtils.isEmpty(documentClass.getSort().getSortName())){
|
|
||||||
throw new RuntimeException("索引字段不能为空");
|
|
||||||
}
|
|
||||||
if (StringUtils.isEmpty(documentClass.getSort().getSortOrder())){
|
|
||||||
throw new RuntimeException("排序字段不能为空");
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
SearchRequest searchRequest = new SearchRequest(documentClass.getIndexName());
|
|
||||||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
||||||
searchSourceBuilder.from((documentClass.getPage().getPageNum()-1)*documentClass.getPage().getPageSize());
|
|
||||||
searchSourceBuilder.size(documentClass.getPage().getPageSize());
|
|
||||||
searchSourceBuilder.sort(documentClass.getSort().getSortName());
|
|
||||||
searchRequest.source(searchSourceBuilder);
|
|
||||||
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
|
|
||||||
SearchHits hits = searchResponse.getHits();
|
|
||||||
SearchHit[] hits1 = hits.getHits();
|
|
||||||
Map<String, Object> map = new HashMap<>();
|
|
||||||
for (SearchHit documentFields : hits1) {
|
|
||||||
map = documentFields.getSourceAsMap();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("网络超时");
|
|
||||||
}
|
|
||||||
return new HashMap<>();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,320 @@
|
||||||
|
package com.bawei.es.service.impl;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
|
import com.bawei.common.core.domain.R;
|
||||||
|
import com.bawei.common.core.exception.ServiceException;
|
||||||
|
import com.bawei.common.core.utils.StringUtils;
|
||||||
|
import com.bawei.es.domain.CommonMap;
|
||||||
|
import com.bawei.es.service.DocumentService;
|
||||||
|
import org.elasticsearch.action.bulk.BulkRequest;
|
||||||
|
import org.elasticsearch.action.bulk.BulkResponse;
|
||||||
|
import org.elasticsearch.action.delete.DeleteRequest;
|
||||||
|
import org.elasticsearch.action.index.IndexRequest;
|
||||||
|
import org.elasticsearch.action.search.SearchRequest;
|
||||||
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
|
import org.elasticsearch.client.RequestOptions;
|
||||||
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
|
import org.elasticsearch.common.text.Text;
|
||||||
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||||
|
import org.elasticsearch.index.query.QueryBuilder;
|
||||||
|
import org.elasticsearch.index.query.QueryBuilders;
|
||||||
|
import org.elasticsearch.index.query.RangeQueryBuilder;
|
||||||
|
import org.elasticsearch.search.SearchHit;
|
||||||
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||||
|
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||||
|
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
|
||||||
|
import org.elasticsearch.search.sort.SortOrder;
|
||||||
|
import org.elasticsearch.xcontent.XContentType;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: 31966
|
||||||
|
* @Project: mall-product
|
||||||
|
* @Package: com.bwie.es.service.impl
|
||||||
|
* @ClassName: DocumentServiceImpl
|
||||||
|
* @Description: Elasticsearch 文档服务实现类
|
||||||
|
* @Date: 2024/4/22 20:58
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class DocumentServiceImpl implements DocumentService {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(DocumentServiceImpl.class);
|
||||||
|
|
||||||
|
@Resource(name = "restHighLevelClient")
|
||||||
|
private RestHighLevelClient restHighLevelClient;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public R queryList(String indexName, Map<String, Object> conditions) {
|
||||||
|
// 验证索引名
|
||||||
|
validateIndexName(indexName);
|
||||||
|
|
||||||
|
// 构建搜索请求
|
||||||
|
SearchRequest searchRequest = buildSearchRequest(indexName, conditions);
|
||||||
|
|
||||||
|
// 执行搜索
|
||||||
|
SearchResponse searchResponse;
|
||||||
|
try {
|
||||||
|
searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Failed to execute search request", e);
|
||||||
|
return R.fail("Query failed: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析搜索结果
|
||||||
|
CommonMap result = processSearchResponse(searchResponse);
|
||||||
|
|
||||||
|
return R.ok(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private SearchRequest buildSearchRequest(String indexName, Map<String, Object> conditions) {
|
||||||
|
SearchRequest searchRequest = new SearchRequest(indexName);
|
||||||
|
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
|
||||||
|
|
||||||
|
// 构造查询条件
|
||||||
|
QueryBuilder queryBuilder = buildQueryBuilder(conditions);
|
||||||
|
sourceBuilder.query(queryBuilder);
|
||||||
|
|
||||||
|
// 设置分页参数
|
||||||
|
setPaginationParameters(conditions, sourceBuilder);
|
||||||
|
|
||||||
|
// 设置排序参数
|
||||||
|
setSortParameters(conditions, sourceBuilder);
|
||||||
|
|
||||||
|
// 设置高亮参数
|
||||||
|
setHighlightParameters(conditions, sourceBuilder);
|
||||||
|
|
||||||
|
searchRequest.source(sourceBuilder);
|
||||||
|
|
||||||
|
// 记录搜索请求的详细信息(仅用于调试)
|
||||||
|
log.debug("ES search request DDL: {}", sourceBuilder.toString());
|
||||||
|
|
||||||
|
return searchRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void batchOperation(String indexName, Map<String, Object> dataJson) {
|
||||||
|
BulkRequest bulkRequest = new BulkRequest();
|
||||||
|
|
||||||
|
// 根据dataJson中的key,进行批量操作
|
||||||
|
for (Map.Entry<String, Object> entry : dataJson.entrySet()) {
|
||||||
|
String id = entry.getKey();
|
||||||
|
Map<String, Object> document = (Map<String, Object>) entry.getValue();
|
||||||
|
|
||||||
|
// 示例代码仅处理索引(index)和删除(delete)操作
|
||||||
|
IndexRequest indexRequest = new IndexRequest(indexName).id(id).source(document, XContentType.JSON);
|
||||||
|
bulkRequest.add(indexRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
|
||||||
|
|
||||||
|
// 检查批量操作结果,返回R对象
|
||||||
|
if (bulkResponse.hasFailures()) {
|
||||||
|
throw new ServiceException("Bulk operation failed: " + bulkResponse.buildFailureMessage());
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ServiceException("批量操作失败: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void batchDeletion(String indexName, List<String> ids) {
|
||||||
|
// 参数校验
|
||||||
|
validateIndexName(indexName);
|
||||||
|
if (CollectionUtils.isEmpty(ids)) {
|
||||||
|
throw new IllegalArgumentException("请输入需要删除的文档ID集合");
|
||||||
|
}
|
||||||
|
|
||||||
|
BulkRequest bulkRequest = new BulkRequest();
|
||||||
|
// 构建批量删除请求
|
||||||
|
for (String id : ids) {
|
||||||
|
bulkRequest.add(new DeleteRequest(indexName, id));
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 执行批量删除操作
|
||||||
|
BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
|
||||||
|
// 检查批量删除结果
|
||||||
|
if (bulkResponse.hasFailures()) {
|
||||||
|
throw new RuntimeException("批量删除失败: " + bulkResponse.buildFailureMessage());
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("执行批量删除操作时发生错误: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 校验索引名称是否为空
|
||||||
|
* @param indexName 索引名称
|
||||||
|
*/
|
||||||
|
private void validateIndexName(String indexName) {
|
||||||
|
if (StringUtils.isBlank(indexName)) {
|
||||||
|
throw new ServiceException("索引名为空,请重试!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setPaginationParameters(Map<String, Object> conditions, SearchSourceBuilder sourceBuilder) {
|
||||||
|
// 从条件中获取分页信息
|
||||||
|
Map<String, Object> pagination = (Map<String, Object>) conditions.get("pagination");
|
||||||
|
if (pagination == null) {
|
||||||
|
// 如果没有分页信息,则不设置分页参数
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 校验并获取分页参数
|
||||||
|
int page;
|
||||||
|
int limit;
|
||||||
|
try {
|
||||||
|
page = Integer.parseInt(pagination.getOrDefault("page", "1").toString());
|
||||||
|
limit = Integer.parseInt(pagination.getOrDefault("limit", "10").toString());
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
// 如果分页参数不是整数,则抛出异常
|
||||||
|
throw new IllegalArgumentException("Invalid pagination parameters: page and limit must be integers", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置分页参数
|
||||||
|
int from = (page - 1) * limit;
|
||||||
|
sourceBuilder.from(from).size(limit);
|
||||||
|
|
||||||
|
// 记录日志(可选,根据实际需求决定是否记录)
|
||||||
|
log.debug("Pagination parameters set: page={}, limit={}, from={}", page, limit, from);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setSortParameters(Map<String, Object> conditions, SearchSourceBuilder sourceBuilder) {
|
||||||
|
Map sort = (Map) conditions.get("sort");
|
||||||
|
if (sort != null) {
|
||||||
|
Object sortOrderObject = sort.get("sortOrder");
|
||||||
|
Object fieldObject = sort.get("field");
|
||||||
|
|
||||||
|
if (sortOrderObject instanceof String && fieldObject instanceof String) {
|
||||||
|
String sortOrder = (String) sortOrderObject;
|
||||||
|
String field = (String) fieldObject;
|
||||||
|
|
||||||
|
if ("asc".equalsIgnoreCase(sortOrder) || "desc".equalsIgnoreCase(sortOrder)) {
|
||||||
|
SortOrder sortOrderEnum = "asc".equalsIgnoreCase(sortOrder) ? SortOrder.ASC : SortOrder.DESC;
|
||||||
|
log.info("Applying sort: field={}, sortOrder={}", field, sortOrder);
|
||||||
|
sourceBuilder.sort(field, sortOrderEnum);
|
||||||
|
} else {
|
||||||
|
SortOrder sortOrderEnum = SortOrder.ASC;
|
||||||
|
log.info("Applying sort: field={}, sortOrder={}", field, sortOrder);
|
||||||
|
sourceBuilder.sort(field, sortOrderEnum);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Invalid sort parameters. Both 'sortOrder' and 'field' must be strings.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void setHighlightParameters(Map<String, Object> conditions, SearchSourceBuilder sourceBuilder) {
|
||||||
|
Map<String, Object> highlightParams = (Map<String, Object>) conditions.get("highlight");
|
||||||
|
if (highlightParams != null) {
|
||||||
|
HighlightBuilder highlightBuilder = new HighlightBuilder();
|
||||||
|
List<String> fieldNames = (List<String>) highlightParams.getOrDefault("fields", Collections.emptyList());
|
||||||
|
String preTag = (String) highlightParams.getOrDefault("preTag", "<em>");
|
||||||
|
String postTag = (String) highlightParams.getOrDefault("postTag", "</em>");
|
||||||
|
|
||||||
|
for (String fieldName : fieldNames) {
|
||||||
|
highlightBuilder.field(fieldName);
|
||||||
|
}
|
||||||
|
|
||||||
|
highlightBuilder.preTags(preTag);
|
||||||
|
highlightBuilder.postTags(postTag);
|
||||||
|
|
||||||
|
sourceBuilder.highlighter(highlightBuilder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private CommonMap processSearchResponse(SearchResponse searchResponse) {
|
||||||
|
CommonMap result = new CommonMap();
|
||||||
|
long totalHits = searchResponse.getHits().getTotalHits().value;
|
||||||
|
result.setTotal(totalHits);
|
||||||
|
|
||||||
|
LinkedHashMap<String, Object> objectMap = new LinkedHashMap<>();
|
||||||
|
for (SearchHit hit : searchResponse.getHits().getHits()) {
|
||||||
|
String sourceAsString = hit.getSourceAsString();
|
||||||
|
HashMap<String, Object> parsedObject = JSONObject.parseObject(sourceAsString, HashMap.class);
|
||||||
|
String id = hit.getId();
|
||||||
|
// 添加高亮信息到解析后的文档
|
||||||
|
addHighlightedFields(hit.getHighlightFields(), parsedObject);
|
||||||
|
|
||||||
|
objectMap.put(id, parsedObject);
|
||||||
|
log.info("查询出来的数据为:{}",JSONObject.toJSONString(parsedObject));
|
||||||
|
}
|
||||||
|
log.info("查询出来的数据为:{}",JSONObject.toJSONString(objectMap));
|
||||||
|
result.setObjectMap(objectMap);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addHighlightedFields(Map<String, HighlightField> highlightFields, Map<String, Object> parsedObject) {
|
||||||
|
if (!highlightFields.isEmpty()) {
|
||||||
|
for (Map.Entry<String, HighlightField> entry : highlightFields.entrySet()) {
|
||||||
|
String fieldName = entry.getKey();
|
||||||
|
Text[] fragments = entry.getValue().fragments();
|
||||||
|
StringBuilder highlightedValue = new StringBuilder();
|
||||||
|
for (Text fragment : fragments) {
|
||||||
|
highlightedValue.append(fragment);
|
||||||
|
}
|
||||||
|
parsedObject.put(fieldName, highlightedValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据条件构建Elasticsearch查询构建器
|
||||||
|
*
|
||||||
|
* @param conditions 查询条件(键值对形式)
|
||||||
|
* @return Elasticsearch查询构建器
|
||||||
|
*/
|
||||||
|
private QueryBuilder buildQueryBuilder(Map<String, Object> conditions) {
|
||||||
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||||
|
|
||||||
|
if (conditions != null && !conditions.isEmpty()) {
|
||||||
|
for (Map.Entry<String, Object> entry : conditions.entrySet()) {
|
||||||
|
String field = entry.getKey();
|
||||||
|
Object value = entry.getValue();
|
||||||
|
|
||||||
|
// 处理范围查询条件
|
||||||
|
if (value instanceof Map && ((Map<?, ?>) value).containsKey("range")) {
|
||||||
|
Map<String, Object> rangeParams = (Map<String, Object>) value;
|
||||||
|
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(field);
|
||||||
|
|
||||||
|
String gt = (String) rangeParams.get("gt");
|
||||||
|
if (gt != null) {
|
||||||
|
rangeQueryBuilder.gt(gt);
|
||||||
|
}
|
||||||
|
|
||||||
|
String gte = (String) rangeParams.get("gte");
|
||||||
|
if (gte != null) {
|
||||||
|
rangeQueryBuilder.gte(gte);
|
||||||
|
}
|
||||||
|
|
||||||
|
String lt = (String) rangeParams.get("lt");
|
||||||
|
if (lt != null) {
|
||||||
|
rangeQueryBuilder.lt(lt);
|
||||||
|
}
|
||||||
|
|
||||||
|
String lte = (String) rangeParams.get("lte");
|
||||||
|
if (lte != null) {
|
||||||
|
rangeQueryBuilder.lte(lte);
|
||||||
|
}
|
||||||
|
|
||||||
|
boolQueryBuilder.filter(rangeQueryBuilder);
|
||||||
|
}
|
||||||
|
// 处理其他查询条件(如 match 查询)
|
||||||
|
else if (value instanceof String) {
|
||||||
|
boolQueryBuilder.filter(QueryBuilders.matchQuery(field, value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return boolQueryBuilder;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,127 +1,194 @@
|
||||||
package com.bawei.es.service.impl;
|
package com.bawei.es.service.impl;
|
||||||
|
|
||||||
import com.bawei.common.core.domain.R;
|
import com.bawei.common.core.exception.ServiceException;
|
||||||
import com.bawei.es.service.IndexService;
|
import com.bawei.es.service.IndexService;
|
||||||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
||||||
import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
|
||||||
import org.elasticsearch.client.RequestOptions;
|
import org.elasticsearch.client.RequestOptions;
|
||||||
import org.elasticsearch.client.RestHighLevelClient;
|
import org.elasticsearch.client.RestHighLevelClient;
|
||||||
import org.elasticsearch.client.indices.CreateIndexRequest;
|
import org.elasticsearch.client.indices.CreateIndexRequest;
|
||||||
import org.elasticsearch.client.indices.CreateIndexResponse;
|
import org.elasticsearch.client.indices.CreateIndexResponse;
|
||||||
import org.elasticsearch.client.indices.GetIndexRequest;
|
import org.elasticsearch.client.indices.GetIndexRequest;
|
||||||
|
import org.elasticsearch.client.indices.GetIndexResponse;
|
||||||
|
import org.elasticsearch.cluster.metadata.MappingMetadata;
|
||||||
|
import org.elasticsearch.common.settings.Settings;
|
||||||
import org.elasticsearch.xcontent.XContentBuilder;
|
import org.elasticsearch.xcontent.XContentBuilder;
|
||||||
import org.elasticsearch.xcontent.XContentFactory;
|
import org.elasticsearch.xcontent.XContentFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author: 31966
|
||||||
|
* @Project: mall-product
|
||||||
|
* @Package: com.bwie.es.service.impl
|
||||||
|
* @ClassName: EsServiceImpl
|
||||||
|
* @Description: 请在此处填写具体描述
|
||||||
|
* @Date: 2024/4/22 20:56
|
||||||
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class IndexServiceImpl implements IndexService {
|
public class IndexServiceImpl implements IndexService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RestHighLevelClient restHighLevelClient;
|
private RestHighLevelClient restHighLevelClient;
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据索引名称查询该索引是否存在
|
|
||||||
* @param indexName 索引名称
|
|
||||||
* @return 是/否
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean exits(String indexName) {
|
public boolean exist(String indexName) {
|
||||||
GetIndexRequest indexRequest = new GetIndexRequest(indexName);
|
if (indexName == null || indexName.isEmpty()){
|
||||||
|
throw new ServiceException("索引名称不能为空");
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
boolean exists = restHighLevelClient.indices().exists(indexRequest, RequestOptions.DEFAULT);
|
GetIndexRequest request = new GetIndexRequest(indexName);
|
||||||
if (exists){
|
GetIndexResponse response = restHighLevelClient.indices().get(request, RequestOptions.DEFAULT);
|
||||||
return true;
|
// 检查响应中是否包含指定的索引
|
||||||
}else {
|
for (String index : response.getIndices()) {
|
||||||
throw new RuntimeException("该索引不存在");
|
if (index.equals(indexName)){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("查找索引异常,异常信息为: "+e.getMessage(),e);
|
// 处理异常,例如记录日志或返回false表示索引不存在
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean index(String indexName, Map<String, Object> mappings) {
|
||||||
|
// 参数校验
|
||||||
|
if (indexName == null || indexName.isEmpty()) {
|
||||||
|
throw new ServiceException("Index name cannot be null or empty");
|
||||||
|
}
|
||||||
|
// 判断索引是否存在
|
||||||
|
boolean indexExists = exist(indexName);
|
||||||
|
if (indexExists) {
|
||||||
|
throw new ServiceException("索引 " + indexName + " 已存在");
|
||||||
|
}
|
||||||
|
// 创建索引并设置映射(根据mappings非空情况)
|
||||||
|
try {
|
||||||
|
Settings.Builder settingsBuilder = Settings.builder();
|
||||||
|
XContentBuilder mappingBuilder = null;
|
||||||
|
|
||||||
|
if (mappings != null && !mappings.isEmpty()) {
|
||||||
|
// 设置映射
|
||||||
|
mappingBuilder = createMapping(mappings);
|
||||||
|
}
|
||||||
|
|
||||||
|
CreateIndexRequest request = new CreateIndexRequest(indexName)
|
||||||
|
.settings(settingsBuilder);
|
||||||
|
if (mappingBuilder!=null){
|
||||||
|
request.mapping(mappingBuilder);
|
||||||
|
}
|
||||||
|
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
|
||||||
|
return response.isAcknowledged();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("创建索引时发生错误: " + e.getMessage());
|
||||||
|
throw new ServiceException("创建索引时发生错误");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 创建索引
|
|
||||||
* @param indexName 索引名称
|
|
||||||
* @param mappings 参数对象
|
|
||||||
* @return 是/否
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public R create(String indexName, Map<String, Object> mappings) {
|
public boolean updataIndex(String indexName, Map<String, Object> mappings) {
|
||||||
Boolean exits = this.exits(indexName);
|
|
||||||
if (exits){
|
|
||||||
throw new RuntimeException("该索引已存在");
|
|
||||||
}
|
|
||||||
CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
|
|
||||||
|
|
||||||
// 将映射参数转换为 JSON 字符串并设置到索引请求中
|
|
||||||
try {
|
try {
|
||||||
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
|
if (indexName!=null && !indexName.isEmpty()){
|
||||||
xContentBuilder.map(mappings);
|
if (Boolean.FALSE.equals(exist(indexName))) {
|
||||||
createIndexRequest.mapping(xContentBuilder);
|
index(indexName,mappings);
|
||||||
} catch (Exception e) {
|
return true;
|
||||||
throw new RuntimeException("构建映射失败,失败原因: "+e.getMessage(),e);
|
}else {
|
||||||
}
|
deleteIndex(indexName);
|
||||||
|
index(indexName,mappings);
|
||||||
try {
|
return true;
|
||||||
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
|
}
|
||||||
if (createIndexResponse.isAcknowledged()){
|
|
||||||
return R.ok("创建成功");
|
|
||||||
}else {
|
}else {
|
||||||
throw new RuntimeException("创建索引失败");
|
throw new ServiceException("索引名字为空,请重试!!!");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("创建索引异常,异常信息为:"+e.getMessage(),e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 更新索引
|
|
||||||
* @param indexName 索引名称
|
|
||||||
* @param mappings 参数对象
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public R update(String indexName, Map<String, Object> mappings) {
|
public boolean deleteIndex(String indexName) {
|
||||||
Boolean exits = this.exits(indexName);
|
// 删除索引
|
||||||
if (!exits){
|
try {
|
||||||
throw new RuntimeException("该索引不存在");
|
restHighLevelClient.indices().delete(new DeleteIndexRequest(indexName), RequestOptions.DEFAULT);
|
||||||
}
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
R delete = delete(indexName);
|
System.err.println("Error deleting index: " + e.getMessage());
|
||||||
if (delete.isSuccess()){
|
throw new ServiceException("Error deleting index");
|
||||||
create(indexName,mappings);
|
|
||||||
return R.ok("更新成功");
|
|
||||||
}else {
|
|
||||||
throw new RuntimeException("更新失败");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除索引
|
|
||||||
* @param indexName 索引名称
|
|
||||||
* @return 是/否
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public R delete(String indexName) {
|
public Map<String, Object> getMapping(String indexName) {
|
||||||
Boolean exits = this.exits(indexName);
|
if (indexName == null || indexName.isEmpty()) {
|
||||||
if (!exits){
|
throw new ServiceException("索引名称不能为空");
|
||||||
throw new RuntimeException("该索引不存在");
|
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
|
GetIndexRequest request = new GetIndexRequest(indexName);
|
||||||
AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
|
GetIndexResponse response = restHighLevelClient.indices().get(request, RequestOptions.DEFAULT);
|
||||||
if (delete.isAcknowledged()){
|
|
||||||
return R.ok("删除成功");
|
// 检查响应中是否包含指定的索引
|
||||||
}else {
|
for (String index : response.getIndices()) {
|
||||||
throw new RuntimeException("删除失败");
|
if (index.equals(indexName)) {
|
||||||
|
Map<String, MappingMetadata> mappings = response.getMappings();
|
||||||
|
|
||||||
|
// 提取并返回指定索引的映射结构
|
||||||
|
MappingMetadata mappingMetadata = mappings.get(indexName);
|
||||||
|
if (mappingMetadata != null) {
|
||||||
|
Map<String,Object> map = (Map<String, Object>) mappingMetadata.getSourceAsMap().get("properties");
|
||||||
|
if (map==null){
|
||||||
|
throw new ServiceException("索引映射为空");
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("删除索引异常,异常信息为: "+e.getMessage(),e);
|
// 处理异常,例如记录日志
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 索引不存在或获取映射失败,返回null
|
||||||
|
throw new ServiceException("索引不存在,请重试");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据给定的映射创建XContentBuilder
|
||||||
|
*/
|
||||||
|
private XContentBuilder createMapping(Map<String, Object> mappings) throws IOException {
|
||||||
|
XContentBuilder builder = XContentFactory.jsonBuilder()
|
||||||
|
.startObject()
|
||||||
|
.startObject("properties");
|
||||||
|
|
||||||
|
for (Map.Entry<String, Object> entry : mappings.entrySet()) {
|
||||||
|
String fieldName = entry.getKey();
|
||||||
|
Object fieldConfig = entry.getValue();
|
||||||
|
|
||||||
|
builder.startObject(fieldName); // Start field mapping
|
||||||
|
|
||||||
|
if (fieldConfig instanceof Map) {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Map<String, Object> nestedMapping = (Map<String, Object>) fieldConfig;
|
||||||
|
|
||||||
|
for (Map.Entry<String, Object> nestedEntry : nestedMapping.entrySet()) {
|
||||||
|
String nestedFieldName = nestedEntry.getKey();
|
||||||
|
Object nestedFieldConfig = nestedEntry.getValue();
|
||||||
|
|
||||||
|
builder.field(nestedFieldName, nestedFieldConfig);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
builder.field("type", fieldConfig.toString()); // Add field type
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.endObject(); // End field mapping
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.endObject() // End "properties"
|
||||||
|
.endObject(); // End root object
|
||||||
|
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# Tomcat
|
# Tomcat
|
||||||
server:
|
server:
|
||||||
port: 9003
|
port: 9204
|
||||||
|
|
||||||
# Spring
|
# Spring
|
||||||
spring:
|
spring:
|
||||||
|
@ -19,9 +19,9 @@ spring:
|
||||||
config:
|
config:
|
||||||
# 配置中心地址
|
# 配置中心地址
|
||||||
server-addr: 127.0.0.1:8848
|
server-addr: 127.0.0.1:8848
|
||||||
namespace: 12345678
|
|
||||||
# 配置文件格式
|
# 配置文件格式
|
||||||
file-extension: yml
|
file-extension: yml
|
||||||
# 共享配置
|
# 共享配置
|
||||||
shared-configs:
|
shared-configs:
|
||||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||||
|
namespace: 12345678
|
||||||
|
|
|
@ -22,7 +22,8 @@
|
||||||
<!-- 文件服务公共依赖 -->
|
<!-- 文件服务公共依赖 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.bawei</groupId>
|
<groupId>com.bawei</groupId>
|
||||||
<artifactId>base-file-common</artifactId>
|
<artifactId>base-es-common</artifactId>
|
||||||
|
<version>3.6.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
package com.bawei.file.remote.api;
|
package com.bawei.file.remote.api;
|
||||||
|
|
||||||
import com.bawei.file.domain.SysFile;
|
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
@ -24,6 +22,6 @@ public interface RemoteFileService
|
||||||
* @param file 文件信息
|
* @param file 文件信息
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
@PostMapping(value = "/upload")
|
||||||
public R<SysFile> upload(@RequestPart(value = "file") MultipartFile file);
|
public R upload();
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,9 @@ public class ServiceNameConstants
|
||||||
*/
|
*/
|
||||||
public static final String FILE_SERVICE = "bawei-file";
|
public static final String FILE_SERVICE = "bawei-file";
|
||||||
|
|
||||||
|
|
||||||
|
public static final String ES_SERVICE = "bawei-es";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 商品服务的serviceid
|
* 商品服务的serviceid
|
||||||
*/
|
*/
|
||||||
|
|
6
pom.xml
6
pom.xml
|
@ -304,6 +304,12 @@
|
||||||
<version>3.6.0</version>
|
<version>3.6.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.bawei</groupId>
|
||||||
|
<artifactId>base-es-common</artifactId>
|
||||||
|
<version>3.6.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!--解决集成knife4j时冲突问题-->
|
<!--解决集成knife4j时冲突问题-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.guava</groupId>
|
<groupId>com.google.guava</groupId>
|
||||||
|
|
Loading…
Reference in New Issue