新增es demo
This commit is contained in:
parent
5e7ebf99f9
commit
44ecafd5fa
@ -1,6 +1,7 @@
|
||||
package sort.exchange;
|
||||
|
||||
import util.ArrayUtil;
|
||||
import util.NumberUtil;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@ -18,25 +19,33 @@ public class QuickSort {
|
||||
if (start >= end) {
|
||||
return;
|
||||
}
|
||||
int base = arr[start], i = start, j = end;
|
||||
int baseIndex = NumberUtil.getRandom(start, end);
|
||||
int base = arr[baseIndex], i = start, j = end;
|
||||
while (i < j) {
|
||||
while (arr[i] <= base && i < end) {
|
||||
i++;
|
||||
}
|
||||
while (arr[j] >= base && j > start) {
|
||||
while (arr[j] >= base && j > i) {
|
||||
j--;
|
||||
}
|
||||
while (arr[i] <= base && i < j) {
|
||||
i++;
|
||||
}
|
||||
|
||||
|
||||
if (i < j) {
|
||||
ArrayUtil.swap(arr, i, j);
|
||||
ArrayUtil.swap(arr, i++, j--);
|
||||
System.out.println(Arrays.toString(arr));
|
||||
}
|
||||
}
|
||||
boolean isSwap = (baseIndex > j && arr[baseIndex] < arr[j]) || (baseIndex < j && arr[baseIndex] > arr[j]);
|
||||
if (isSwap) {
|
||||
ArrayUtil.swap(arr, baseIndex, i);
|
||||
}
|
||||
System.out.println(Arrays.toString(arr));
|
||||
deal(arr, start, i);
|
||||
deal(arr, j, end);
|
||||
deal(arr, start, i - 1);
|
||||
deal(arr, j + 1, end);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Integer[] arr = {4, 3, 1, 89, 5};
|
||||
Integer[] arr = {1, 43, 2, 3, 4, 5, 6, 7, 5, 6, 555, 12, 31, 5, 5};
|
||||
deal(arr, 0, arr.length - 1);
|
||||
}
|
||||
}
|
||||
|
17
3.排序算法/src/util/NumberUtil.java
Normal file
17
3.排序算法/src/util/NumberUtil.java
Normal file
@ -0,0 +1,17 @@
|
||||
package util;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/5/17 17:15
|
||||
*/
|
||||
public class NumberUtil {
|
||||
|
||||
public static int getRandom(int min, int max) {
|
||||
return (int) Math.ceil(min + Math.random() * (max - min));
|
||||
}
|
||||
|
||||
|
||||
}
|
15
3.排序算法/src/util/StringUtil.java
Normal file
15
3.排序算法/src/util/StringUtil.java
Normal file
@ -0,0 +1,15 @@
|
||||
package util;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/4/4 16:17
|
||||
*/
|
||||
public class StringUtil {
|
||||
|
||||
public static boolean isEmpty(String str) {
|
||||
return str == null || str.trim().length() == 0;
|
||||
}
|
||||
}
|
31
es-demo/.gitignore
vendored
Normal file
31
es-demo/.gitignore
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**
|
||||
!**/src/test/**
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
79
es-demo/pom.xml
Normal file
79
es-demo/pom.xml
Normal file
@ -0,0 +1,79 @@
|
||||
<?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>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.6.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.fanxb</groupId>
|
||||
<artifactId>es-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>es-demo</name>
|
||||
<description>Elasticsearch Demo project for Spring Boot</description>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<!--注意:如果使用了parent那么需要在此定义es版本号,因为spring-boot-start-parent中已经定义了es相关依赖的版本号
|
||||
,high-level-client中的部分依赖会被覆盖成低版本的,导出出现莫名其妙的错误-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch.client</groupId>
|
||||
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||
<version>7.2.0</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch</groupId>
|
||||
<artifactId>elasticsearch</artifactId>
|
||||
<version>7.2.0</version>
|
||||
</dependency>
|
||||
<!--<!– https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-client –>-->
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch.client</groupId>
|
||||
<artifactId>elasticsearch-rest-client</artifactId>
|
||||
<version>7.2.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.elasticsearch.client</groupId>
|
||||
<artifactId>elasticsearch-rest-high-level-client</artifactId>
|
||||
<version>7.2.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.56</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,13 @@
|
||||
package com.fanxb.esdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class EsDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(EsDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
56
es-demo/src/main/java/com/fanxb/esdemo/entity/Book.java
Normal file
56
es-demo/src/main/java/com/fanxb/esdemo/entity/Book.java
Normal file
@ -0,0 +1,56 @@
|
||||
package com.fanxb.esdemo.entity;
|
||||
|
||||
/**
|
||||
* 类功能简述: 插入es的数据
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/7/29 11:33
|
||||
*/
|
||||
public class Book {
|
||||
private Integer id;
|
||||
private Integer userId;
|
||||
private String name;
|
||||
|
||||
public Book() {
|
||||
}
|
||||
|
||||
public Book(Integer id, Integer userId, String name) {
|
||||
this.id = id;
|
||||
this.userId = userId;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Book{" +
|
||||
"id=" + id +
|
||||
", userId=" + userId +
|
||||
", name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(Integer userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
39
es-demo/src/main/java/com/fanxb/esdemo/entity/EsEntity.java
Normal file
39
es-demo/src/main/java/com/fanxb/esdemo/entity/EsEntity.java
Normal file
@ -0,0 +1,39 @@
|
||||
package com.fanxb.esdemo.entity;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/7/29 11:29
|
||||
*/
|
||||
public final class EsEntity<T> {
|
||||
|
||||
private String id;
|
||||
private T data;
|
||||
|
||||
public EsEntity() {
|
||||
}
|
||||
|
||||
public EsEntity(String id, T data) {
|
||||
this.data = data;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
115
es-demo/src/main/java/com/fanxb/esdemo/service/BookService.java
Normal file
115
es-demo/src/main/java/com/fanxb/esdemo/service/BookService.java
Normal file
@ -0,0 +1,115 @@
|
||||
package com.fanxb.esdemo.service;
|
||||
|
||||
import com.fanxb.esdemo.entity.Book;
|
||||
import com.fanxb.esdemo.entity.EsEntity;
|
||||
import com.fanxb.esdemo.util.EsUtil;
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.index.query.TermQueryBuilder;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/7/29 14:31
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/book")
|
||||
@Service
|
||||
public class BookService {
|
||||
|
||||
@Autowired
|
||||
private EsUtil esUtil;
|
||||
|
||||
/**
|
||||
* @param id 获取某一个
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public Book getById(@PathVariable("id") int id) {
|
||||
SearchSourceBuilder builder = new SearchSourceBuilder();
|
||||
builder.query(new TermQueryBuilder("id", id));
|
||||
List<Book> res = esUtil.search(EsUtil.INDEX_NAME, builder, Book.class);
|
||||
if (res.size() > 0) {
|
||||
return res.get(0);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取全部
|
||||
*/
|
||||
@GetMapping("/")
|
||||
public List<Book> getAll() {
|
||||
return esUtil.search(EsUtil.INDEX_NAME, new SearchSourceBuilder(), Book.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据关键词搜索某用户下的书
|
||||
*
|
||||
* @param content 关键词
|
||||
*/
|
||||
@GetMapping("/search")
|
||||
public List<Book> searchByUserIdAndName(int userId, String content) {
|
||||
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
|
||||
boolQueryBuilder.must(QueryBuilders.termQuery("userId", userId));
|
||||
boolQueryBuilder.must(QueryBuilders.matchQuery("name", content));
|
||||
SearchSourceBuilder builder = new SearchSourceBuilder();
|
||||
builder.size(10).query(boolQueryBuilder);
|
||||
return esUtil.search(EsUtil.INDEX_NAME, builder, Book.class);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 单个插入
|
||||
*
|
||||
* @param book book
|
||||
*/
|
||||
@PutMapping("/")
|
||||
public void putOne(@RequestBody Book book) {
|
||||
EsEntity<Book> entity = new EsEntity<>(book.getId().toString(), book);
|
||||
esUtil.insertOrUpdateOne(EsUtil.INDEX_NAME, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入
|
||||
*
|
||||
* @param books books
|
||||
*/
|
||||
@PutMapping("/many")
|
||||
public void putList(@RequestBody List<Book> books) {
|
||||
List<EsEntity> list = new ArrayList<>();
|
||||
books.forEach(item -> list.add(new EsEntity<>(item.getId().toString(), item)));
|
||||
esUtil.insertBatch(EsUtil.INDEX_NAME, list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除
|
||||
*
|
||||
* @param list list
|
||||
*/
|
||||
@DeleteMapping("/deleteBatch")
|
||||
public void deleteBatch(List<Integer> list) {
|
||||
esUtil.deleteBatch(EsUtil.INDEX_NAME, list);
|
||||
}
|
||||
|
||||
/**
|
||||
* delete by query 根据用户id删除数据
|
||||
*
|
||||
* @param userId userId
|
||||
*/
|
||||
@DeleteMapping("/userId/{userId}")
|
||||
public void deleteByUserId(@PathVariable("userId") int userId) {
|
||||
esUtil.deleteByQuery(EsUtil.INDEX_NAME, new TermQueryBuilder("userId", userId));
|
||||
}
|
||||
|
||||
|
||||
}
|
235
es-demo/src/main/java/com/fanxb/esdemo/util/EsUtil.java
Normal file
235
es-demo/src/main/java/com/fanxb/esdemo/util/EsUtil.java
Normal file
@ -0,0 +1,235 @@
|
||||
package com.fanxb.esdemo.util;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.fanxb.esdemo.entity.EsEntity;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
|
||||
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.client.RequestOptions;
|
||||
import org.elasticsearch.client.RestClient;
|
||||
import org.elasticsearch.client.RestHighLevelClient;
|
||||
import org.elasticsearch.client.indices.CreateIndexRequest;
|
||||
import org.elasticsearch.client.indices.CreateIndexResponse;
|
||||
import org.elasticsearch.client.indices.GetIndexRequest;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/7/29 11:24
|
||||
*/
|
||||
@Component
|
||||
public class EsUtil {
|
||||
|
||||
|
||||
@Value("${es.host}")
|
||||
public String host;
|
||||
@Value("${es.port}")
|
||||
public int port;
|
||||
@Value("${es.scheme}")
|
||||
public String scheme;
|
||||
|
||||
public static final String INDEX_NAME = "book-index";
|
||||
|
||||
public static final String CREATE_INDEX = "{\n" +
|
||||
" \"properties\": {\n" +
|
||||
" \"id\":{\n" +
|
||||
" \"type\":\"integer\"\n" +
|
||||
" },\n" +
|
||||
" \"userId\":{\n" +
|
||||
" \"type\":\"integer\"\n" +
|
||||
" },\n" +
|
||||
" \"name\":{\n" +
|
||||
" \"type\":\"text\",\n" +
|
||||
" \"analyzer\": \"ik_max_word\",\n" +
|
||||
" \"search_analyzer\": \"ik_smart\"\n" +
|
||||
" },\n" +
|
||||
" \"url\":{\n" +
|
||||
" \"type\":\"text\",\n" +
|
||||
" \"index\": true,\n" +
|
||||
" \"analyzer\": \"ik_max_word\",\n" +
|
||||
" \"search_analyzer\": \"ik_smart\"\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" }";
|
||||
|
||||
public static RestHighLevelClient client = null;
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
try {
|
||||
if (client != null) {
|
||||
client.close();
|
||||
}
|
||||
client = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, scheme)));
|
||||
if (this.indexExist(INDEX_NAME)) {
|
||||
return;
|
||||
}
|
||||
CreateIndexRequest request = new CreateIndexRequest(INDEX_NAME);
|
||||
request.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2));
|
||||
request.mapping(CREATE_INDEX, XContentType.JSON);
|
||||
CreateIndexResponse res = client.indices().create(request, RequestOptions.DEFAULT);
|
||||
if (!res.isAcknowledged()) {
|
||||
throw new RuntimeException("初始化失败");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description: 判断某个index是否存在
|
||||
*
|
||||
* @param index index名
|
||||
* @return boolean
|
||||
* @author fanxb
|
||||
* @date 2019/7/24 14:57
|
||||
*/
|
||||
public boolean indexExist(String index) throws Exception {
|
||||
GetIndexRequest request = new GetIndexRequest(index);
|
||||
request.local(false);
|
||||
request.humanReadable(true);
|
||||
request.includeDefaults(false);
|
||||
return client.indices().exists(request, RequestOptions.DEFAULT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Description: 插入/更新一条记录
|
||||
*
|
||||
* @param index index
|
||||
* @param entity 对象
|
||||
* @author fanxb
|
||||
* @date 2019/7/24 15:02
|
||||
*/
|
||||
public void insertOrUpdateOne(String index, EsEntity entity) {
|
||||
IndexRequest request = new IndexRequest(index);
|
||||
request.id(entity.getId());
|
||||
request.source(JSON.toJSONString(entity.getData()), XContentType.JSON);
|
||||
try {
|
||||
client.index(request, RequestOptions.DEFAULT);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description: 批量插入数据
|
||||
*
|
||||
* @param index index
|
||||
* @param list 带插入列表
|
||||
* @author fanxb
|
||||
* @date 2019/7/24 17:38
|
||||
*/
|
||||
public void insertBatch(String index, List<EsEntity> list) {
|
||||
BulkRequest request = new BulkRequest();
|
||||
list.forEach(item -> request.add(new IndexRequest(index).id(item.getId())
|
||||
.source(JSON.toJSONString(item.getData()), XContentType.JSON)));
|
||||
try {
|
||||
client.bulk(request, RequestOptions.DEFAULT);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description: 批量删除
|
||||
*
|
||||
* @param index index
|
||||
* @param idList 待删除列表
|
||||
* @author fanxb
|
||||
* @date 2019/7/25 14:24
|
||||
*/
|
||||
public <T> void deleteBatch(String index, Collection<T> idList) {
|
||||
BulkRequest request = new BulkRequest();
|
||||
idList.forEach(item -> request.add(new DeleteRequest(index, item.toString())));
|
||||
try {
|
||||
client.bulk(request, RequestOptions.DEFAULT);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description: 搜索
|
||||
*
|
||||
* @param index index
|
||||
* @param builder 查询参数
|
||||
* @param c 结果类对象
|
||||
* @return java.util.ArrayList
|
||||
* @author fanxb
|
||||
* @date 2019/7/25 13:46
|
||||
*/
|
||||
public <T> List<T> search(String index, SearchSourceBuilder builder, Class<T> c) {
|
||||
SearchRequest request = new SearchRequest(index);
|
||||
request.source(builder);
|
||||
try {
|
||||
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
|
||||
SearchHit[] hits = response.getHits().getHits();
|
||||
List<T> res = new ArrayList<>(hits.length);
|
||||
for (SearchHit hit : hits) {
|
||||
res.add(JSON.parseObject(hit.getSourceAsString(), c));
|
||||
}
|
||||
return res;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description: 删除index
|
||||
*
|
||||
* @param index index
|
||||
* @return void
|
||||
* @author fanxb
|
||||
* @date 2019/7/26 11:30
|
||||
*/
|
||||
public void deleteIndex(String index) {
|
||||
try {
|
||||
client.indices().delete(new DeleteIndexRequest(index), RequestOptions.DEFAULT);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Description: delete by query
|
||||
*
|
||||
* @param index index
|
||||
* @param builder builder
|
||||
* @author fanxb
|
||||
* @date 2019/7/26 15:16
|
||||
*/
|
||||
public void deleteByQuery(String index, QueryBuilder builder) {
|
||||
DeleteByQueryRequest request = new DeleteByQueryRequest(index);
|
||||
request.setQuery(builder);
|
||||
//设置批量操作数量,最大为10000
|
||||
request.setBatchSize(10000);
|
||||
request.setConflicts("proceed");
|
||||
try {
|
||||
client.deleteByQuery(request, RequestOptions.DEFAULT);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
3
es-demo/src/main/resources/application.properties
Normal file
3
es-demo/src/main/resources/application.properties
Normal file
@ -0,0 +1,3 @@
|
||||
es.host=192.168.64.129
|
||||
es.port=9200
|
||||
es.scheme=http
|
@ -0,0 +1,60 @@
|
||||
package com.fanxb.esdemo;
|
||||
|
||||
import com.fanxb.esdemo.service.BookService;
|
||||
import com.fanxb.esdemo.entity.Book;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class EsDemoApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private BookService bookService;
|
||||
|
||||
@Test
|
||||
public void getOne() {
|
||||
System.out.println(bookService.getById(1).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAll() {
|
||||
List<Book> res = bookService.getAll();
|
||||
res.forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addOneTest() {
|
||||
bookService.putOne(new Book(1, 1, "格林童话"));
|
||||
bookService.putOne(new Book(2, 1, "美人鱼"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addBatchTest() {
|
||||
List<Book> list = new ArrayList<>();
|
||||
list.add(new Book(3, 1, "第一本书"));
|
||||
list.add(new Book(4, 1, "第二本书"));
|
||||
bookService.putList(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteBatch() {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
list.add(1);
|
||||
list.add(3);
|
||||
bookService.deleteBatch(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteByQuery(){
|
||||
bookService.deleteByUserId(1);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user