feat(商城): 添加elasticsearch

master
wayn 4 years ago
parent 2ce28d853a
commit 605a86ac22

@ -26,6 +26,33 @@
<version>${swagger.version}</version>
</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>
<groupId>com.wayn</groupId>
<artifactId>waynboot-common</artifactId>
@ -38,8 +65,24 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<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>
</plugins>
</build>
</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.wayn.common.base.ElasticEntity;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
@ -35,18 +36,14 @@ public class BaseElasticService {
RestHighLevelClient restHighLevelClient;
/**
* @param idxName
* @param idxSQL
* @return void
* @throws
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 17:30
* @since
*
*
* @param idxName
* @param idxSQL
*/
public void createIndex(String idxName, String idxSQL) {
try {
if (!this.indexExist(idxName)) {
if (this.indexExist(idxName)) {
log.error(" idxName={} 已经存在,idxSql={}", idxName, idxSQL);
return;
}
@ -67,13 +64,9 @@ public class BaseElasticService {
/**
* index
*
* @param idxName index
* @param idxName
* @return boolean
* @throws
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 17:27
* @since
* @throws Exception
*/
public boolean indexExist(String idxName) throws Exception {
GetIndexRequest request = new GetIndexRequest(idxName);
@ -81,7 +74,7 @@ public class BaseElasticService {
request.humanReadable(true);
request.includeDefaults(false);
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
* @return boolean
* @throws
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 17:27
* @since
*/
public boolean isExistsIndex(String idxName) throws Exception {
return restHighLevelClient.indices().exists(new GetIndexRequest(idxName), RequestOptions.DEFAULT);
@ -103,12 +91,6 @@ public class BaseElasticService {
*
*
* @param request
* @return void
* @throws
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 19:27
* @since
*/
public void buildSetting(CreateIndexRequest request) {
request.settings(Settings.builder().put("index.number_of_shards", 3)
@ -118,12 +100,6 @@ public class BaseElasticService {
/**
* @param idxName index
* @param entity
* @return void
* @throws
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 17:27
* @since
*/
public void insertOrUpdateOne(String idxName, ElasticEntity entity) {
IndexRequest request = new IndexRequest(idxName);
@ -144,12 +120,6 @@ public class BaseElasticService {
*
* @param idxName index
* @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) {
BulkRequest request = new BulkRequest();
@ -167,12 +137,6 @@ public class BaseElasticService {
*
* @param idxName index
* @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) {
BulkRequest request = new BulkRequest();
@ -189,11 +153,6 @@ public class BaseElasticService {
* @param builder
* @param c
* @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) {
SearchRequest request = new SearchRequest(idxName);
@ -214,17 +173,11 @@ public class BaseElasticService {
/**
* index
*
* @param idxName
* @return void
* @throws
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 17:13
* @since
* @param idxName
*/
public void deleteIndex(String idxName) {
try {
if (!this.indexExist(idxName)) {
if (this.indexExist(idxName)) {
log.error(" idxName={} 已经存在", idxName);
return;
}
@ -236,14 +189,10 @@ public class BaseElasticService {
/**
* @param idxName
* @param builder
* @return void
* @throws
* @author WCNGS@QQ.COM
* @See
* @date 2019/10/17 17:13
* @since
*
*
* @param idxName
* @param builder
*/
public void deleteByQuery(String idxName, QueryBuilder builder) {

@ -31,32 +31,6 @@
<version>${spring-boot.version}</version>
</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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>

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

Loading…
Cancel
Save