依赖包
compile 'org.springframework.boot:spring-boot-starter-web:2.0.4.RELEASE'
compile 'org.springframework.boot:spring-boot-starter-data-elasticsearch:2.0.4.RELEASE'
PS: maven依赖包按照格式自行修改
代码
model
package com.chz.medical.spider.medicine.model;
import org.springframework.data.elasticsearch.annotations.Document;
import java.io.Serializable;
/**
* 药品信息
* @Author: gongstring
* @Email: gongstring@foxmail.com
* @Time: 2018/8/12 上午11:12
* @Version: 1.0
*/
@Document(indexName = "medical",type = "medicine")
public class Medicine implements Serializable {
private Long id;
private String gyzz;
private String tmbh;
private String content;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getGyzz() {
return gyzz;
}
public void setGyzz(String gyzz) {
this.gyzz = gyzz;
}
public String getTmbh() {
return tmbh;
}
public void setTmbh(String tmbh) {
this.tmbh = tmbh;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
repository(数据操作Dao类)
package com.chz.medical.spider.medicine.repository;
import com.chz.medical.spider.medicine.model.Medicine;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* 药品库
* @Author: gongstring
* @Email: gongstring@foxmail.com
* @Time: 2018/8/12 上午11:18
* @Version: 1.0
*/
public interface MedicineRepository extends ElasticsearchRepository<Medicine,Long> {
/**
* 根据国药准字以及条码标号查询符合条件的药品
*
* @param gyzz
* @param tmbh
* @return
*/
Page<Medicine> findByGyzzAndTmbh(String gyzz, Integer tmbh,Pageable page);
/**
* 根据国药准字或条码标号查询符合条件的药品
*
* @param gyzz
* @param tmbh
* @return
*/
Page<Medicine> findByGyzzOrTmbh(String gyzz, Integer tmbh,Pageable page);
}
注意事项
1.启动异常
控制台日志:unsupported version: [5.5.0] minimal compatible version is: [5.6.0]
主要是因为添加了Repository实现类,依赖包版本不正确导致;
Elasticsearch Server日志文件异常如下:
[2018-08-12T15:24:11,433][WARN ][o.e.x.s.t.n.SecurityNetty4ServerTransport] [XHnI_Gv] exception caught on transport layer [NettyTcpChannel{localAddress=/127.0.0.1:9300, remoteAddress=/127.0.0.1:50157}], closing connection
java.lang.IllegalStateException: Received message from unsupported version: [5.5.0] minimal compatible version is: [5.6.0]
at org.elasticsearch.transport.TcpTransport.ensureVersionCompatibility(TcpTransport.java:1462) ~[elasticsearch-6.3.2.jar:6.3.2]
at org.elasticsearch.transport.TcpTransport.messageReceived(TcpTransport.java:1409) ~[elasticsearch-6.3.2.jar:6.3.2]
at org.elasticsearch.transport.netty4.Netty4MessageChannelHandler.channelRead(Netty4MessageChannelHandler.java:64) ~[transport-netty4-6.3.2.jar:6.3.2]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:310) [netty-codec-4.1.16.Final.jar:4.1.16.Final]
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:297) [netty-codec-4.1.16.Final.jar:4.1.16.Final]
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:413) [netty-codec-4.1.16.Final.jar:4.1.16.Final]
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:265) [netty-codec-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.handler.logging.LoggingHandler.channelRead(LoggingHandler.java:241) [netty-handler-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1359) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:935) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:134) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:645) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysPlain(NioEventLoop.java:545) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:499) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459) [netty-transport-4.1.16.Final.jar:4.1.16.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) [netty-common-4.1.16.Final.jar:4.1.16.Final]
at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]
解决方法:
从日志提示信息来看,Elasticsearch client的依赖包版本号,至少为5.6.0,说明Spring-data-elasticsearch默认依赖包版本过低,需要添加依赖包(最好与ES Server版本号一致,我的Server是6.3.2):
compile 'org.elasticsearch.client:rest:6.3.2'
compile 'org.elasticsearch.client:transport:6.3.2'