springboot集成ElasticsearchRepository
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
添加配置
spring:
data:
elasticsearch:
cluster-nodes: 192.168.137.129:9300
repositories:
enabled: true
创建ApiLog
package com.example.demo;
/**
* Author : 尤毅鹤
* Date: 2020/04/07 15:19
* Description: <描述>
*/
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "apilog_index", type = "apilog")
public class ApiLog {
private static final long serialVersionUID = -382318121710813866L;
@Id
private long id;
private int uid;
private int app_id;
private String url;
private long c_time;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public int getApp_id() {
return app_id;
}
public void setApp_id(int app_id) {
this.app_id = app_id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public long getC_time() {
return c_time;
}
public void setC_time(long c_time) {
this.c_time = c_time;
}
public ApiLog() {
}
public ApiLog(long id, int uid, int app_id, String url, long c_time) {
this.id = id;
this.uid = uid;
this.app_id = app_id;
this.url = url;
this.c_time = c_time;
}
@Override
public String toString() {
return "ApiLog{" +
"id=" + id +
", uid=" + uid +
", app_id=" + app_id +
", url='" + url + '\'' +
", c_time=" + c_time +
'}';
}
}
ApiLogService
package com.example.demo;
import java.util.ArrayList;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class ApiLogService {
@Autowired
private ApiLogSearchRepository repository;
/**
* 删除文档
* @param id
* @return
*/
public boolean deleteApiLog(Long id) {
try {
repository.deleteById(id);
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
return true;
}
/**
* 插入文档
* @param aLog
* @return
*/
public ApiLog saveApiLog(ApiLog aLog) {
ApiLog apiLog = new ApiLog();
try {
apiLog = repository.save(aLog);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
return apiLog;
}
/**
* 按id查询
* @param id
* @return
*/
public ApiLog findOneApiLog(long id) {
ApiLog apiLog = new ApiLog();
try {
apiLog = repository.findById(id).get();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
return apiLog;
}
/**
* 查询全部文档
* @return
*/
public List<ApiLog> findAllApiLog() {
List<ApiLog> list = new ArrayList<>();
try {
Iterable<ApiLog> aIterable = repository.findAll();
for (ApiLog apiLog : aIterable) {
list.add(apiLog);
}
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
return list;
}
/**
* 按条件过滤查询
* @param b_time
* @param e_time
* @return
*/
public List<ApiLog> findApiLogByDate(String b_time,String e_time) {
List<ApiLog> list = new ArrayList<>();
try {
// 单个字符串
// QueryBuilder qb0 = QueryBuilders.termQuery("id", "0");
// 闭区间
QueryBuilder qb1 = QueryBuilders.rangeQuery("c_time").from(b_time).to(e_time);
// 大于
QueryBuilder qb2 = QueryBuilders.rangeQuery("uid").gt(0);
// 过滤多条件
QueryBuilder qb = QueryBuilders.boolQuery().must(qb1).must(qb2);
Iterable<ApiLog> aIterable = repository.search(qb);
for (ApiLog apiLog : aIterable) {
list.add(apiLog);
}
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
return list;
}
}
ApiLogSearchRepository
package com.example.demo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ApiLogSearchRepository extends ElasticsearchRepository<ApiLog, Long>{
}
测试
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)
class DemoApplicationTests {
@Autowired
ApiLogController apiLogController;
@Test
void contextLoads() {
ApiLog apiLog = apiLogController.saveApiLog(new ApiLog(System.currentTimeMillis(), 10, 30, "http://www.baidu.com", System.currentTimeMillis()));
System.out.println(apiLog);
}
}
查看