Java查询ES某个时间段或者范围的数据
1.引⼊SpringData相关Esjar包
附上pom⽂件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" <modelVersion>4.0.0</modelVersion>
<groupId>com.leyou.demo</groupId>
<artifactId>elasticsearch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>elasticsearch</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
springboot中文</project>
2.编写l⽂件(ES配置,xml同理)
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 127.0.0.1:9300
3.创建与ES中所需数据对应的实体类(字段需对应,顺序应该⽆所谓不确定)@Data
@Document(indexName = "这⾥是索引名称",type = "这⾥是⽂档名称")
public class Item {
@Id
private String Id;
private String name;
private Long time;
}
PS:
可以通过实体类创建索引
4.创建Dao
@Component
public interface ESDao extends ElasticSearchRepository<;实体类,类型>{}
5.Service
@Service
public class EService {
@AutoWired
private ESDao esd;
public void searchByTime(){
BoolQueryBuilder bb = boolQuery();
bb.must(rangeQuery(时间的字段名).gt(开始时间).lt(结束时间));
SearchQuery search = new NativeSearchQueryBuilder()
.withQuery(bb)
.withPageable(PageRequest.of(取数据的第⼏条开始,第⼏条结束)).build(); Page<;实体类> result = esd.search(search);
NumberOfElements())
}
}
办公机思路⼿打,仅供参考,尽信书不如⽆书
办公机没办法截代码,附上原博客链接Java操作ES增删改查
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论