master
chenbingxuan 2024-02-17 19:25:22 +08:00
parent f6cf5fc685
commit 5a91556e68
40 changed files with 1371 additions and 26 deletions

View File

@ -21,4 +21,6 @@ public class ServiceNameConstants
* serviceid * serviceid
*/ */
public static final String FILE_SERVICE = "HomeWork-file"; public static final String FILE_SERVICE = "HomeWork-file";
public static final String ES_SERVICE = "bawei-es";
} }

View File

@ -17,7 +17,7 @@ public @interface EnableRyFeignClients
{ {
String[] value() default {}; String[] value() default {};
String[] basePackages() default { "HomeWork" }; String[] basePackages() default { "HomeWork" , "com.bawei" };
Class<?>[] basePackageClasses() default {}; Class<?>[] basePackageClasses() default {};

View File

@ -120,5 +120,6 @@ public class TreeServiceImpl implements TreeService {
treeVos.add(treeVo1); treeVos.add(treeVo1);
} }
return treeVos; return treeVos;
} }/**/
} }

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>HomeWork</groupId>
<artifactId>base-es</artifactId>
<version>3.6.3</version>
</parent>
<artifactId>base-es-common</artifactId>
<dependencies>
<!-- 项目公共核心依赖 -->
<!-- RuoYi Common Core-->
<dependency>
<groupId>HomeWork</groupId>
<artifactId>HomeWork-common-core</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,14 @@
package com.bawei.es.domain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel("分页")
@Data
public class BasePage {
@ApiModelProperty(value = "页码",required = true)
private Integer pageNum;
@ApiModelProperty(value = "每页数量",required = true)
private Integer pageSize;
}

View File

@ -0,0 +1,4 @@
package com.bawei.es.domain;
public class DocInfoReq {
}

View File

@ -0,0 +1,16 @@
package com.bawei.es.domain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel("文档")
public class Document {
@ApiModelProperty(value = "id",required = false)
private String id;
@ApiModelProperty(value = "姓名",required = false)
private String name;
@ApiModelProperty(value = "年龄",required = false)
private Integer age;
}

View File

@ -0,0 +1,19 @@
package com.bawei.es.domain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.util.List;
@ApiModel("文档删除")
@Data
public class DocumentDelReq {
@ApiModelProperty(value = "索引名称",required = true)
@NotNull(message = "索引名称不能为空")
private String indexName;
@ApiModelProperty(value = "文档id", required = true)
@NotNull(message = "id不能为空")
private List<String> ids;
}

View File

@ -0,0 +1,26 @@
package com.bawei.es.domain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.util.List;
@Data
@ApiModel("文档查询")
public class DocumentListReq {
@ApiModelProperty(value = "索引名称",required = true)
@NotNull(message = "索引名称不能为空")
private String indexName;
@ApiModelProperty(value = "全文检索", required = false)
private String keyWord;
@ApiModelProperty(value = "分页参数", required = false)
private BasePage basePage;
@ApiModelProperty("检索字段")
private List<String> queryFields;
@ApiModelProperty("返回字段")
private List<String> returnFields;
}

View File

@ -0,0 +1,26 @@
package com.bawei.es.domain;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
@Data
@Api(tags = "文档索引")
public class DocumentReq {
@ApiModelProperty(value = "索引名称",required = true)
@NotNull(message = "索引名称不能为空")
private String indexName;
@ApiModelProperty(value = "是否制定文档ID")
private boolean isNeedId = false;
@ApiModelProperty(value = "文档ID对应字段")
private String idField;
@ApiModelProperty(value = "文档内容")
private List<Map<String,Object>> documentContent;
}

View File

@ -0,0 +1,21 @@
package com.bawei.es.domain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
@ApiModel("文档删除")
@Data
public class DocumentUpdReq {
@ApiModelProperty(value = "索引名称",required = true)
@NotNull(message = "索引名称不能为空")
private String indexName;
@ApiModelProperty(value = "文档内容", required = true)
@NotNull(message = "id不能为空")
private String id;
@ApiModelProperty(value = "文档内容", required = true)
@NotNull(message = "文档内容不能为空")
private Document document;
}

View File

