版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
关于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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年度歌厅网络安全技术服务合同2篇
- 2024年消防设施设备维护保养合同2篇
- 2024年度实习生实习期间企业文化建设合同3篇
- 2024年度房地产信托基金连带担保合同规范3篇
- 2024年度健身俱乐部与管理公司合作经营合同2篇
- 2024年度书画艺术品投资合同3篇
- 2024年度制造业企业间产业升级借款合同范本3篇
- 2024年度内支模架施工安全监理与承包合同范本2篇
- 2024LNG运输合同:公路运输与仓储配送一体化服务2篇
- 2024年度海砂采购合同条件2篇
- 2024-2030年中国美妆工具市场应用趋势分析与前景销售格局研究报告
- 天津市一中2024-2025学年高三第二次模拟生物试题含解析
- 2024年个人家庭房屋装修合同标准版本(四篇)
- 《稻草人》课件-2024-2025学年语文三年级上册统编版
- 《12 玩也有学问》教学设计-2024-2025学年道德与法治一年级上册统编版
- 头脑特工队-Inside-Out中英文字幕对照
- 逆风飞翔向阳而生抗逆力主题班会
- 《野在秋风里》地产秋日美拉德复古生活节市集游园会艺术节活动策划方案
- 2024年全国应急通信比武理论考核试题库(含答案)
- 2025年考研政治政治理论时政热点知识测试题库及答案(共三套)
- 医药行业高效药品配送体系建设方案
评论
0/150
提交评论