<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>demoname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.0.M3version>
<relativePath />
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<elasticsearch.version>5.5.0elasticsearch.version>
<elasticsearch.client.version>5.5.1elasticsearch.client.version>
<spring.data.elasticsearch.version>3.0.0.RC2spring.data.elasticsearch.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-elasticsearchartifactId>
dependency>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>transportartifactId>
<version>${elasticsearch.client.version}version>
dependency>
<dependency>
<groupId>org.elasticsearchgroupId>
<artifactId>elasticsearchartifactId>
<version>${elasticsearch.version}version>
dependency>
<dependency>
<groupId>com.google.collectionsgroupId>
<artifactId>google-collectionsartifactId>
<version>1.0version>
dependency>
<dependency>
<groupId>joda-timegroupId>
<artifactId>joda-timeartifactId>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-to-slf4jartifactId>
<version>2.8.2version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
<repositories>
<repository>
<id>spring-snapshotsid>
<name>Spring Snapshotsname>
<url>https://repo.spring.io/snapshoturl>
<snapshots>
<enabled>trueenabled>
snapshots>
repository>
<repository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>https://repo.spring.io/milestoneurl>
<snapshots>
<enabled>falseenabled>
snapshots>
repository>
<repository>
<id>spring-libs-snapshotid>
<name>Spring Snapshot Repositoryname>
<url>http://repo.spring.io/libs-snapshoturl>
repository>
repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshotsid>
<name>Spring Snapshotsname>
<url>https://repo.spring.io/snapshoturl>
<snapshots>
<enabled>trueenabled>
snapshots>
pluginRepository>
<pluginRepository>
<id>spring-milestonesid>
<name>Spring Milestonesname>
<url>https://repo.spring.io/milestoneurl>
<snapshots>
<enabled>falseenabled>
snapshots>
pluginRepository>
pluginRepositories>
project>
代码结构如下
com.example.demo.model.SipBean
com.example.demo.dao.SipRepository
com.example.demo.service.SipService
com.example.demo.service.impl.SipServiceImpl
com.example.demo.action.SipController
com.example.demo.DemoApplication
自己定义的类
package com.example.demo.model;
import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "t_sip", type = "sip", shards = 1, replicas = 0, refreshInterval = "-1")
public class SipBean {
@Id
private Integer id;
private Integer actiontype;
private Integer areaid;
private String assetid;
private Integer bandwidth;
private String c1url;
private String destination;
private String dstip;
private Integer dstport;
private String frequency;
private String groupcode;
private String programno;
private String purchasetoken;
private String qamname;
private String requestheader;
private String responseheader;
private Integer responsetime;
private Integer scale;
private String sessionid;
private String sn;
private String source;
private String srcip;
private Integer srcport;
private Integer statuscode;
private String statusmsg;
private Date time;
private String url;
public SipBean() {
}
public SipBean(Integer id, Integer actiontype, Integer areaid, String assetid, Integer bandwidth, String c1url,
String destination, String dstip, Integer dstport, String frequency, String groupcode, String programno,
String purchasetoken, String qamname, String requestheader, String responseheader, Integer responsetime,
Integer scale, String sessionid, String sn, String source, String srcip, Integer srcport,
Integer statuscode, String statusmsg, Date time, String url) {
this.id = id;
this.actiontype = actiontype;
this.areaid = areaid;
this.assetid = assetid;
this.bandwidth = bandwidth;
this.c1url = c1url;
this.destination = destination;
this.dstip = dstip;
this.dstport = dstport;
this.frequency = frequency;
this.groupcode = groupcode;
this.programno = programno;
this.purchasetoken = purchasetoken;
this.qamname = qamname;
this.requestheader = requestheader;
this.responseheader = responseheader;
this.responsetime = responsetime;
this.scale = scale;
this.sessionid = sessionid;
this.sn = sn;
this.source = source;
this.srcip = srcip;
this.srcport = srcport;
this.statuscode = statuscode;
this.statusmsg = statusmsg;
this.time = time;
this.url = url;
}
.... getter setter ...
}
集成了ElasticsearchRepository类,里面已经实现了基本的方法,service层可以直接调用;
package com.example.demo.dao;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import com.example.demo.model.SipBean;
import java.util.List;
public interface SipRepository extends ElasticsearchRepository {
/**
*自己定义的,按照sn查询
*/
@Query("{\"bool\" : {\"must\" : {\"term\" : {\"sn\" : \"?0\"}}}}")
Page findBySn(String sn, Pageable pageable);
}
具体实现的业务代码
显示接口定义SipService
package com.example.demo.service;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.example.demo.model.SipBean;
public interface SipService {
public SipBean findSip(Integer id);
public List<SipBean> findSipList();
Page<SipBean> findBySn(String sn, Integer pageNumber, Integer pageSize);
Page<SipBean> findByFuzzyQuery(String field,String value, Integer pageNumber, Integer pageSize);
Page<SipBean> findByMutliQuery(String start,String end, String field,String value, Integer pageNumber, Integer pageSize);
Page<SipBean> findAll(Integer pageNumber, Integer pageSize) ;
}
下面是Service的具体实现类
package com.example.demo.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.example.demo.dao.SipRepository;
import com.example.demo.model.SipBean;
import com.example.demo.service.SipService;
import com.google.common.collect.Lists;
@Service
public class SipServiceImpl implements SipService {
@Autowired
private SipRepository sipRepository;
/**
* 按照id查詢
*/
@Override
public SipBean findSip(Integer id) {
Optional opt = sipRepository.findById(id);
return opt.get();
}
/**
*數據不多的時候,返回所有的記錄
*/
@Override
public List findSipList() {
Iterable elements = sipRepository.findAll();
List list = Lists.newArrayList(elements);
return list;
}
/**
* 分頁查詢,帶條件
*/
@Override
public Page findBySn(String sn, Integer pageNumber, Integer pageSize) {
// TODO Auto-generated method stub
QueryBuilder query = QueryBuilders.matchQuery("sn",sn);
Pageable pageable = PageRequest.of(pageNumber, pageSize);
return sipRepository.search(query, pageable);
}
/**
* 分頁查詢
*/
@Override
public Page findAll(Integer pageNumber, Integer pageSize) {
Pageable pageable = PageRequest.of(pageNumber, pageSize);
return sipRepository.findAll(pageable);
}
/**
* 分頁。模糊查詢
*/
@Override
public Page findByFuzzyQuery(String field,String value, Integer pageNumber, Integer pageSize) {
QueryBuilder query = QueryBuilders.fuzzyQuery(field, value);
Pageable pageable = PageRequest.of(pageNumber, pageSize);
return sipRepository.search(query, pageable);
}
/**
* 分頁,時間,排序查詢
*/
@Override
public Page findByMutliQuery(String start, String end, String field, String value, Integer pageNumber,
Integer pageSize) {
DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
long istart = format.parseDateTime(start).getMillis();
long iend = format.parseDateTime(end).getMillis();
BoolQueryBuilder query = QueryBuilders.boolQuery()
.must(QueryBuilders.rangeQuery("time").lte(iend).gte(istart));
if(field!=null && !"".equals(field)){
query.must(QueryBuilders.fuzzyQuery(field, value));
}
Pageable pageable = PageRequest.of(pageNumber, pageSize,Sort.by("sn"));
//Pageable pageable2 = PageRequest.of(pageNumber, pageSize,Direction.ASC,"sn","time");
return sipRepository.search(query, pageable);
}
}
界面调用的代码
package com.example.demo.action;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.SipBean;
@RestController
@RequestMapping(value = "/sip")
public class SipController {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private com.example.demo.service.SipService sipService;
/**
* 按照ID查詢
* @param id
* @return
*/
@RequestMapping(value = "/getSipById")
@ResponseBody
public Object getSipById(Integer id){
SipBean sip =sipService.findSip(id);
return sip;
}
@RequestMapping(value = "/getSipList")
@ResponseBody
public Object getSipList(){
List sips =sipService.findSipList();
return sips;
}
@RequestMapping(value = "/getSipByPage")
@ResponseBody
public Page getSipByPage(Integer pageNumber, Integer pageSize) {
Page sips =sipService.findAll(pageNumber, pageSize);
return sips;
}
@RequestMapping(value = "/getSipBySn")
@ResponseBody
public Page getSipBySn(String sn,Integer pageNumber, Integer pageSize) {
Page sips =sipService.findBySn(sn, pageNumber, pageSize);
return sips;
}
@RequestMapping(value = "/getSipByFuzzyQuery")
@ResponseBody
public Page getSipByFuzzyQuery(String field,String value,Integer pageNumber, Integer pageSize) {
Page sips =sipService.findByFuzzyQuery(field, value, pageNumber, pageSize);
return sips;
}
@RequestMapping(value = "/findByMutliQuery")
@ResponseBody
public Page getMutliQuery(String start,String end,String field,String value,Integer pageNumber, Integer pageSize) {
Page sips =sipService.findByMutliQuery(start,end,field, value, pageNumber, pageSize);
return sips;
}
}
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
@SpringBootApplication
@EnableElasticsearchRepositories
@ComponentScan("com.example.demo")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
前提是你的Elasticsearch已经配置好,可以正常使用,集群或者单个都使用,稍微改动就可以了
logging.level.com.example=DEBUG
logging.level.org.springframework.data.elasticsearch=DEBUG
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false
spring.data.elasticsearch.cluster-name=es-application
spring.data.elasticsearch.cluster-nodes=172.17.5.223:9300,172.17.5.53:9300
spring.data.elasticsearch.local=false
spring.data.elasticsearch.repositories.enabled=true
到此,新版的es 和springboot代码已经配置完,后面就可以用写简单工具来测试。
我用的工具是postman,需要的同学可以导官网上下载,后台开发必备利器!
https://www.getpostman.com/postman