关于elasticsearchJavaHighLevelREST的封装操作_第1页
关于elasticsearchJavaHighLevelREST的封装操作_第2页
关于elasticsearchJavaHighLevelREST的封装操作_第3页
关于elasticsearchJavaHighLevelREST的封装操作_第4页
关于elasticsearchJavaHighLevelREST的封装操作_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

关于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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论