博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
elasticsearch快速入门
阅读量:7074 次
发布时间:2019-06-28

本文共 3629 字,大约阅读时间需要 12 分钟。

官网 :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() 模糊查询

转载于:https://www.cnblogs.com/richter/p/7337800.html

你可能感兴趣的文章
Exchange 2013 ServerComponent状态异常处理
查看>>
makefile 判断 64bit or 32 bit
查看>>
创建swap
查看>>
LNMP安装配置
查看>>
马哥2016全新Linux+Python高端运维班第八周作业
查看>>
kvm&pxe
查看>>
二分搜索法 Arrays.binarySearch遇到返回-1的问题
查看>>
环视 Lookaround ---- 正则
查看>>
MySQL事务
查看>>
定义一个空的C++类,编译器自动创建四类成员函数
查看>>
SEO基本概念入门
查看>>
代码实现 UITableView与UITableViewCell
查看>>
用python写的agent
查看>>
微信天气接口查询
查看>>
spring-前置通知
查看>>
Transient修饰符的使用
查看>>
shell特殊符号,cut、sort、wc、uniq、tee、tr、split命令
查看>>
运维面试题
查看>>
java 消息摘要算法 MAC
查看>>
2011.11.6
查看>>