背景
之前写过一篇《java使用transportClient连接elasticsearch并做接口实现增删改查ES6.4.3版本》的文章,在项目中可以作为基本的使用。但是没有权限验证及分词的处理。在资料查找中,发现TransportClien客户端工具会在之后的版本中作废。且实践中6.4.3版本无法对账密验证(可能方式不对)。遂改造成了RestHighLevelClient客户端实现之前的功能。主要逻辑都差不多。这里做个整理
pom配置相关包
我这里把涉及到的核心包整理,方便拷贝
org.springframework
spring-beans
3.1.2.RELEASE
com.alibaba
fastjson
1.2.28
junit
junit
3.8.1
test
org.elasticsearch
elasticsearch
6.4.3
org.elasticsearch.client
transport
6.4.3
org.elasticsearch
elasticsearch-core
6.4.3
compile
org.elasticsearch
elasticsearch-secure-sm
6.4.3
compile
org.elasticsearch
elasticsearch-x-content
6.4.3
compile
org.apache.lucene
lucene-core
7.4.0
compile
org.apache.lucene
lucene-analyzers-common
7.4.0
compile
org.apache.lucene
lucene-core
org.apache.lucene
lucene-backward-codecs
7.4.0
compile
org.apache.lucene
lucene-core
org.apache.lucene
lucene-grouping
7.4.0
compile
org.apache.lucene
lucene-core
org.apache.lucene
lucene-queries
org.apache.lucene
lucene-highlighter
7.4.0
compile
org.apache.lucene
lucene-analyzers-common
org.apache.lucene
lucene-core
org.apache.lucene
lucene-join
org.apache.lucene
lucene-memory
org.apache.lucene
lucene-queries
org.apache.lucene
lucene-join
7.4.0
compile
org.apache.lucene
lucene-core
org.apache.lucene
lucene-memory
7.4.0
compile
org.apache.lucene
lucene-core
org.apache.lucene
lucene-misc
7.4.0
compile
org.apache.lucene
lucene-core
org.apache.lucene
lucene-queries
7.4.0
compile
org.apache.lucene
lucene-core
org.apache.lucene
lucene-queryparser
7.4.0
compile
org.apache.lucene
lucene-core
org.apache.lucene
lucene-queries
org.apache.lucene
lucene-sandbox
org.apache.lucene
lucene-sandbox
7.4.0
compile
org.apache.lucene
lucene-core
org.apache.lucene
lucene-spatial
7.4.0
compile
org.apache.lucene
lucene-core
org.apache.lucene
lucene-spatial-extras
7.4.0
compile
org.apache.lucene
lucene-core
org.apache.lucene
lucene-spatial3d
io.sgr
s2-geometry-library-java
org.locationtech.spatial4j
spatial4j
org.apache.lucene
lucene-spatial3d
7.4.0
compile
org.apache.lucene
lucene-core
org.apache.lucene
lucene-suggest
7.4.0
compile
org.apache.lucene
lucene-analyzers-common
org.apache.lucene
lucene-core
org.elasticsearch
elasticsearch-cli
6.4.3
compile
com.carrotsearch
hppc
0.7.1
compile
joda-time
joda-time
2.10
compile
com.tdunning
t-digest
3.2
compile
org.hdrhistogram
HdrHistogram
2.1.9
compile
org.locationtech.spatial4j
spatial4j
0.7
compile
true
org.locationtech.jts
jts-core
1.15.0
compile
true
org.apache.logging.log4j
log4j-api
2.11.1
compile
org.apache.logging.log4j
log4j-core
2.11.1
compile
org.apache.logging.log4j
log4j-api
true
org.apache.logging.log4j
log4j-1.2-api
2.11.1
compile
org.apache.logging.log4j
log4j-core
org.apache.logging.log4j
log4j-api
true
org.elasticsearch
jna
4.5.1
compile
org.elasticsearch.client
elasticsearch-rest-high-level-client
6.4.2
org.elasticsearch.client
elasticsearch-rest-client
6.4.2
org.springframework
spring-test
3.1.2.RELEASE
test
org.springframework
spring-context
3.1.2.RELEASE
test
junit
junit
4.12
test
org.slf4j
slf4j-api
1.7.7
接口定义
和之前相比,多了个分词结果返回接口
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 getAnalyze(String paramString)
throws Exception;
}
接口实现及认证设置
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 ElasticsearchServiceImpl
implements 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 getAnalyze(String text)
throws Exception
{
List list = new ArrayList();
text = text.length() > 100 ? text.substring(0, 100) : text;
Request request = new Request("GET", "_analyze");
JSONObject entity = new JSONObject();
entity.put("analyzer", "ik_max_word");
entity.put("text", text);
request.setJsonEntity(entity.toJSONString());
Response response = this.client.getLowLevelClient().performRequest(request);
JSONObject tokens = JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
JSONArray arrays = tokens.getJSONArray("tokens");
for (int i = 0; i < arrays.size(); i++)
{
JSONObject obj = JSON.parseObject(arrays.getString(i));
list.add(obj.getString("token"));
}
return list;
}
public void afterPropertiesSet()
throws Exception
{
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(this.userName, this.userPwd));
RestClientBuilder builder = RestClient.builder(new HttpHost[] { new HttpHost(this.ip, this.port) }).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback()
{
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder)
{
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
this.client = new RestHighLevelClient(builder);
}
}
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 {
@Autowired
private ElasticsearchService es;
@Test
public void getInfo() {
try {
List ls = es.getAnalyze("中华人民共和国合同法");
for(String s: ls) {
System.out.println("listing key :" + s);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
返回结果如下:
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 block
e.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连接》