概述
依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.2</version>
</dependency>
配置
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.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;
import java.util.LinkedList;
import java.util.List;
@Configuration
public class ElasticSearchConfig {
/**
* 协议
*/
@Value("${elasticsearch.schema:http}")
private String schema;
/**
* 集群地址,如果有多个用“,”隔开
*/
@Value("#{'${elasticsearch.address}'.split(',')}")
private List<String> addresses;
@Value("${elasticsearch.certificated:true}")
private boolean certificated;
@Value("${elasticsearch.username}")
private String username;
@Value("${elasticsearch.password}")
private String password;
/**
* 连接超时时间
*/
@Value("${elasticsearch.connectTimeout:5000}")
private int connectTimeout;
/**
* Socket 连接超时时间
*/
@Value("${elasticsearch.socketTimeout:5000}")
private int socketTimeout;
/**
* 获取连接的超时时间
*/
@Value("${elasticsearch.connectionRequestTimeout:5000}")
private int connectionRequestTimeout;
/**
* 最大连接数
*/
@Value("${elasticsearch.maxConnectNum:100}")
private int maxConnectNum;
/**
* 最大路由连接数
*/
@Value("${elasticsearch.maxConnectPerRoute:100}")
private int maxConnectPerRoute;
@Bean(name = "restHighLevelClient")
public RestHighLevelClient restHighLevelClient() {
// 拆分地址
List<HttpHost> hostLists = new LinkedList<>();
for (String address : addresses) {
String host = address.split(":")[0];
String port = address.split(":")[1];
hostLists.add(new HttpHost(host, Integer.parseInt(port), schema));
}
// 设置密码
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
// 转换成 HttpHost 数组
HttpHost[] httpHost = hostLists.toArray(new HttpHost[]{});
// 构建连接对象
RestClientBuilder builder = RestClient.builder(httpHost);
builder.setRequestConfigCallback(requestConfigBuilder -> {
// 异步连接延时配置
requestConfigBuilder.setConnectTimeout(connectTimeout);
requestConfigBuilder.setSocketTimeout(socketTimeout);
requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeout);
return requestConfigBuilder;
});
builder.setHttpClientConfigCallback(httpClientBuilder -> {
// 异步连接数配置
httpClientBuilder.setMaxConnTotal(maxConnectNum);
httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
// 密码设置
if (certificated) {
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
return httpClientBuilder;
});
return new RestHighLevelClient(builder);
}
}
一些封装的方法
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
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.replication.ReplicationResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
@Component
public class ElasticSearchDao {
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticSearchDao.class);
@Resource
private RestHighLevelClient restHighLevelClient;
/**
* 创建document
*
* @param indexRequest index请求
* @return 创建结果
*/
public boolean index(IndexRequest indexRequest) {
try {
boolean created = true;
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
if (shardInfo.getFailed() > 0) {
created = false;
for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
LOGGER.error("index fail, cause: ", failure.getCause());
}
}
return created;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 批量创建document
*
* @return 创建成功数量
* @date 2020/6/18 10:00
*/
public List<String> bulkIndex(BulkRequest bulkRequest) {
List<String> list = new LinkedList<>();
try {
BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
for (BulkItemResponse bulkItemResponse : bulkResponse) {
if (bulkItemResponse.isFailed()) {
BulkItemResponse.Failure failure = bulkItemResponse.getFailure();
LOGGER.error("index bulkIndex, cause: ", failure.getCause());
} else {
list.add(bulkItemResponse.getId());
}
}
return list;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 获取
*
* @param getRequest get请求
* @return source
*/
public String get(GetRequest getRequest) {
try {
GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
if (!getResponse.isExists()) {
return null;
}
return getResponse.getSourceAsString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 计数
*
* @param countRequest count请求
* @return 数量
*/
public long count(CountRequest countRequest) {
try {
CountResponse countResponse = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT);
return countResponse.getCount();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 搜索
*
* @param searchRequest search请求
* @return 文档
*/
public SearchHits search(SearchRequest searchRequest) {
try {
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
return searchResponse.getHits();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 删除API
*/
public boolean delete(DeleteRequest deleteRequest) {
boolean delete = false;
try {
DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
DocWriteResponse.Result result = deleteResponse.getResult();
if (DocWriteResponse.Result.DELETED.equals(result)) {
delete = true;
}
return delete;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 按查询删除
*/
public void deleteByQuery(DeleteByQueryRequest deleteByQueryRequest) {
try {
restHighLevelClient.deleteByQuery(deleteByQueryRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 更新API
*/
public boolean update(UpdateRequest updateRequest) {
boolean update = false;
try {
UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
DocWriteResponse.Result result = updateResponse.getResult();
if (DocWriteResponse.Result.UPDATED.equals(result) || DocWriteResponse.Result.CREATED.equals(result)) {
update = true;
}
return update;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 高亮对象
*
* @param name fieldName
* @return HighlightBuilder
*/
public HighlightBuilder highlightBuilder(String name) {
HighlightBuilder highlightBuilder = new HighlightBuilder();
HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("title");
highlightTitle.highlighterType("unified");
highlightBuilder.field(highlightTitle);
HighlightBuilder.Field highlightField = new HighlightBuilder.Field(name);
highlightBuilder.field(highlightField);
return highlightBuilder;
}
}
测试
@Test
public void testBulkIndex() {
String index = "";
BulkRequest bulkRequest = new BulkRequest(index);
Map<String, Object> map = new HashMap<>();
map.put("key","value");
IndexRequest indexRequest = new IndexRequest();
indexRequest.source(JSON.toJSONString(map, SerializerFeature.WriteNullStringAsEmpty), XContentType.JSON);
// 可添加多个indexRequest
bulkRequest.add(indexRequest);
elasticSearchDao.bulkIndex(bulkRequest);
}
最后
以上就是自信滑板为你收集整理的java 集成 elastic search 7.6.2/7.7.0的全部内容,希望文章能够帮你解决java 集成 elastic search 7.6.2/7.7.0所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复