Search in sources :

Example 1 with EsException

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);
    }
}
Also used : EsException(com.fanxb.bookmark.common.exception.EsException) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) CustomException(com.fanxb.bookmark.common.exception.CustomException) EsException(com.fanxb.bookmark.common.exception.EsException)

Example 2 with EsException

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);
    }
}
Also used : EsException(com.fanxb.bookmark.common.exception.EsException) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) CustomException(com.fanxb.bookmark.common.exception.CustomException) EsException(com.fanxb.bookmark.common.exception.EsException)

Example 3 with EsException

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);
    }
}
Also used : EsException(com.fanxb.bookmark.common.exception.EsException) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) CustomException(com.fanxb.bookmark.common.exception.CustomException) EsException(com.fanxb.bookmark.common.exception.EsException)

Example 4 with EsException

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);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) EsException(com.fanxb.bookmark.common.exception.EsException) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) CustomException(com.fanxb.bookmark.common.exception.CustomException) EsException(com.fanxb.bookmark.common.exception.EsException) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 5 with EsException

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);
    }
}
Also used : EsException(com.fanxb.bookmark.common.exception.EsException) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) CustomException(com.fanxb.bookmark.common.exception.CustomException) EsException(com.fanxb.bookmark.common.exception.EsException)

Aggregations

CustomException (com.fanxb.bookmark.common.exception.CustomException)5 EsException (com.fanxb.bookmark.common.exception.EsException)5 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)2 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)2 GetIndexRequest (org.elasticsearch.client.indices.GetIndexRequest)2 ArrayList (java.util.ArrayList)1 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)1 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 DeleteByQueryRequest (org.elasticsearch.index.reindex.DeleteByQueryRequest)1 SearchHit (org.elasticsearch.search.SearchHit)1