背景
之前写过一篇《java使用transportClient连接elasticsearch并做接口实现增删改查ES6.4.3版本》的文章,在项目中可以作为基本的使用。但是没有权限验证及分词的处理。在资料查找中,发现TransportClien客户端工具会在之后的版本中作废。且实践中6.4.3版本无法对账密验证(可能方式不对)。遂改造成了RestHighLevelClient客户端实现之前的功能。主要逻辑都差不多。这里做个整理
pom配置相关包
我这里把涉及到的核心包整理,方便拷贝
接口定义
和之前相比,多了个分词结果返回接口
package com.kaishiba.elasticsearch.service;import java.util.List;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;public abstract interface ElasticsearchService
{public abstract RestHighLevelClient getRestHighLevelClient()throws Exception;public abstract SearchResponse getSearchResult(SearchRequest paramSearchRequest)throws Exception;public abstract SearchResponse getSearchResult(String paramString1, String paramString2, SearchSourceBuilder paramSearchSourceBuilder)throws Exception;public abstract SearchResponse getSearchResult(String paramString1, String paramString2, Integer paramInteger1, Integer paramInteger2, String paramString3, QueryBuilder paramQueryBuilder, AggregationBuilder paramAggregationBuilder)throws Exception;public abstract boolean updateIndexRequest(String paramString1, String paramString2, String paramString3, String paramString4)throws Exception;public abstract boolean deleteIndex(String paramString1, String paramString2, String paramString3)throws Exception;public abstract List
}
接口实现及认证设置
package com.kaishiba.elasticsearch.service.impl;import java.util.ArrayList;
import java.util.List;import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.InitializingBean;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kaishiba.elasticsearch.service.ElasticsearchService;/*** es通用化组件方法实现* @author chenhailong* @date 2019年8月12日 上午9:59:23*/
public class ElasticsearchServiceImplimplements ElasticsearchService, InitializingBean
{private String ip;private int port;private String userName;private String userPwd;private RestHighLevelClient client;public String getIp(){return this.ip;}public void setIp(String ip){this.ip = ip;}public int getPort(){return this.port;}public void setPort(int port){this.port = port;}public String getUserPwd(){return this.userPwd;}public void setUserPwd(String userPwd){this.userPwd = userPwd;}public String getUserName(){return this.userName;}public void setUserName(String userName){this.userName = userName;}public RestHighLevelClient getRestHighLevelClient(){return this.client;}public SearchResponse getSearchResult(SearchRequest request)throws Exception{return this.client.search(request, RequestOptions.DEFAULT);}public SearchResponse getSearchResult(String indexName, String typeName, SearchSourceBuilder searchSourceBuilder)throws Exception{return this.client.search(new SearchRequest().indices(new String[] { indexName }).types(new String[] { typeName }).source(searchSourceBuilder), RequestOptions.DEFAULT);}public SearchResponse getSearchResult(String indexName, String typeName, Integer page, Integer pageSize, String sortDesc, QueryBuilder queryBuilder, AggregationBuilder aggregationBuilder)throws Exception{return this.client.search(new SearchRequest().indices(new String[] { indexName }).types(new String[] { typeName }).source(new SearchSourceBuilder().query(queryBuilder).from(page.intValue() * pageSize.intValue()).size(pageSize.intValue()).sort(sortDesc, SortOrder.DESC).aggregation(aggregationBuilder)), RequestOptions.DEFAULT);}public boolean updateIndexRequest(String indexName, String typeName, String id, String jsonStr)throws Exception{IndexRequest createRequest = new IndexRequest(indexName, typeName, id);createRequest.source(jsonStr, XContentType.JSON);UpdateRequest updateRequest = new UpdateRequest(indexName, typeName, id);updateRequest.doc(jsonStr, XContentType.JSON).upsert(createRequest);UpdateResponse updateResponse = this.client.update(updateRequest, RequestOptions.DEFAULT);if ((updateResponse.getResult().getLowercase().equals("updated")) || (updateResponse.getResult().getLowercase().equals("created"))) {return true;}return false;}public boolean deleteIndex(String indexName, String typeName, String id)throws Exception{DeleteRequest deleteRequest = new DeleteRequest(indexName, typeName, id);DeleteResponse deleteResponse = this.client.delete(deleteRequest, RequestOptions.DEFAULT);if (deleteResponse.getResult().getLowercase().equals("deleted")) {return true;}return false;}public List
spring bean配置如下
写个测试类如下
注:ik分词需要下载对应版本的分词插件‘elasticsearch-analysis-ik-6.4.2’,装好之后可以进行如下操作
/** Copyright 2016 kaistart.com All right reserved. This software is the* confidential and proprietary information of kaistart.com ("Confidential* Information"). You shall not disclose such Confidential Information and shall* use it only in accordance with the terms of the license agreement you entered* into with kaistart.com.*/
package com.kaishiba.elasticsearch.test;import java.util.List;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.kaishiba.elasticsearch.service.ElasticsearchService;/*** @author chenhailong* @date 2019年8月12日 上午10:07:11 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/spring-es.xml"})
public class App {@Autowiredprivate ElasticsearchService es;@Testpublic void getInfo() {try {List
}
返回结果如下:
listing key :中华人民共和国
listing key :中华人民
listing key :中华
listing key :华人
listing key :人民共和国
listing key :人民
listing key :共和国
listing key :共和
listing key :国合
listing key :合同法
listing key :合同
listing key :法
当修改为错误的账号密码之后,运行以下代码依然正常
@Test
public void get() {
Request request = new Request("GET","/");
try {System.out.println(es.getRestHighLevelClient().getLowLevelClient().performRequest(request));
} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();
}
}# Response{requestLine=GET / HTTP/1.1, host=http://127.0.0.1:9200, response=HTTP/1.1 200 OK}
这是由于没有安装 x-pack插件,参考详情查看
《单独安装elasticsearch6.x并破解xpack开启SSL认证进行测试使用》
《elasticsearch6.4.2安装x-pack安全认证后,java如何使用transportclient连接/resthighlevelclient连接》