我们用到搜索引擎是因为模糊查询会使索引失效,而数据量很大的话,查询速度很慢;我们才会用到搜索引擎;
使用
1.导2个依赖
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
2.applicationContext-elasticsearch.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">
<!-- 搜索DAO 扫描 -->
<elasticsearch:repositories base-package="cn.itcast.bos.index" />
<!-- 配置Client -->
<elasticsearch:transport-client id="client" cluster-nodes="127.0.0.1:9300"/>
<!-- 配置搜索模板 -->
<bean id="elasticsearchTemplate"
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client" />
</bean>
</beans>
3.在实体类上添加注解:这些注解用来创建 索引库和映射关系的;
@Entity
@Table(name = "T_WAY_BILL")
@Document(indexName = "bos", type = "waybill")
public class WayBill implements Serializable {
@Id
@GeneratedValue
@Column(name = "C_ID")
@org.springframework.data.annotation.Id
@Field(index = FieldIndex.not_analyzed, store = true, type = FieldType.Integer)
private Integer id;
@Column(name = "C_WAY_BILL_NUM", unique = true)
@Field(index = FieldIndex.not_analyzed, store = true, type = FieldType.String)
private String wayBillNum; // 运单编号
4.编写WayBillIndexRepository:这上面没有 注解;
public interface WayBillIndexRepository extends
ElasticsearchRepository<WayBill, Integer> {
public List<WayBill> findBySendAddress(String sendAddress);
}
5.注入WayBillIndexRepository,来对索引库来增删该查;