Elasticsearch
为什么要使用ES
数据库查询有缺点?
数据越大, 查询效率越低;(数据库的解决方案是建立索引, 但是使用前模糊查询,会导致索引失效)
ES可以解决该问题: 解决数据库中数据量过大同时模糊查询会导致数据库索引失效,查询效率低的问题.123
相关概念
索引: (存储数据的位置)
* 可以看作关系型数据库中的表;
映射: (存储吗? 存储的类型是什么? 是否分词? 采用的分词器是什么? )
* 数据如何存储在索引上(为建索引和搜索准备输入文本)
* 映射mapping (数据如何存放到索引对象上, 需要有一个映射配置, 数据类型, 是否存储, 是否分词.)
文档: (存储的数据)
* 就相当于表中的数据
文档类型: (指定存储文档的类型)
* 文档是可以有多种类型的
分词器:
* IK分词器, 庖丁分词器
词条: (搜索)123456789101112
Elasticsearch相关Query对象
* matchAllQuery: 查询索引库中所有的数据;
* termQuery: 词条查询,词条的完全匹配;
* wildcardQuery: 模糊查询(* ? 通配符);
* queryStringQuery: 将查询条件分词后根据词条匹配 默认返回的并集;
* boolQuery: must(and), should(or), must_not(not);12345
spring整合Elasticsearch
1. 导入相关jar包;1
<!-- 2. 配置applicationContext.xml -->
<!-- 让spring能够扫描到对应ES的dao接口, 让spring去自动创建该包下所有接口的代理类 -->
<elasticsearch:repositories base-package="cn.orange.dao"/>
<!-- 项目连接的ES服务器的地址和端口 -->
<elasticsearch:transport-client id="client" cluster-nodes="localhost:9300" />
<!-- 配置模板, 该id名称不能修改 -->
<!-- spring data elasticsearch DAO 必须依赖 elasticsearchTemplate -->
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="client"/>
</bean>12345678910
3.实体类需要添加注解
/**
* indexName: 索引名称
* type: 文档的类型
* @Id: 索引库的id
* index: 是否进行分词 analyzed:分词 not_analyzed:不分词 no:不根据此字段进行检索
* store: 是否存储
* analyzer: 指定使用的分词器
* type: 存储数据的类型
* searchAnalyzer: queryStringQuery就是查询条件进行分词的分词器.
*/
@Document(indexName = "blog3", type = "article")
public class Aticle(){
@Id
@Field(index = FieldIndex.not_analyzed, store = true, type = FieldType.Integer)
private Integer id;
@Field(index = FieldIndex.analyzed, analyzer = "ik", store = true, searchAnalyzer = "ik", type = FieldType.String)
private String title;
@Field(index = FieldIndex.analyzed, analyzer = "ik", store = true, searchAnalyzer = "ik", type = FieldType.String)
private String content;
}
有条件查询
must 条件必须成立 相当于and
must not 条件必须不成立 相当于no
should 条件可以不成立 相当于or