简介
在项目开发过程中需要从Elasticsearch中查询日志数据,下面介绍从Elasticsearch中查询索引全部数据,以及按照时间区间查询索引数据。
引入依赖
<!-- Elasticsearch客户端 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.9.1</version>
</dependency>
<!-- Elasticsearch版本 -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.9.1</version>
</dependency>
创建配置类
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticsearchConfig {
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
COMMON_OPTIONS = builder.build();
}
@Bean
public RestHighLevelClient esRestClient() {
RestClientBuilder builder = null;
builder = RestClient.builder(new HttpHost("ip address", 9200, "http"));
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
}
查询索引下的全部数据文章来源:https://www.toymoban.com/news/detail-513392.html
public static void matchAllQuery() throws Exception {
ElasticsearchConfig elasticSearchConfig = new ElasticsearchConfig();
RestHighLevelClient client = elasticSearchConfig.esRestClient();
// 搜索请求对象
SearchRequest searchRequest = new SearchRequest("shenyu-access-logging");
// 指定类型
searchRequest.types("_doc");
// 搜索源构建对象
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 搜索方式
// matchAllQuery搜索全部,设置查询数据条数为20。
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchSourceBuilder.size(20);
// 设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
searchSourceBuilder.fetchSource(new String[] {"requestUri", "requestHeader", "requestBody", "responseHeader", "responseBody"}, new String[]{});
// 向搜索请求对象中设置搜索源
searchRequest.source(searchSourceBuilder);
// 执行搜索,向ES发起http请求
SearchResponse searchResponse = client.search(searchRequest, ElasticsearchConfig.COMMON_OPTIONS);
// 搜索结果
SearchHits hits = searchResponse.getHits();
// 匹配到的总记录数
TotalHits totalHits = hits.getTotalHits();
// 得到匹配度高的文档
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit: searchHits) {
// 文档的主键
String id = hit.getId();
// 源文档内容
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
System.out.println(sourceAsMap);
}
}
按照时间区间查询索引数据文章来源地址https://www.toymoban.com/news/detail-513392.html
public static void matchTimeRangeQuery() throws Exception {
ElasticsearchConfig elasticSearchConfig = new ElasticsearchConfig();
RestHighLevelClient client = elasticSearchConfig.esRestClient();
// 搜索请求对象
SearchRequest searchRequest = new SearchRequest("shenyu-access-logging");
// 指定类型
searchRequest.types("_doc");
// 搜索源构建对象
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 搜索方式
// 按时间区间查询,设置查询数据条数为20。
searchSourceBuilder.query(QueryBuilders.rangeQuery("timeLocal.keyword").from("2022-11-02 15:21:22.794").to("2022-11-02 16:22:33.794"));
searchSourceBuilder.size(20);
// 设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
searchSourceBuilder.fetchSource(new String[] {"timeLocal", "requestUri", "requestHeader", "requestBody", "responseHeader", "responseBody"}, new String[]{});
// 向搜索请求对象中设置搜索源
searchRequest.source(searchSourceBuilder);
// 执行搜索,向ES发起http请求
SearchResponse searchResponse = client.search(searchRequest, ElasticsearchConfig.COMMON_OPTIONS);
// 搜索结果
SearchHits hits = searchResponse.getHits();
// 匹配到的总记录数
TotalHits totalHits = hits.getTotalHits();
// 得到匹配度高的文档
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit: searchHits) {
// 文档的主键
String id = hit.getId();
// 源文档内容
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
System.out.println(sourceAsMap);
}
}
到了这里,关于Elasticsearch 查询全部数据,按照时间区间查询数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!