java spring boot中elasticsearch 自定义查询
Posted 我想出去玩-
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java spring boot中elasticsearch 自定义查询相关的知识,希望对你有一定的参考价值。
spring boot 中连接使用elasticsearch 可使用spring-data-elasticsearch连接,操作简单,但是对于复杂查询和条件判断查询不易操作。后续使用org.elasticsearch.client.RestHighLevelClient查询。
RestHighLevelClient 配置文件
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
elasticsearch:
host: 192.168.61.106
port: 9200
clustername: elasticsearch
username: elastic
password: gobon888
@Configuration
@Slf4j
public class ElasticSearchConfig
/**
* 主机
*/
@Value("$elasticsearch.host")
private String esHost;
/**
* 传输层端口,注意和ES的Restful API默认9200端口有区分
*/
@Value("$elasticsearch.port")
private int esPort;
/**
* 用户名
*/
@Value("$elasticsearch.username")
private String userName;
/**
* 密码
*/
@Value("$elasticsearch.password")
private String password;
@Bean
public RestHighLevelClient client()
try
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(userName, password));
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost(esHost, esPort))
.setHttpClientConfigCallback(httpClientBuilder ->
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
);
return client;
catch (Exception ex)
log.error("es初始化出错:", ex.getMessage());
return null;
查询使用
@Autowired
private RestHighLevelClient restHighLevelClient;
@Override
public List<ProcessNodeLog> getCallEsLog(CallEsQueryLogParam param)
List<ProcessNodeLog> result = new ArrayList<>();
// 设置查询索引
SearchRequest searchRequest = new SearchRequest("process_node_log");
// 查询构造器
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 查询过滤器
BoolQueryBuilder filter = QueryBuilders.boolQuery();
// 用户id
if (param.getUserId() != null)
MatchQueryBuilder userId = QueryBuilders.matchQuery("operatorUserId", param.getUserId());
filter.filter(userId);
// 时间
if (StringUtils.isNotBlank(param.getStartTime()) || StringUtils.isNotBlank(param.getEndTime()))
RangeQueryBuilder time = QueryBuilders.rangeQuery("createTime");
if (StringUtils.isNotBlank(param.getStartTime()))
time.gte(param.getStartTime());
if (StringUtils.isNotBlank(param.getEndTime()))
time.lte(param.getEndTime());
filter.filter(time);
// 电话号码
if (StringUtils.isNotBlank(param.getOperatorNumber()))
MatchQueryBuilder number = QueryBuilders.matchQuery("phoneNumber", param.getPhoneNumber());
filter.filter(number);
// 自定义唯一id
if (StringUtils.isNotBlank(param.getUniqueId()))
MatchQueryBuilder query = QueryBuilders.matchQuery("uniqueId", param.getUniqueId());
filter.filter(query);
sourceBuilder.query(filter);
// 排序
sourceBuilder.sort("createTime", SortOrder.DESC);
// 获取数量
sourceBuilder.size(param.getCount() == null ? 10 : param.getCount());
searchRequest.source(sourceBuilder);
try
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
for (SearchHit hit : hits)
String sourceAsString = hit.getSourceAsString();
ProcessNodeLog text = JSON.parseObject(sourceAsString, ProcessNodeLog.class);
result.add(text);
catch (IOException e)
e.printStackTrace();
log.error("查询失败:", e.getMessage());
return result;
以上是关于java spring boot中elasticsearch 自定义查询的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Spring Boot 应用程序登录 Amazon Elastic Beanstalk
使用“spring-boot-starter-parent”时如何在 Maven 中使用较低的 Elastic 搜索版本
您如何将 Spring Boot 日志直接摄取到 Elastic 中
java.lang.RuntimeException:从本地使用 Spring Boot 容器 API 时连接被拒绝