首先,必须吐槽一下,springboot这个elasticsearch包对于elasticsearch的支持十分不友好,目前只支持很低版本的elasticsearch,如果有哪位大牛知道如何兼容更高版本的elasticsearch,欢迎给我留言。
es下载地址:https://www.elastic.co/downloads/elasticsearch
我测试的es版本是2.4.5,spring-boot-starter-data-elasticsearch不支持太高版本的elasticsearch。
另外,我是在windows下安装的,在官网下载好zip包后,到bin文件夹下,运行elasticsearch.bat,测试:http://localhost:9200/,能正确显示版本就可以了。
安装好后,我们在pom.xml文件添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> <version>1.5.10.RELEASE</version> </dependency>
在properties文件中添加es设置:
spring.data.elasticsearch.cluster-name=elasticsearch es默认
spring.data.elasticsearch.cluster-nodes= 127.0.0.1:9300 es默认
实体类:
@Document(indexName = "monitoralarm", type = "monitorAlarms")//加上了@Document注解之后,默认情况下这个实体中所有的属性都会被建立索引、并且分词 public class MonitorAlarmVO implements Serializable { @Id private Long id; private String uniqueKey; private Long monitoryPointId; private int modelDivisionType; private Long groupId; private int type; private int status; private String serverHost; private String data; private int picWidth; private int picHeight; private String objects; private Date alarmTime; private Date createTime; private boolean isRemove; private Date removeTime; }
接口:
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; public interface MonitorAlarmRepository extends ElasticsearchRepository<MonitorAlarmVO, Long> { //Page<MonitorAlarmVO> findByGroupId(Long groupId); }
controller:
public class MonitorAlarmController { @Autowired private MonitorAlarmRepository monitorAlarmRepository; //============================elasticsearch /** * 1、根据id查 * @param id * @return */ @GetMapping("/esById/{id}") public MonitorAlarmVO getBookById(@PathVariable Long id) { return monitorAlarmRepository.findOne(id); } /** * 2、查 ++:全文检索(根据整个实体的所有属性,可能结果为0个) * @param key * @return */ @GetMapping("/esByKey/{key}") public List<MonitorAlarmVO> testSearch(@PathVariable String key) { QueryStringQueryBuilder builder = new QueryStringQueryBuilder(key); Iterable<MonitorAlarmVO> searchResult = monitorAlarmRepository.search(builder); Iterator<MonitorAlarmVO> iterator = searchResult.iterator(); List<MonitorAlarmVO> list = new ArrayList<MonitorAlarmVO>(); while (iterator.hasNext()) { list.add(iterator.next()); } return list; } /** * 3、查 +++:分页、分数、分域(结果一个也不少) * @param page * @param size * @param q * @return * @return */ @GetMapping("/{page}/{size}/{key}") public List<MonitorAlarmVO> searchCity(@PathVariable Integer page, @PathVariable Integer size, @PathVariable String key) { // 分页参数 Pageable pageable = new PageRequest(page, size); // 分数,并自动按分排序 FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery() .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("name", key)), ScoreFunctionBuilders.weightFactorFunction(1000)) // 权重:name 1000分 .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("message", key)), ScoreFunctionBuilders.weightFactorFunction(100)); // 权重:message 100分 // 分数、分页 SearchQuery searchQuery = new NativeSearchQueryBuilder().withPageable(pageable) .withQuery(functionScoreQueryBuilder).build(); Page<MonitorAlarmVO> searchPageResults = monitorAlarmRepository.search(searchQuery); return searchPageResults.getContent(); } /** * 4、增 * @param monitorAlarmVO * @return */ @PostMapping("/esInsert") public MonitorAlarmVO insertBook(MonitorAlarmVO monitorAlarmVO) { monitorAlarmRepository.save(monitorAlarmVO); return monitorAlarmVO; } /** * 5、删 id * @param id * @return */ @DeleteMapping("/esDelete/{id}") public MonitorAlarmVO insertBook(@PathVariable Long id) { MonitorAlarmVO monitorAlarmVO = monitorAlarmRepository.findOne(id); monitorAlarmRepository.delete(id); return monitorAlarmVO; } /** * 6、改 * @param monitorAlarmVO * @return */ @PutMapping("/esUpdate") public MonitorAlarmVO updateBook(MonitorAlarmVO monitorAlarmVO) { monitorAlarmRepository.save(monitorAlarmVO); return monitorAlarmVO; } //============================elasticsearch }
好的,我们启动试试,报了一个错误,提示如下:
threw exception; nested exception is java.lang.NoSuchMethodError: org.jboss.netty.channel.socket.nio.NioWorker
这个是因为我的项目里的dubbo/zookeeper的netty包和es的netty包冲突了导致启动不了,解决方法是排除其中一个的netty包,我这里选择排除dubbo和zookeeper的netty包试试,但是后面发现排除了netty包会导致dubbo出错,有两全其美的办法麻烦给我留言下。
如下:
<dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <exclusions> <exclusion> <artifactId>netty</artifactId> <groupId>org.jboss.netty</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>io.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency>
最后,我们可以启动项目,访问试试了。