feat(商城): 添加elasticsearch

master
wayn 4 years ago
parent 2ce28d853a
commit 605a86ac22

@ -26,6 +26,33 @@
<version>${swagger.version}</version> <version>${swagger.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.2</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.4.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.4.2</version>
</dependency>
<dependency> <dependency>
<groupId>com.wayn</groupId> <groupId>com.wayn</groupId>
<artifactId>waynboot-common</artifactId> <artifactId>waynboot-common</artifactId>
@ -38,8 +65,24 @@
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 指定该Main Class为全局的唯一入口 -->
<mainClass>com.wayn.AdminApplication</mainClass>
<layout>ZIP</layout>
<commandlineArguments>
-parameters
</commandlineArguments>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
</goals>
</execution>
</executions>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
</project> </project>

@ -0,0 +1,46 @@
package com.wayn.admin.api.controller.shop;
import com.wayn.admin.framework.manager.elastic.service.BaseElasticService;
import com.wayn.common.base.ElasticEntity;
import com.wayn.common.util.R;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("elastic")
public class ElasticController {
@Autowired
private BaseElasticService baseElasticService;
@GetMapping("insert")
public R insert() {
ElasticEntity elasticEntity = new ElasticEntity();
elasticEntity.setId("2");
Map<String, Object> map = new HashMap<>();
map.put("name", "书本2");
map.put("price", 1000D);
map.put("date", new Date());
elasticEntity.setData(map);
baseElasticService.insertOrUpdateOne("store", elasticEntity);
return R.success();
}
@GetMapping("search")
public R search() {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("name", "沃尔玛"));
List<Object> list = baseElasticService.search("store", searchSourceBuilder, Object.class);
return R.success().add("data", list);
}
}

@ -1,23 +0,0 @@
package com.wayn.admin.api.controller.shop;
import com.wayn.common.base.BaseElasticService;
import com.wayn.common.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("test")
public class TestController {
@Autowired
private BaseElasticService baseElasticService;
@GetMapping
public R test() {
baseElasticService.createIndex("2", "");
return R.success();
}
}

