官网 :https://www.elastic.co/products/elasticsearch
目录结构: bin 存放 elasticSearch 运行命令
config 存放配置文件 lib 存放 elasticSearch 运行依赖 jar 包 modules 存放 elasticSearch 模块 plugins 存放插件 运行 elasticSearch/bin/elasticsearch.bat文件(需要配置JAVA_HOME环境变量)ElasticSearch 插件安装 es head
在cmd %elasticsearch%/bin/ 输入 plugin.bat install mobz/elasticsearch-head访问 http://localhost:9200/_plugin/head/ 查询安装是否成功ElasticSearch基础概念
索引:是Elasticsearch对逻辑 数据的逻辑储存,可以把它看成关系型数据库的表(存储数据的表结构 ,任何搜索数据,存在索引对象上)文档:储存在ElasticSearch中的主要实体,可以看成数据库表中的一行记录(一条数据记录, 存在索引对象上)文档类型:在ElasticSearch中,一个索引对象可以储存很多不用用途的对象(一个索引对象 存放多种类型数据, 数据用文档类型进行标识)映射:为建索引和搜索准备输入文本( 数据如何存放到索引对象上,需要有一个映射配置, 数据类型、是否存储、是否分词 …)IK分词器
下载后在本地进行解压缩,之后进入解压缩的目录运行mvn clean package命令进行编译。编译后在release目录中找到生成的elasticsearch-analysis-ik-1.10.0.zip压缩包。解压缩以后生成如下目录结构:
把这些文件拷贝到%elasticsearch-2.4.0%/plugins/analysis-ik目录下
然后target/releases/elasticsearch-analysis-ik-1.10.0/config下的文件:
拷贝到elasticsearch的config文件夹下就可以。
接下来修改配置文件elasticsearch.yml:
添加index.analysis.analyzer.ik.type: "ik"配置给ik解析器添加一个标示
下面测试一下,在浏览器中输入如下内容:
http://172.16.0.104:9200/_analyze?analyzer=ik&pretty=true&text=我是中国人
与springdata整合:
maven 导入坐标
导入 spring-test 和 junit 编写测试用例
Slf4j-log4j 日志包
在 src/main/resources 下建立 applicationContext.xml 和 log4j.properties
<?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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.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.dao" /> <!-- 扫描 @Server --> <context:component-scan base-package="cn.itcast.service"/> <!-- 配置elasticsearch 连接 --> <elasticsearch:transport-client id="client" cluster-nodes="localhost:9300"/> <!-- spring data elasticseach Dao 依赖 模板 --> <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> <constructor-arg name="client" ref="client" /> </bean></beans>
索引和映射如何创建 --- 基于 spring data elasticsearch 注解
在使用 spring data elasticsearch 开发, 需要将索引和映射信息 配置实体类上面@Document 文档对象 (索引信息、文档类型 )@Id 文档主键 唯一标识@Field 每个文档的字段配置(类型、是否分词、是否存储、分词器 )@Document(indexName="blog3",type="article")
public class Article { @Id @Field(index=FieldIndex.not_analyzed, store=true,type=FieldType.Integer) private Integer id; @Field(index=FieldIndex.analyzed,searchAnalyzer="ik",analyzer="ik",store=true, type=FieldType.String) private String title; @Field(index=FieldIndex.analyzed,searchAnalyzer="ik",analyzer="ik",store=true, type=FieldType.String) private String content;}测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")public class MyTest { @Autowired private ArticleService articleService; @Autowired private Client client; @Autowired private ElasticsearchTemplate elasticsearchTemplate; @Test public void demo(){ elasticsearchTemplate.createIndex(Article.class); elasticsearchTemplate.putMapping(Article.class); }}
搜索文档数据
查询数据 主要依赖 QueryBuilder 对象 ,可以通过 QueryBuilders 获取boolQuery() 布尔查询,可以用来组合多个查询条件fuzzyQuery() 相似度查询matchAllQuery() 查询所有数据regexpQuery() 正则表达式查询termQuery() 词条查询wildcardQuery() 模糊查询