配置类
package com.rest.utils.esutils;
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.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @ClassName a
* @Description: TODO
* @Author majt
* @Date 2019/9/26
* @Version V1.0
**/
@Configuration
public class ESConfig {
@Value("${es.username}")
public String ES_USER = "patentv4";
@Value("${es.password}")
public String ES_PASSWORD = "patentv4";
@Value("${es.ip}")
public String ES_IP = "47.100.136.202";
@Value("${es.port}")
public int ES_PORT = 9205;
public static final String POST = "POST";
public static final String GET = "GET";
public static final String PUT = "PUT";
public static final String DELETE = "DELETE";
@Bean("client")
public RestHighLevelClient restClient() {
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
initRestClient());
return restHighLevelClient;
}
@Bean("esLowClient")
public RestClient restLowClient() {
return initRestClient().build();
}
public RestClientBuilder initRestClient() {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
System.out.println(ES_USER);
credentialsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(ES_USER, ES_PASSWORD));
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(ES_IP, ES_PORT, "http"))
//设置超时时间
.setRequestConfigCallback(
requestConfigBuilder -> requestConfigBuilder
.setConnectTimeout(5000)
.setSocketTimeout(60000)
)
.setHttpClientConfigCallback(httpClientBuilder -> {
//设置线程数
HttpAsyncClientBuilder httpAsyncClientBuilder = httpClientBuilder.setDefaultIOReactorConfig(
IOReactorConfig.custom()
.setIoThreadCount(1)
.build());
//设置认证信息
httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
return httpAsyncClientBuilder;
});
return restClientBuilder;
}
}
工具类
package com.rest.utils.esutils;
import com.alibaba.fastjson.JSONObject;
import com.rest.common.ApplicationContextManagerial;
import lombok.extern.log4j.Log4j2;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.*;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
/**
* @author: create by majt
* @version: v1.0
* @description: es工具类
* @date:2019/10/9 14:27
*/
@Log4j2
public class ESUtil {
private static RestClient esLowClient;
public static RestClient getEsLowClient() {
if (esLowClient == null) {
esLowClient = ApplicationContextManagerial.getBean("esLowClient", RestClient.class);
}
if (esLowClient == null) {
throw new RuntimeException("获取es低级客户端实例出错");
}
return esLowClient;
}
/**
* 删除文档
*
* @param client
* @param index
* @param docId
* @return
*/
public static DeleteResponse deleteDocument(RestHighLevelClient client, String index, String docId) {
DeleteRequest deleteRequest = new DeleteRequest(index, docId);
DeleteResponse delete = null;
try {
delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return delete;
}
/**
* 根据query条件删除
*
* @param client
* @param indices
* @param fieldName
* @param value
* @return
*/
public static BulkByScrollResponse deleteByQuery(RestHighLevelClient client, String[] indices, String fieldName, String value) {
DeleteByQueryRequest request = new DeleteByQueryRequest(indices);
TermQueryBuilder termQueryBuilder = new TermQueryBuilder(fieldName, value);
request.setQuery(termQueryBuilder);
request.setConflicts("proceed");
request.setRouting("=cat");
request.setRefresh(true);
BulkByScrollResponse bulkByScrollResponse = null;
try {
bulkByScrollResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return bulkByScrollResponse;
}
/**
* 清空索引
*
* @param client
* @param indices
* @return
*/
public static BulkByScrollResponse clearIndices(RestHighLevelClient client, String[] indices) {
DeleteByQueryRequest request = new DeleteByQueryRequest(indices);
request.setQuery(QueryBuilders.matchAllQuery());
BulkByScrollResponse bulkByScrollResponse = null;
try {
bulkByScrollResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return bulkByScrollResponse;
}
/**
* 根据id查询文档
*
* @param client
* @param index
* @param docId
* @return
*/
public static GetResponse getDocument(RestHighLevelClient client, String index, String docId) {
GetRequest getRequest = new GetRequest(index, docId);
GetResponse getResponse = null;
try {
getResponse = client.get(getRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return getResponse;
}
/**
* 判断文档是否存在
*
* @param client
* @param index
* @param docId
* @return
*/
public static boolean existsDocument(RestHighLevelClient client, String index, String docId) {
GetRequest getRequest = new GetRequest(index, docId);
boolean exists = false;
try {
exists = client.exists(getRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return exists;
}
/**
* 根据指定字段的多值查询
*
* @param client
* @param index
* @param field
* @param values
* @param includes 结果中包含的字段
* @param excludes 结果中排除的字段
* @return
*/
public static SearchResponse search(RestHighLevelClient client, String index, String field, String[] values, String[] includes, String[] excludes) {
SearchRequest searchRequest = new SearchRequest(index);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.fetchSource(includes, excludes);
sourceBuilder.from(0);
sourceBuilder.size(20);
TermsQueryBuilder queryBuilder = QueryBuilders.termsQuery(field, values);
sourceBuilder.query(queryBuilder);
searchRequest.source(sourceBuilder);
SearchResponse response = doSearch(client, searchRequest);
return response;
}
/**
* 新增文档
*
* @param client
* @param index
* @param jsonString
* @param _id
* @return
*/
public static IndexResponse add(RestHighLevelClient client, String index, String jsonString, String _id) {
IndexRequest request = new IndexRequest(index);
request.id(_id);
request.source(jsonString, XContentType.JSON);
IndexResponse response = null;
try {
response = client.index(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
/**
* 判断索引是否存在
*
* @param index
* @return
*/
public static boolean existsIndex(RestHighLevelClient client, String... index) {
GetIndexRequest request = new GetIndexRequest(index);
boolean exists = false;
try {
exists = client.indices().exists(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return exists;
}
/**
* 创建索引
*
* @param client
* @param index
* @param source
* @return
*/
public static CreateIndexResponse createIndex(RestHighLevelClient client, String index, String source) {
CreateIndexRequest request = new CreateIndexRequest(index);
request.source(source, XContentType.JSON);
CreateIndexResponse createIndexResponse = null;
try {
createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return createIndexResponse;
}
/**
* 释放资源
*
* @param client
*/
public static void close(RestHighLevelClient client) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 统计
*
* @param client
* @param dsl
* @param indices
* @return
*/
public static int queryCount(RestHighLevelClient client, String dsl, String... indices) {
SearchResponse response = wrapperSearch(client, dsl, null, null, 0, 0, indices);
return Integer.parseInt(response.getHits().getTotalHits().value + "");
}
/**
* 检索式检索
*
* @param client
* @param dsl
* @param indices
* @return
*/
public static SearchResponse wrapperSearch(RestHighLevelClient client, String dsl, String[] includes, String[] excludes, int from, int size, String... indices) {
SearchRequest request = new SearchRequest(indices);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
if (size != 0) {
sourceBuilder.from(from < 0 ? 0 : from).size(size);
}
sourceBuilder.fetchSource(includes, excludes);
sourceBuilder.query(QueryBuilders.wrapperQuery(dsl));
request.source(sourceBuilder);
SearchResponse response = doSearch(client, request);
return response;
}
/**
* 执行查询
*
* @param client
* @param request
* @return
*/
private static SearchResponse doSearch(RestHighLevelClient client, SearchRequest request) {
try {
return client.search(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 删除索引
*
* @param client
* @param indices
* @return
*/
public static AcknowledgedResponse deleteIndex(RestHighLevelClient client, String... indices) {
DeleteIndexRequest request = new DeleteIndexRequest(indices);
AcknowledgedResponse response = null;
try {
response = client.indices().delete(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
/**
* es低级客户端方法
*/
public JSONObject send(String method, String index, String urlSuffix, String paramBodey) {
// Map params = Collections.emptyMap();
if (paramBodey == null) {
paramBodey = "";
}
// HttpEntity entity = new NStringEntity(paramBodey, ContentType.APPLICATION_JSON);
Response response = null;
Request request = new Request(
method,
index + urlSuffix);
request.setJsonEntity(paramBodey);
try {
// response = getEsLowClient().performRequest(method, index + urlSuffix, params, entity);
response = getEsLowClient().performRequest(request);
} catch (IOException e) {
log.error("访问es出错:\n" +
"请求参数:"
+ index + urlSuffix + "\n"
+ paramBodey + "\n"
+ "错误详情信息:\n"
, e);
return null;
}
String responseBody = null;
try {
responseBody = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
log.error("解析es返回结果出错:\n", e);
}
return JSONObject.parseObject(responseBody);
}
}
测试类
package com.rest.ipmsv4.inventor.controller;
import cn.hutool.json.JSONUtil;
import com.rest.utils.ESClient.ESUtil;
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.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.index.query.*;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.script.mustache.SearchTemplateRequest;
import org.elasticsearch.script.mustache.SearchTemplateResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
import java.util.*;
/**
* @author: create by majt
* @version: v1.0
* @description: com.rest.ipmsv4.inventor.controller
* @date:2019/9/26 17:09
*/
public class QueryService {
private static RestHighLevelClient client;
public static String ES_USER = "user";
public static String ES_PASSWORD = "pass";
public static String ES_IP = "192.168.1.1";
public static int ES_PORT = 9200;
public static void main(String[] args) throws IOException {
// searchRequest();
// getRequest();
// existsRequest();
// String[] values = new String[]{"AUE8cW0B7fVRN7wPLP7j", "BEE8cW0B7fVRN7wPLf5Q"};
// search(client,"test_517ceb5b-38a4-448f-9859-9967cee634a5","_id",values);
// deleteRequest();
// String dsl = "{\"bool\":{\"should\":[{\"match_phrase\":{\"apply_people\":\"武汉金运激光股份有限公司\"}}]}}";
// query(dsl, "test_517ceb5b-38a4-448f-9859-9967cee634a5");
// String script = "if(ctx._source.age == 22){ctx._source.name+='张三';}";
// ESUtil.updateByQuery(client,script,"test");
// GetResponse document = ESUtil.getDocument(client, "test", "apolz20B7fVRN7wPj8_K");
// List
// System.out.println(friend);
// Map ff = new HashMap<>();
// ff.put("name","小八");
// ff.put("age",55);
// Map ff1 = new HashMap<>();
// ff1.put("name","小九");
// ff1.put("age",56);
// List> f = new ArrayList<>();
// f.add(ff);
// f.add(ff1);
// ESUtil.update(client,"test","9ZDHyW0B7fVRN7wPIDTv","friend",f,"age",55);
String[] includes = {"inventor_info"};
SearchResponse response = ESUtil.matchSearch(client, "test-basic-patent", "apply_num", "CN201721818387.7", includes, null);
System.out.println(response.getHits().getHits()[0]);
client.close();
}
/**
* 删除请求
*/
private static void deleteRequest() throws IOException {
DeleteByQueryRequest request = new DeleteByQueryRequest("test");
request.setQuery(QueryBuilders.matchAllQuery());
client.deleteByQuery(request, RequestOptions.DEFAULT);
}
/**
* get查询
*
* @throws IOException
*/
private static void getRequest() throws IOException {
GetRequest getRequest = new GetRequest("test_basic-patent2", "dEJZcW0B7fVRN7wPxn2D");
GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
String sourceAsString = documentFields.getSourceAsString();
System.out.println(sourceAsString);
}
/**
* 是否存在
*/
private static void existsRequest() throws IOException {
GetRequest getRequest = new GetRequest("test_basic-patent2", "dEJZcW0B7fVRN7wPxn2D");
boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
/**
* search 查询
*
* @throws IOException
*/
private static void searchRequest() throws IOException {
SearchRequest searchRequest = new SearchRequest("test_basic-patent2");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//相似匹配
QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
queryBuilder = QueryBuilders.matchQuery("agent", "黄");
//精准匹配
// queryBuilder = QueryBuilders.termQuery("_id", "dMDJZ20Bnib7SwgvAyVy");
System.out.printf("查询语句: %s", queryBuilder);
searchSourceBuilder.query(queryBuilder);
SearchRequest source = searchRequest.source(searchSourceBuilder);
SearchResponse search = client.search(source, RequestOptions.DEFAULT);
System.out.printf("\n查询结果: %s", JSONUtil.formatJsonStr(search.toString()));
}
public static SearchResponse search(RestHighLevelClient client, String index, String field, String[] values) {
SearchRequest searchRequest = new SearchRequest(index);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.fetchSource(new String[]{"name"}, null);
TermsQueryBuilder queryBuilder = QueryBuilders.termsQuery(field, values);
sourceBuilder.query(queryBuilder);
searchRequest.source(sourceBuilder);
SearchResponse response = null;
try {
response = client.search(searchRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
List<SearchHit> searchHits = Arrays.asList(response.getHits().getHits());
searchHits.forEach(element -> {
System.out.println(element.getId());
System.out.println(element.getSourceAsMap().get("name"));
});
return response;
}
public static void query(String dsl, String... indices) {
SearchRequest request = new SearchRequest(indices);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.wrapperQuery(dsl));
request.source(sourceBuilder);
SearchResponse response = null;
try {
response = client.search(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(response.getHits().getTotalHits().value);
}
static {
client = new RestHighLevelClient(initRestClient());
}
public static RestClientBuilder initRestClient() {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(ES_USER, ES_PASSWORD));
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(ES_IP, ES_PORT, "http"))
//设置超时时间
.setRequestConfigCallback(
requestConfigBuilder -> requestConfigBuilder
.setConnectTimeout(5000)
.setSocketTimeout(60000)
)
.setHttpClientConfigCallback(httpClientBuilder -> {
//设置线程数
HttpAsyncClientBuilder httpAsyncClientBuilder = httpClientBuilder.setDefaultIOReactorConfig(
IOReactorConfig.custom()
.setIoThreadCount(1)
.build());
//设置认证信息
httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
return httpAsyncClientBuilder;
});
return restClientBuilder;
}
}