1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| @Slf4j public class EsserviceImpl implements IEsService {
@Autowired public RestHighLevelClient client;
protected static final RequestOptions COMMON_OPTIONS;
static { RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder(); builder.setHttpAsyncResponseConsumerFactory(new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(30 * 1024 * 1024)); COMMON_OPTIONS = builder.build(); }
@Override public void createIndexRequest(String index) { CreateIndexRequest createIndexRequest = new CreateIndexRequest(index) .settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 0)); try { CreateIndexResponse response = client.indices().create(createIndexRequest, COMMON_OPTIONS); log.info(" 所有节点确认响应 : {}", response.isAcknowledged()); log.info(" 所有分片的复制未超时 :{}", response.isShardsAcknowledged()); } catch (IOException e) { log.error("创建索引库【{}】失败", index, e); } }
@Override public void deleteIndexRequest(String index) { DeleteIndexRequest request = new DeleteIndexRequest(index); try { AcknowledgedResponse response = client.indices().delete(request, COMMON_OPTIONS); System.out.println(response.isAcknowledged()); } catch (IOException e) { log.error("删除索引库【{}】失败", index, e); } }
@Override public void updateRequest(String index, String id, Object object) { UpdateRequest updateRequest = new UpdateRequest(index, id); updateRequest.doc(BeanUtil.beanToMap(object), XContentType.JSON); try { client.update(updateRequest, COMMON_OPTIONS); } catch (IOException e) { log.error("更新索引文档 {" + index + "} 数据 {" + object + "} 失败", e); } }
@Override public void insertRequest(String index, String id, Object object) { IndexRequest indexRequest = new IndexRequest(index).id(id).source(BeanUtil.beanToMap(object), XContentType.JSON); try { client.index(indexRequest, COMMON_OPTIONS); } catch (IOException e) { log.error("创建索引文档 {" + index + "} 数据 {" + object + "} 失败", e); }
}
@Override public void deleteRequest(String index, String id) { DeleteRequest deleteRequest = new DeleteRequest(index, id); try { client.delete(deleteRequest, COMMON_OPTIONS); } catch (IOException e) { log.error("删除索引文档 {" + index + "} 数据id {" + id + "} 失败", e); } } }
|