@ -0,0 +1,20 @@
package com.bawei.es.domain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.util.Map;
@Data
@ApiModel("索引信息")
public class IndexEntry {
@ApiModelProperty(value = "索引名称",required = true)
@NotNull(message = "索引名称不能为空")
private String indexName;
@ApiModelProperty(value = "索引mapping",required = false)
private Map<String,Object> indexMapping;
@ApiModelProperty(value = "索引參數",required = false)
private Map<String,Object> settings;
}

View File

@ -0,0 +1,20 @@
package com.bawei.es.domain;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.util.Map;
@Data
@ApiModel("索引信息")
public class IndexInfoReq {
@ApiModelProperty(value = "索引名称",required = true)
@NotNull(message = "索引名称不能为空")
private String indexName;
@ApiModelProperty(value = "索引mapping",required = false)
private Map<String,Object> indexMapping;
@ApiModelProperty(value = "索引參數",required = false)
private Map<String,Object> settings;
}

View File

@ -0,0 +1,15 @@
package com.bawei.es.domain;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import java.util.Map;
@Data
@ApiModel("索引对象")
public class IndexInfoResp {
private String indexName;
private Map<String,Object> aliases;
private Map<String,Object> mappings;
private Map<String,Object> settings;
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>HomeWork</groupId>
<artifactId>base-es</artifactId>
<version>3.6.3</version>
</parent>
<artifactId>base-es-remote</artifactId>
<dependencies>
<dependency>
<groupId>HomeWork</groupId>
<artifactId>base-es-common</artifactId>
<version>3.6.3</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,32 @@
package com.bawei.es.remote.api;
import HomeWork.common.core.constant.ServiceNameConstants;
import HomeWork.common.core.domain.R;
import com.bawei.es.domain.*;
import com.bawei.es.remote.factory.RemoteEsDocumentFallbackFactory;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
/**
*
*
* @author bawei
*
*/
@FeignClient(contextId = "RemoteEsDocService", value = ServiceNameConstants.ES_SERVICE, fallbackFactory = RemoteEsDocumentFallbackFactory.class)
public interface RemoteEsDocService
{
@ApiOperation("文档插入")
@PostMapping("/es/document/add")
public R add(@RequestBody DocumentReq documentReq);
@ApiOperation("文档删除")
@PostMapping("/es/document/del")
public R del(@RequestBody DocumentDelReq documentDelReq);
@ApiOperation("文档查询")
@PostMapping("/list")
public R list(@RequestBody DocumentListReq documentListReq);
}

View File

@ -0,0 +1,44 @@
package com.bawei.es.remote.factory;
import HomeWork.common.core.domain.R;
import com.bawei.es.domain.DocumentDelReq;
import com.bawei.es.domain.DocumentListReq;
import com.bawei.es.domain.DocumentReq;
import com.bawei.es.remote.api.RemoteEsDocService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
/**
*
*
* @author bawei
*/
@Component
public class RemoteEsDocumentFallbackFactory implements FallbackFactory<RemoteEsDocService>
{
private static final Logger log = LoggerFactory.getLogger(RemoteEsDocumentFallbackFactory.class);
@Override
public RemoteEsDocService create(Throwable cause) {
log.error("日志服务调用失败:{}", cause.getMessage());
return new RemoteEsDocService(){
@Override
public R add(DocumentReq documentReq) {
return R.fail("调用es服务插入数据失败请稍后重试");
}
@Override
public R del(DocumentDelReq documentDelReq) {
return R.fail("调用es服务删除数据失败请稍后重试");
}
@Override
public R list(DocumentListReq documentListReq) {
return R.fail("调用es服务查询数据失败请稍后重试");
}
};
}
}

View File

@ -0,0 +1,136 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>HomeWork</groupId>
<artifactId>base-es</artifactId>
<version>3.6.3</version>
</parent>
<artifactId>base-es-server</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<!-- 人脸-->
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
<!-- 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>
<exclusions>
<exclusion>
<artifactId>spring-boot-autoconfigure</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- Swagger UI -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.fox.version}</version>
</dependency>
<!-- RuoYi Common DataScope -->
<dependency>
<groupId>HomeWork</groupId>
<artifactId>HomeWork-common-datascope</artifactId>
</dependency>
<!-- RuoYi Common Log -->
<dependency>
<groupId>HomeWork</groupId>
<artifactId>HomeWork-common-log</artifactId>
</dependency>
<!-- RuoYi Common Swagger -->
<dependency>
<groupId>HomeWork</groupId>
<artifactId>HomeWork-common-swagger</artifactId>
</dependency>
<dependency>
<groupId>HomeWork</groupId>
<artifactId>base-es-remote</artifactId>
<version>3.6.3</version>
<exclusions>
<exclusion>
<artifactId>mybatis-spring-boot-starter</artifactId>
<groupId>org.mybatis.spring.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,17 @@
package com.bawei.es;
import HomeWork.common.security.annotation.EnableCustomConfig;
import HomeWork.common.security.annotation.EnableRyFeignClients;
import HomeWork.common.swagger.annotation.EnableCustomSwagger2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableCustomConfig
@EnableCustomSwagger2
@EnableRyFeignClients
@SpringBootApplication
public class EsApp {
public static void main(String[] args) {
SpringApplication.run(EsApp.class);
}
}

