elasticsearch搜索服务器常用于处理数据量较大的搜索应用
index索引 文档存储的地方 相当于数据库
type 文档类型 相当于类
分词查询 ik分词器的使用
下载地址 https://github.com/medcl/elasticsearch-analysis-ik/tree/2.x 详见bos13
spring整合elasticsearch
1 <!-- 导入elasticsearch依赖-->
<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 在dao实现 ElasticsearchRepository<WayBill,Integer> 用于保存实体类到elasticsearch
为类添加注解
@Document(indexName="bos",type="wayBill")
public class WayBill
@Field(index=FieldIndex.not_analyzed,store=true,type=FieldType.String)
@Field(index=FieldIndex.analyzed,analyzer="ik",searchAnalyzer="ik",type=FieldType.String)
4 在applicationContext.xml中配置
扫描dao包 <elasticsearch:repositories base-package="cn.itcast.bos.dao.index"></elasticsearch:repositories>
配置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" ></constructor-arg>
</bean>
<constructor-arg name="client" ref="client" ></constructor-arg>
</bean>
5 具体实现
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); //布尔条件查询 多条件组合查询
//构建条件查询
QueryBuilder queryBuilder = new TermQueryBuilder("wayBillNum", waybill.getWayBillNum());
//添加条件
boolQueryBuilder.must(queryBuilder);
详细信息请查阅相关文档