Commit 6710c8df by leejoker

es批量操作,增加列表非空判断

1 parent 2d183fb7
......@@ -32,6 +32,7 @@ import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.io.IOException;
import java.util.ArrayList;
......@@ -225,7 +226,9 @@ public class ElasticSearch {
list.parallelStream().forEach(item -> request.add(new IndexRequest(idxName, "_doc").id(item.getId())
.source(item.getData(), XContentType.JSON)));
try {
restHighLevelClient.bulkAsync(request, RequestOptions.DEFAULT, getActionListener(consumer, list, param));
if (!CollectionUtils.isEmpty(list)) {
restHighLevelClient.bulkAsync(request, RequestOptions.DEFAULT, getActionListener(consumer, list, param));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
......@@ -236,7 +239,9 @@ public class ElasticSearch {
list.parallelStream().forEach(item -> request.add(new IndexRequest(idxName, "_doc").id(item.getString("msgid"))
.source(item, XContentType.JSON)));
try {
restHighLevelClient.bulkAsync(request, RequestOptions.DEFAULT, getActionListener(consumer, list));
if (!CollectionUtils.isEmpty(list)) {
restHighLevelClient.bulkAsync(request, RequestOptions.DEFAULT, getActionListener(consumer, list));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
......@@ -246,7 +251,9 @@ public class ElasticSearch {
BulkRequest request = new BulkRequest();
list.forEach(item -> request.add(new UpdateRequest(idxName, item.getId()).upsert(item.getData(), XContentType.JSON)));
try {
restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
if (!CollectionUtils.isEmpty(list)) {
restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
......@@ -268,7 +275,9 @@ public class ElasticSearch {
BulkRequest request = new BulkRequest();
idList.forEach(item -> request.add(new DeleteRequest(idxName, "_doc", item.toString())));
try {
restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
if (!CollectionUtils.isEmpty(idList)) {
restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!