View File

@ -0,0 +1,27 @@
package com.bawei.es.config;
import lombok.Data;
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.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
@Data
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
private String host;
private Integer port;
@Override
public RestHighLevelClient elasticsearchClient() {
if (host == null || port == null) {
throw new RuntimeException("elasticsearch配置错误");
}
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
return restHighLevelClient;
}
}

View File

@ -0,0 +1,63 @@
package com.bawei.es.controller;
import HomeWork.common.core.domain.R;
import com.bawei.es.domain.DocumentDelReq;
import com.bawei.es.domain.DocumentListReq;
import com.bawei.es.domain.DocumentReq;
import com.bawei.es.domain.DocumentUpdReq;
import com.bawei.es.server.EsDocumentServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api("es文档管理")
@AllArgsConstructor
@RestController
@RequestMapping("/es/document")
public class EsDocumentController {
private EsDocumentServer esDocumentServer;
/**
*
* @param documentReq
* @return
*/
@ApiOperation("文档插入")
@PostMapping("/add")
public R add(@RequestBody DocumentReq documentReq){
esDocumentServer.add(documentReq);
return R.ok();
}
/**
*
* @param documentDelReq
* @return
*/
@ApiOperation("文档删除")
@PostMapping("/del")
public R del(@RequestBody DocumentDelReq documentDelReq){
esDocumentServer.del(documentDelReq);
return R.ok();
}
@ApiOperation("文档修改")
@PostMapping("/upd")
public R upd(@RequestBody DocumentUpdReq documentReq){
esDocumentServer.upd(documentReq);
return R.ok();
}
@ApiOperation("文档查询")
@PostMapping("/list")
public R list(@RequestBody DocumentListReq documentListReq){
return R.ok(esDocumentServer.list(documentListReq));
}
}

View File

@ -0,0 +1,55 @@
package com.bawei.es.controller;
import HomeWork.common.core.domain.R;
import com.bawei.es.domain.IndexEntry;
import com.bawei.es.domain.IndexInfoReq;
import com.bawei.es.domain.IndexInfoResp;
import com.bawei.es.server.EsIndexService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/es/index")
@Api(tags = "es索引管理")
public class EsIndexController {
@Autowired
private EsIndexService esIndexService;
@RequestMapping("/create")
@ApiOperation("创建索引")
public R createIndex(@RequestBody IndexInfoReq indexInfoReq) {
esIndexService.createIndex(indexInfoReq);
return R.ok();
}
@PostMapping("/del/{indexName}")
@ApiOperation("删除索引")
public R delIndex(@PathVariable String indexName){
esIndexService.delIndex(indexName);
return R.ok();
}
@PostMapping("/get/{indexName}")
@ApiOperation("判断索引是否存在")
public R isExistIndex(@PathVariable String indexName){
boolean isExist = esIndexService.isExistIndex(indexName);
return R.ok(isExist);
}
@PostMapping("/find/{indexName}")
@ApiOperation("查找索引信息")
public R<IndexInfoResp> find(@PathVariable String indexName){
IndexInfoResp indexInfoResp=esIndexService.find(indexName);
return R.ok(indexInfoResp);
}
@PostMapping("/upd")
@ApiOperation("修改")
public R updIndex(@RequestBody IndexEntry indexEntry){
esIndexService.updIndex(indexEntry);
return R.ok();
}
}

View File

