update
This commit is contained in:
parent
46851ebe1c
commit
1c8608ce7d
@ -178,13 +178,13 @@ es 的中文分词目前比较流行的分词插件为 ik([github 地址](https:
|
||||
|
||||
**注意:这里有一个依赖的大坑,要注意!**
|
||||
|
||||
如果定义了`<parent>`,就必须在`<dependencyManagement>`中指定部分依赖的版本,否则会出现各种莫名其妙的错误,上面注释中已经指出。
|
||||
如果定义了`<parent>`,就必须在`<dependencyManagement>`中指定部分依赖的版本,否则会因为依赖版本不对出现各种莫名其妙的错误,上面注释中已经指出。
|
||||
|
||||
## 创建 util/EsUtil.java 工具类
|
||||
|
||||
主要功能函数如下:
|
||||
|
||||
1. 预创建 index
|
||||
### 预创建 index
|
||||
|
||||
虽然 es 在插入数据时会自动根据字段类型来创建字段定义,但是自动创建并不总是和需要相符的,比如想让某个字段不分词,或者使用其他的分词器。所以在代码中先判断 index(es7 中已经废弃了 mapping,也就是一个 index 相当于一个表)是否存在,如果不存在就创建 index.
|
||||
|
||||
@ -216,7 +216,9 @@ public void init() {
|
||||
}
|
||||
```
|
||||
|
||||
2. 插入或者更新一个对象
|
||||
### 插入或者更新一个对象
|
||||
|
||||
通过指定 id,如果此 id 存在那么就是更新,否则是插入。
|
||||
|
||||
```java
|
||||
public void insertOrUpdateOne(String index, EsEntity entity) {
|
||||
@ -230,3 +232,85 @@ public void insertOrUpdateOne(String index, EsEntity entity) {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 批量插入
|
||||
|
||||
high level client 提供了方便的批量操作接口,如下所示:
|
||||
|
||||
```java
|
||||
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);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 批量删除
|
||||
|
||||
和上面一样同样用到了`BulkRequest`
|
||||
|
||||
```java
|
||||
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);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 搜索
|
||||
|
||||
通过构建`SearchSourceBuilder`查询参数
|
||||
|
||||
```java
|
||||
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);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### delete by query
|
||||
|
||||
es 插入数据容易,删除就比较麻烦了,特别是根据条件删除。
|
||||
|
||||
```java
|
||||
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);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# 结束
|
||||
|
||||
可通过测试类`com.fanxb.esdemo.service.BookServiceTest`查看运行结果。
|
||||
|
||||
源码地址:[github](https://github.com/FleyX/demo-project/tree/master/es-demo)
|
||||
|
||||
**本文原创发布于:**[https://www.tapme.top/blog/detail/2019-07-29-14-59](https://www.tapme.top/blog/detail/2019-07-29-14-59)
|
||||
|
Loading…
x
Reference in New Issue
Block a user