@ -1,6 +1,7 @@
package com.wayn.common.base; package com.wayn.admin.framework.manager.elastic.service;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.wayn.common.base.ElasticEntity;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkRequest;
@ -35,18 +36,14 @@ public class BaseElasticService {
RestHighLevelClient restHighLevelClient; RestHighLevelClient restHighLevelClient;
/** /**
* @param idxName *
* @param idxSQL *
* @return void * @param idxName
* @throws * @param idxSQL
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 17:30
* @since
*/ */
public void createIndex(String idxName, String idxSQL) { public void createIndex(String idxName, String idxSQL) {
try { try {
if (!this.indexExist(idxName)) { if (this.indexExist(idxName)) {
log.error(" idxName={} 已经存在,idxSql={}", idxName, idxSQL); log.error(" idxName={} 已经存在,idxSql={}", idxName, idxSQL);
return; return;
} }
@ -67,13 +64,9 @@ public class BaseElasticService {
/** /**
* index * index
* *
* @param idxName index * @param idxName
* @return boolean * @return boolean
* @throws * @throws Exception
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 17:27
* @since
*/ */
public boolean indexExist(String idxName) throws Exception { public boolean indexExist(String idxName) throws Exception {
GetIndexRequest request = new GetIndexRequest(idxName); GetIndexRequest request = new GetIndexRequest(idxName);
@ -81,7 +74,7 @@ public class BaseElasticService {
request.humanReadable(true); request.humanReadable(true);
request.includeDefaults(false); request.includeDefaults(false);
request.indicesOptions(IndicesOptions.lenientExpandOpen()); request.indicesOptions(IndicesOptions.lenientExpandOpen());
return restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT); return !restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
} }
/** /**
@ -89,11 +82,6 @@ public class BaseElasticService {
* *
* @param idxName index * @param idxName index
* @return boolean * @return boolean
* @throws
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 17:27
* @since
*/ */
public boolean isExistsIndex(String idxName) throws Exception { public boolean isExistsIndex(String idxName) throws Exception {
return restHighLevelClient.indices().exists(new GetIndexRequest(idxName), RequestOptions.DEFAULT); return restHighLevelClient.indices().exists(new GetIndexRequest(idxName), RequestOptions.DEFAULT);
@ -103,12 +91,6 @@ public class BaseElasticService {
* *
* *
* @param request * @param request
* @return void
* @throws
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 19:27
* @since
*/ */
public void buildSetting(CreateIndexRequest request) { public void buildSetting(CreateIndexRequest request) {
request.settings(Settings.builder().put("index.number_of_shards", 3) request.settings(Settings.builder().put("index.number_of_shards", 3)
@ -118,12 +100,6 @@ public class BaseElasticService {
/** /**
* @param idxName index * @param idxName index
* @param entity * @param entity
* @return void
* @throws
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 17:27
* @since
*/ */
public void insertOrUpdateOne(String idxName, ElasticEntity entity) { public void insertOrUpdateOne(String idxName, ElasticEntity entity) {
IndexRequest request = new IndexRequest(idxName); IndexRequest request = new IndexRequest(idxName);
@ -144,12 +120,6 @@ public class BaseElasticService {
* *
* @param idxName index * @param idxName index
* @param list * @param list
* @return void
* @throws
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 17:26
* @since
*/ */
public void insertBatch(String idxName, List<ElasticEntity> list) { public void insertBatch(String idxName, List<ElasticEntity> list) {
BulkRequest request = new BulkRequest(); BulkRequest request = new BulkRequest();
@ -167,12 +137,6 @@ public class BaseElasticService {
* *
* @param idxName index * @param idxName index
* @param idList * @param idList
* @return void
* @throws
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 17:14
* @since
*/ */
public <T> void deleteBatch(String idxName, Collection<T> idList) { public <T> void deleteBatch(String idxName, Collection<T> idList) {
BulkRequest request = new BulkRequest(); BulkRequest request = new BulkRequest();
@ -189,11 +153,6 @@ public class BaseElasticService {
* @param builder * @param builder
* @param c * @param c
* @return java.util.List<T> * @return java.util.List<T>
* @throws
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 17:14
* @since
*/ */
public <T> List<T> search(String idxName, SearchSourceBuilder builder, Class<T> c) { public <T> List<T> search(String idxName, SearchSourceBuilder builder, Class<T> c) {
SearchRequest request = new SearchRequest(idxName); SearchRequest request = new SearchRequest(idxName);
@ -214,17 +173,11 @@ public class BaseElasticService {
/** /**
* index * index
* *
* @param idxName * @param idxName
* @return void
* @throws
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 17:13
* @since
*/ */
public void deleteIndex(String idxName) { public void deleteIndex(String idxName) {
try { try {
if (!this.indexExist(idxName)) { if (this.indexExist(idxName)) {
log.error(" idxName={} 已经存在", idxName); log.error(" idxName={} 已经存在", idxName);
return; return;
} }
@ -236,14 +189,10 @@ public class BaseElasticService {
/** /**
* @param idxName *
* @param builder *
* @return void * @param idxName
* @throws * @param builder
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 17:13
* @since
*/ */
public void deleteByQuery(String idxName, QueryBuilder builder) { public void deleteByQuery(String idxName, QueryBuilder builder) {

@ -31,32 +31,6 @@
<version>${spring-boot.version}</version> <version>${spring-boot.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.0</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.4.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.4.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId> <artifactId>spring-boot-starter-security</artifactId>

@ -2,13 +2,15 @@ package com.wayn.common.base;
import lombok.Data; import lombok.Data;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
/**
* elastic
*/
@Data @Data
public class ElasticEntity { public class ElasticEntity {
private String id; private String id;
private Map<String, Object> data = new HashMap<>(); private Map<String, Object> data;
} }

Loading…
Cancel
Save