@ -0,0 +1,18 @@
package com.bawei.es.server;
import com.bawei.es.domain.*;
import java.util.ArrayList;
import java.util.Map;
public interface EsDocumentServer {
void add(DocumentReq documentReq);
void del(DocumentDelReq documentDelReq);
void upd(DocumentUpdReq documentReq);
ArrayList<Map<String,Object>> list(DocumentListReq documentListReq);
}

View File

@ -0,0 +1,19 @@
package com.bawei.es.server;
import com.bawei.es.domain.DocumentReq;
import com.bawei.es.domain.IndexEntry;
import com.bawei.es.domain.IndexInfoReq;
import com.bawei.es.domain.IndexInfoResp;
public interface EsIndexService {
void createIndex(IndexInfoReq indexInfoReq);
void delIndex(String indexName);
boolean isExistIndex(String indexName);
IndexInfoResp find(String indexName);
void updIndex(IndexEntry indexEntry);
}

View File

@ -0,0 +1,186 @@
package com.bawei.es.server.impl;
import com.bawei.es.domain.*;
import com.bawei.es.server.EsDocumentServer;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
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.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
@AllArgsConstructor
@Slf4j
public class EsDocumentServerImpl implements EsDocumentServer {
private RestHighLevelClient restHighLevelClient;
@Override
public void add(DocumentReq documentReq) {
//打印请求参数
log.info("参数为{}",documentReq);
//效验索引是否存在
if (findIndexName(documentReq.getIndexName())){
//批量添加
BulkRequest bulkRequest = new BulkRequest();
List<Map<String, Object>> data = documentReq.getDocumentContent();
//判断数据是否为空
if (data==null||data.size()==0){
throw new RuntimeException("数据为空");
}
//循环集合
for (Map<String, Object> datum : data) {
//创建索引请求indexRequest
IndexRequest indexRequest = new IndexRequest(documentReq.getIndexName());
//遍历数据
log.info("数据{}",datum);
if (documentReq.getIdField()!=null&&datum.containsKey(documentReq.getIdField())){
//获取data中的map取id
String uuid = datum.get(documentReq.getIdField()).toString();
//判断isNeedId是否==true和uuid非空验证
if (documentReq.isNeedId()==true&&uuid!=null){
//设置id
indexRequest.id(uuid);
}
}
//设置内容
indexRequest.source(datum);
bulkRequest.add(indexRequest);
}
try {
restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
} catch (Exception e) {
log.info("es插入失败异常信息{}",e);
throw new RuntimeException("es插入失败");
}
}else {
throw new RuntimeException("索引名不存在");
}
}
@Override
public void del(DocumentDelReq documentDelReq) {
if (findIndexName(documentDelReq.getIndexName())){
BulkRequest bulkRequest = new BulkRequest();
try {
for (String id : documentDelReq.getIds()) {
DeleteRequest deleteRequest = new DeleteRequest(documentDelReq.getIndexName(), id);
bulkRequest.add(deleteRequest);
}
restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
} catch (Exception e) {
throw new RuntimeException(e);
}
}else {
throw new RuntimeException("索引名不存在");
}
}
@Override
public void upd(DocumentUpdReq documentReq) {
log.info("打印请求{}",documentReq);
if (findIndexName(documentReq.getIndexName())){
String name = documentReq.getDocument().getName();
Integer age = documentReq.getDocument().getAge();
try {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.id(documentReq.getId());
updateRequest.index(documentReq.getIndexName());
updateRequest.doc(
XContentFactory.jsonBuilder().startObject()
.field("name",name)
.field("age",age)
.endObject()
);
restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);
} catch (Exception e) {
throw new RuntimeException(e);
}
}else {
throw new RuntimeException("索引名不存在");
}
}
@Override
public ArrayList<Map<String, Object>> list(DocumentListReq documentListReq) {
//返回结果
ArrayList<Map<String, Object>> maps = new ArrayList<>();
//输出请求参数
log.info("打印请求参数{}",documentListReq);
//判断索引是否存在
if (findIndexName(documentListReq.getIndexName())){
// 搜索源构建对象
SearchRequest searchRequest = new SearchRequest(documentListReq.getIndexName());
// 搜索源构建对象
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 搜索方式
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//全文检索
if(documentListReq.getKeyWord()!=null&&!documentListReq.getKeyWord().equals("")){
boolQuery.must(QueryBuilders.matchQuery(documentListReq.getQueryFields().get(0),documentListReq.getKeyWord()));
}
searchSourceBuilder.query(boolQuery);
//判断分页参数是否为空
if(documentListReq.getBasePage().getPageNum()!=null&&documentListReq.getBasePage().getPageSize()!=null){
searchSourceBuilder.from((documentListReq.getBasePage().getPageNum()-1)*documentListReq.getBasePage().getPageSize());
searchSourceBuilder.size(documentListReq.getBasePage().getPageSize());
}
//检索字段
if (documentListReq.getQueryFields()!=null&&!documentListReq.getQueryFields().equals("")){
String[] array = documentListReq.getReturnFields().toArray(new String[0]);
searchSourceBuilder.fetchSource(array,null);
}
try {
//向es发送请求
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
//循环查询结果
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
//转型
Map<String, Object> sourceAsMap = documentFields.getSourceAsMap();
log.info("遍历结果{}",sourceAsMap);
maps.add(sourceAsMap);
}
return maps;
} catch (Exception e) {
throw new RuntimeException(e);
}
}else {
throw new RuntimeException("索引名不存在");
}
}
public boolean findIndexName(String indexName){
if (indexName!=null){
try {
GetIndexRequest getIndexRequest = new GetIndexRequest(indexName);
return restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
} catch (Exception e) {
log.info("查询异常信息{}",e);
throw new RuntimeException("索引查询失败");
}
}else {
throw new RuntimeException("索引名为空");
}
}
}

