use of com.fanxb.bookmark.common.exception.EsException in project bookmark by FleyX.
the class EsUtil method insertOrUpdateOne.
/**
* Description: 插入/更新一条记录
*
* @param index index
* @param entity 对象
* @author fanxb
* @date 2019/7/24 15:02
*/
public void insertOrUpdateOne(String index, EsEntity entity) {
if (!status) {
return;
}
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 EsException(e);
}
}
use of com.fanxb.bookmark.common.exception.EsException in project bookmark by FleyX.
the class EsUtil method deleteBatch.
/**
* Description: 批量删除
*
* @param index index
* @param idList 待删除列表
* @author fanxb
* @date 2019/7/25 14:24
*/
public <T> void deleteBatch(String index, Collection<String> idList) {
if (!status) {
return;
}
BulkRequest request = new BulkRequest();
idList.forEach(item -> request.add(new DeleteRequest(index, item)));
try {
client.bulk(request, RequestOptions.DEFAULT);
} catch (Exception e) {
throw new EsException(e);
}
}
use of com.fanxb.bookmark.common.exception.EsException in project bookmark by FleyX.
the class EsUtil method deleteByQuery.
/**
* 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) {
if (!status) {
return;
}
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 EsException(e);
}
}
use of com.fanxb.bookmark.common.exception.EsException in project bookmark by FleyX.
the class EsUtil method search.
/**
* 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) {
if (!status) {
return null;
}
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 EsException(e);
}
}
use of com.fanxb.bookmark.common.exception.EsException in project bookmark by FleyX.
the class EsUtil method insertBatch.
/**
* Description: 批量插入数据
*
* @param index index
* @param list 带插入列表
* @author fanxb
* @date 2019/7/24 17:38
*/
public <T> void insertBatch(String index, List<EsEntity<T>> list) {
if (!status) {
return;
}
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 EsException(e);
}
}
Aggregations