




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
关于elasticsearchJavaHighLevelREST的封装操作pox.xml文件添加以下内容org.elasticsearch.clientelasticsearch-rest-high-level-client6.3.2新建ESHighLevelRestUtil.javapackagecom;importjava.util.HashMap;importjava.util.Map;importorg.apache.http.HttpHost;importorg.elasticsearch.ElasticsearchException;importorg.elasticsearch.action.admin.indices.create.CreateIndexRequest;importorg.elasticsearch.action.admin.indices.create.CreateIndexResponse;importorg.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;importorg.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;importorg.elasticsearch.action.admin.indices.get.GetIndexRequest;importorg.elasticsearch.action.delete.DeleteRequest;importorg.elasticsearch.action.delete.DeleteResponse;importorg.elasticsearch.action.index.IndexRequest;importorg.elasticsearch.action.index.IndexResponse;importorg.elasticsearch.action.support.replication.ReplicationResponse;importorg.elasticsearch.action.DocWriteResponse;importorg.elasticsearch.client.RestClient;importorg.elasticsearch.client.RestHighLevelClient;importmon.settings.Settings;importmon.xcontent.XContentType;importorg.elasticsearch.rest.RestStatus;publicclassESHighLevelRestUtil{ staticRestHighLevelClientclient=newRestHighLevelClient( RestClient.builder(newHttpHost("49",9200,"http"))); /** *验证索引是否存在 * *@paramindex *索引名称638棋牌 *@return *@throwsException */ publicbooleanindexExists(Stringindex)throwsException{ GetIndexRequestrequest=newGetIndexRequest(); request.indices(index); request.local(false); request.humanReadable(true); booleanexists=client.indices().exists(request); returnexists; } /** * *@paramindex *@paramindexType *@paramproperties *结构:{name:{type:text}}{age:{type:integer}} *@return *@throwsException */ publicbooleanindexCreate(Stringindex,StringindexType, Map<string,object="">properties)throwsException{ if(indexExists(index)){ returntrue; } CreateIndexRequestrequest=newCreateIndexRequest(index); request.settings(Settings.builder().put("index.number_of_shards",3) .put("index.number_of_replicas",2)); Map<string,object="">jsonMap=newHashMap<>(); Map<string,object="">mapping=newHashMap<>(); mapping.put("properties",properties); jsonMap.put(indexType,mapping); request.mapping(indexType,jsonMap); CreateIndexResponsecreateIndexResponse=client.indices().create( request); booleanacknowledged=createIndexResponse.isAcknowledged(); returnacknowledged; } /** *删除索引 * *@paramindex *@return *@throwsException */ publicbooleanindexDelete(Stringindex)throwsException{ try{ DeleteIndexRequestrequest=newDeleteIndexRequest(index); DeleteIndexResponsedeleteIndexResponse=client.indices().delete( request); returndeleteIndexResponse.isAcknowledged(); }catch(ElasticsearchExceptionexception){ if(exception.status()==RestStatus.NOT_FOUND){ returntrue; }else{ returnfalse; } } } /** *创建更新文档 * *@paramindex *@paramindexType *@paramdocumentId *@paramjosonStr *@return *@throwsException */ publicbooleandocumentCreate(Stringindex,StringindexType, StringdocumentId,StringjosonStr)throwsException{ IndexRequestrequest=newIndexRequest(index,indexType,documentId); request.source(josonStr,XContentType.JSON); IndexResponseindexResponse=client.index(request); if(indexResponse.getResult()==DocWriteResponse.Result.CREATED ||indexResponse.getResult()==DocWriteResponse.Result.UPDATED){ returntrue; } ReplicationResponse.ShardInfoshardInfo=indexResponse.getShardInfo(); if(shardInfo.getTotal()!=shardInfo.getSuccessful()){ returntrue; } if(shardInfo.getFailed()>0){ for(ReplicationResponse.ShardInfo.Failurefailure:shardInfo .getFailures()){ thrownewException(failure.reason()); } } returnfalse; } /** *创建更新索引乐淘棋牌 * *@paramindex *@paramindexType *@paramdocumentId *@parammap *@return *@throwsException */ publicbooleandocumentCreate(Stringindex,StringindexType, StringdocumentId,Map<string,object="">map)throwsException{ IndexRequestrequest=newIndexRequest(index,indexType,documentId); request.source(map); IndexResponseindexResponse=client.index(request); if(indexResponse.getResult()==DocWriteResponse.Result.CREATED ||indexResponse.getResult()==DocWriteResponse.Result.UPDATED){ returntrue; } ReplicationResponse.ShardInfoshardInfo=indexResponse.getShardInfo(); if(shardInfo.getTotal()!=shardInfo.getSuccessful()){ returntrue; } if(shardInfo.getFailed()>0){ for(ReplicationResponse.ShardInfo.Failurefailure:shardInfo .getFailures()){ thrownewException(failure.reason()); } } returnfalse; } /** *创建索引 * *@paramindex *@paramindexType *@paramjosonStr *@return *@throwsException */ publicStringdocumentCreate(Stringindex,StringindexType,StringjosonStr) throwsException{ IndexRequestrequest=newIndexRequest(index,indexType); request.source(josonStr,XContentType.JSON); IndexResponseindexResponse=client.index(request); Stringid=indexResponse.getId(); if(indexResponse.getResult()==DocWriteResponse.Result.CREATED ||indexResponse.getResult()==DocWriteResponse.Result.UPDATED){ returnid; } ReplicationResponse.ShardInfoshardInfo=indexResponse.getShardInfo(); if(shardInfo.getTotal()!=shardInfo.getSuccessful()){ returnid; } if(shardInfo.getFailed()>0){ for(ReplicationResponse.ShardInfo.Failurefailure:shardInfo .getFailures()){ thrownewException(failure.reason()); } } returnnull; } /** *创建索引 * *@paramindex *@paramindexType *@parammap *@return *@throwsException */ publicStringdocumentCreate(Stringindex,StringindexType, Map<string,object="">map)throwsException{ IndexRequestrequest=newIndexRequest(index,indexType); request.source(map); IndexResponseindexResponse=client.index(request); Stringid=indexResponse.getId(); if(indexResponse.getResult()==DocWriteResponse.Result.CREATED ||indexResponse.getResult()==DocWriteResponse.Result.UPDATED){ returnid; } ReplicationResponse.ShardInfoshardInfo=indexResponse.getShardInfo(); if(shardInfo.getTotal()!=shardInfo.getSuccessful()){ returnid; } if(shardInfo.getFailed()>0){ for(ReplicationResponse.ShardInfo.Failurefailure:shardInfo .getFailures()){ thrownewException(failure.reason()); } } returnnull; } publicbooleandocumentDelete(Stringindex,StringindexType, StringdocumentId)throwsException{ DeleteRequestrequest=newDeleteRequest(index,indexType,documentId); DeleteResponsedeleteResponse=client.delete(request); if(deleteResponse.getResult()==DocWriteResponse.Result.NOT_FOUND){ returntrue; } ReplicationResponse.ShardInfoshardInfo=deleteResponse.getShardInfo(); if(shardInfo.getTotal()!=shardInfo.getSuccessful()){ returntrue; } if(shardInfo.getFailed()>0){ for(ReplicationResponse.ShardInfo.Failurefailure:shardInfo .getFailures()){ thrownewException(failure.reason()); } } returnfalse; }}新建ESHighLevelRestTest.javapackagecom;importjava.util.HashMap;importjava.util.Map;importorg.apache.http.HttpHost;importorg.elasticsearch.action.admin.indices.get.GetIndexRequest;importorg.elasticsearch.client.RestClient;importorg.elasticsearch.client.RestHighLevelClient;publicclassESHighLevelRestTest{ publicstaticvoidmain(String[]args)throwsException{ //TODOAuto-generatedmethodstub ESHighLevelRestUtilutil=newESHighLevelRestUtil(); System.out.println(util.indexExists("indextest001")); Map<string,object="">map=newHashMap<string,object="">(); map
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 蜜蜂教学课件下载平台
- 小学教学反思课件
- 肌理相关美术教学课件
- 《争吵》教学课件
- 【三明】2025年福建三明市三元区南部卫生系统事业单位招聘笔试历年典型考题及考点剖析附带答案详解
- 课件单词配音教学
- 新年跳舞活动方案
- 新作文创新大赛活动方案
- 无障碍设施专项活动方案
- 新冠来了我不怕活动方案
- 2025年第一季度烟花爆竹安全作业特种作业操作证考试试卷(新手实战卷)
- 2025年 荆州市荆发控股集团招聘考试笔试试卷附答案
- 2025年新疆生产建设兵团中考语文试卷(含答案)
- 2025年市场营销学课程期末考试试题及答案
- 矿井调度员考试题及答案
- 2025至2030中国清分机行业发展趋势分析与未来投资战略咨询研究报告
- 2024届清华大学强基计划数学学科笔试试题(附答案)
- 布氏杆菌护理查房课件
- 北京市中级专业技术资格评审申报表
- 工厂供电课程设计1
- 鼠害虫害防治管理制度
评论
0/150
提交评论