View File

@ -0,0 +1,136 @@
package com.bawei.es.server.impl;
import com.alibaba.fastjson.JSONObject;
import com.bawei.es.domain.DocumentReq;
import com.bawei.es.domain.IndexEntry;
import com.bawei.es.domain.IndexInfoReq;
import com.bawei.es.domain.IndexInfoResp;
import com.bawei.es.server.EsIndexService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xcontent.XContentType;
import org.springframework.stereotype.Service;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Service
@Slf4j
@AllArgsConstructor
public class EsIndexServiceImpl implements EsIndexService {
private RestHighLevelClient restHighLevelClient;
/**
*
* @param indexInfoReq
*/
@Override
public void createIndex(IndexInfoReq indexInfoReq) {
//打印日志
log.info("创建索引{}", JSONObject.toJSONString(indexInfoReq));
//判断名字是否存在
boolean existIndex = isExistIndex(indexInfoReq.getIndexName());
if (existIndex){
throw new RuntimeException("索引:"+indexInfoReq.getIndexName()+"索引已存在");
}
//创建索引
CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexInfoReq.getIndexName());
if(indexInfoReq.getIndexMapping()!=null){
String jsonString = JSONObject.toJSONString(indexInfoReq.getIndexMapping());
createIndexRequest.mapping(indexInfoReq.getIndexName(),jsonString,XContentType.JSON);
}
try {
restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
} catch (Exception e) {
throw new RuntimeException("创建失败",e);
}
}
@Override
public void delIndex(String indexName) {
try {
boolean existIndex = isExistIndex(indexName);
if (!existIndex){
throw new RuntimeException("索引:"+indexName+"不存在");
}
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
restHighLevelClient.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
*
* @param indexName
* @return
*/
public boolean isExistIndex(String indexName) {
GetIndexRequest getIndexResult = new GetIndexRequest(indexName);
try {
boolean exists = restHighLevelClient.indices().exists(getIndexResult, RequestOptions.DEFAULT);
return exists;
} catch (Exception e) {
throw new RuntimeException("查询索引失败",e);
}
}
/**
*
* @param indexName
* @return
*/
@Override
public IndexInfoResp find(String indexName) {
if(isExistIndex(indexName)){
IndexInfoResp indexInfoResp = new IndexInfoResp();
GetIndexRequest getIndexRequest = new GetIndexRequest(indexName);
try {
IndexInfoResp indexInfoResp1 = new IndexInfoResp();
GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);
indexInfoResp1.setIndexName(getIndexResponse.getIndices()[0]);
indexInfoResp1.setMappings(getIndexResponse.getMappings().get(indexName).getSourceAsMap());
Settings settings = getIndexResponse.getSettings().get(indexName);
Set<String> strings = settings.keySet();
HashMap<String, Object> map = new HashMap<>();
strings.forEach(key->{
map.put(key,settings.get(key));
});
indexInfoResp1.setSettings(map);
Map<String, List<AliasMetadata>> aliases = getIndexResponse.getAliases();
Map<String,Object> aliasMap=JSONObject.parseObject(JSONObject.toJSONString(aliases),Map.class);
indexInfoResp.setAliases(aliasMap);
return indexInfoResp;
} catch (Exception e) {
throw new RuntimeException("索引失败",e);
}
}else {
throw new RuntimeException("索引名不存在");
}
}
@Override
public void updIndex(IndexEntry indexEntry) {
try {
if (isExistIndex(indexEntry.getIndexName())){
PutMappingRequest putMappingRequest = new PutMappingRequest(indexEntry.getIndexName());
String jsonString = JSONObject.toJSONString(indexEntry.getIndexMapping());
putMappingRequest.source(jsonString,XContentType.JSON);
restHighLevelClient.indices().putMapping(putMappingRequest,RequestOptions.DEFAULT);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,10 @@
Spring Boot Version: ${spring-boot.version}
Spring Application Name: ${spring.application.name}
_ _
(_) | |
_ __ _ _ ___ _ _ _ ______ ___ _ _ ___ | |_ ___ _ __ ___
| '__|| | | | / _ \ | | | || ||______|/ __|| | | |/ __|| __| / _ \| '_ ` _ \
| | | |_| || (_) || |_| || | \__ \| |_| |\__ \| |_ | __/| | | | | |
|_| \__,_| \___/ \__, ||_| |___/ \__, ||___/ \__| \___||_| |_| |_|
__/ | __/ |
|___/ |___/

View File

@ -0,0 +1,39 @@
# Tomcat
server:
port: 9204
# Spring
spring:
redis:
host: 101.34.77.101
port: 6379
password: cbx@123
application:
# 应用名称
name: bawei-es
profiles:
# 环境配置
active: dev
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: 101.34.77.101:8848
namespace: 20041112
config:
# 配置中心地址
server-addr: 101.34.77.101:8848
namespace: 20041112
# 配置文件格式
file-extension: yml
# 共享配置
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
# feign 配置
feign:
compression:
request:
enabled: true
min-request-size: 10000
response:
enabled: true

View File

@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 日志存放路径 -->
<property name="log.path" value="logs/bawei-system" />
<!-- 日志输出格式 -->
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />
<!-- 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!-- 系统日志输出 -->
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/info.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>INFO</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/error.log</file>
<!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>ERROR</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 系统模块日志级别控制 -->
<logger name="com.bawei" level="info" />
<!-- Spring日志级别控制 -->
<logger name="org.springframework" level="warn" />
<root level="info">
<appender-ref ref="console" />
</root>
<!--系统操作日志-->
<root level="info">
<appender-ref ref="file_info" />
<appender-ref ref="file_error" />
</root>
</configuration>

View File

@ -2,14 +2,20 @@
<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>HomeWork</groupId> <groupId>HomeWork</groupId>
<artifactId>HomeWork-modules</artifactId> <artifactId>HomeWork-modules</artifactId>
<version>3.6.3</version> <version>3.6.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>HomeWork-es</artifactId> <artifactId>base-es</artifactId>
<packaging>pom</packaging>
<modules>
<module>base-es-common</module>
<module>base-es-server</module>
<module>base-es-remote</module>
</modules>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>
@ -17,4 +23,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<description>
bawei-es系统模块
</description>
</project> </project>

View File

@ -1,7 +0,0 @@
package HomeWork;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

View File

@ -2,19 +2,125 @@
<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>HomeWork</groupId> <groupId>HomeWork</groupId>
<artifactId>HomeWork-modules</artifactId> <artifactId>HomeWork-modules</artifactId>
<version>3.6.3</version> <version>3.6.3</version>
</parent> </parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>HomeWork-kafka</artifactId> <artifactId>HomeWork-kafka</artifactId>
<properties> <dependencies>
<maven.compiler.source>17</maven.compiler.source> <dependency>
<maven.compiler.target>17</maven.compiler.target> <groupId>commons-io</groupId>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <artifactId>commons-io</artifactId>
</properties> <version>2.11.0</version>
</dependency>
<!-- 人脸-->
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
<!-- 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>
<!-- Swagger UI -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.fox.version}</version>
</dependency>
<!-- RuoYi Common DataScope -->
<dependency>
<groupId>HomeWork</groupId>
<artifactId>HomeWork-common-datascope</artifactId>
</dependency>
<!-- RuoYi Common Log -->
<dependency>
<groupId>HomeWork</groupId>
<artifactId>HomeWork-common-log</artifactId>
</dependency>
<!-- RuoYi Common Swagger -->
<dependency>
<groupId>HomeWork</groupId>
<artifactId>HomeWork-common-swagger</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>HomeWork</groupId>
<artifactId>base-es-remote</artifactId>
<version>3.6.3</version>
<exclusions>
<exclusion>
<artifactId>mybatis-spring-boot-starter</artifactId>
<groupId>org.mybatis.spring.boot</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -0,0 +1,20 @@
package HomeWork;
import HomeWork.common.security.annotation.EnableCustomConfig;
import HomeWork.common.security.annotation.EnableRyFeignClients;
import HomeWork.common.swagger.annotation.EnableCustomSwagger2;
import com.bawei.es.remote.api.RemoteEsDocService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@EnableCustomConfig
@EnableCustomSwagger2
@EnableRyFeignClients
@SpringBootApplication
@ComponentScan(basePackageClasses = {RemoteEsDocService.class},basePackages = {"HomeWork","com.bawei"})
public class HomeWorkKafkaApp {
public static void main(String[] args) {
SpringApplication.run(HomeWorkKafkaApp.class);
}
}

View File

@ -1,7 +0,0 @@
package HomeWork;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

View File

@ -0,0 +1,45 @@
package HomeWork.consumer;
import com.bawei.es.domain.DocumentReq;
import com.bawei.es.remote.api.RemoteEsDocService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Component
public class Consumer {
@Resource
private RemoteEsDocService remoteEsDocService;
/**
*
* @param records
* @param ack
*/
@KafkaListener(topics = {"test"},groupId = "2004")
public void func(List<ConsumerRecord<String, Object>> records, Acknowledgment ack){
for (ConsumerRecord<String, Object> record : records) {
String topic = record.topic();
log.info("订阅topic{}, 数据内容:{}", topic, record.value());
DocumentReq documentReq = new DocumentReq();
documentReq.setIndexName("img");
ArrayList<Map<String, Object>> maps = new ArrayList<>();
HashMap<String, Object> stringObjectHashMap = new HashMap<>();
stringObjectHashMap.put("img",record.value());
maps.add(stringObjectHashMap);
documentReq.setDocumentContent(maps);
remoteEsDocService.add(documentReq);
// 确认消息已消费
ack.acknowledge();
}
}
}

View File

@ -0,0 +1,24 @@
package HomeWork.controller;
import HomeWork.common.core.domain.R;
import HomeWork.service.KafkaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/kafka")
public class KafkaController {
@Autowired
private KafkaService kafkaService;
@PostMapping("/send")
public R send(@RequestParam String img) {
kafkaService.send(img);
return R.ok();
}
}

View File

@ -0,0 +1,8 @@
package HomeWork.service;
import java.util.List;
public interface KafkaService {
void send(String img);
}

View File

@ -0,0 +1,33 @@
package HomeWork.service.impl;
import HomeWork.service.KafkaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFutureCallback;
import java.util.List;
@Service
@Slf4j
public class KafkaServiceImpl implements KafkaService {
@Autowired
private KafkaTemplate<String, Object> kafkaTemplate;
@Override
public void send(String img) {
kafkaTemplate.send("test", img).addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
@Override
public void onFailure(Throwable throwable) {
log.error("发送消息失败:{}", throwable.getMessage());
}
@Override
public void onSuccess(SendResult<String, Object> result) {
log.info("发送消息成功:{}-{}-{}", result.getRecordMetadata().topic(), result.getRecordMetadata().partition(), result.getRecordMetadata().offset());
}
});
log.info("生产了一条新数据:{}", img);
}
}

View File

@ -0,0 +1,27 @@
# Tomcat
server:
port: 9401
# Spring
spring:
application:
# 应用名称
name: HomeWork-kafka
profiles:
# 环境配置
active: dev
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: 101.34.77.101:8848
namespace: 20041112
config:
# 配置中心地址
server-addr: 101.34.77.101:8848
namespace: 20041112
# 配置文件格式
file-extension: yml
# 共享配置
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}