全文搜索引擎 Elasticsearch 入门
1. 百科
ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
2. 安装
依赖Java8,本文在Linux上运行
下载、解压
$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.zip
$ unzip elasticsearch-5.5..zip
$ cd elasticsearch-5.5./
启动
curl localhost:
返回
$curl localhost:9200
{
"name" : "Jt1bemT",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "I_R2FCz8SUypT80rkbLeKw",
"version" : {
"number" : "5.5.1",
"build_hash" : "19c13d0",
"build_date" : "2017-07-18T20:44:24.823Z",
"build_snapshot" : false,
"lucene_version" : "6.6.0"
},
"tagline" : "You Know, for Search"
}
默认是只能本地调用,设置远程访问:
修改config/elasticsearch.yml
network.host: 0.0.0.0
重启,还是访问不了,报错
ERROR: [1] bootstrap checks failed
[1]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
原因:这是在因为Centos6不支持SecComp,而ES5.2.0默认bootstrap.system_call_filter为true进行检测,所以导致检测失败,失败后直接导致ES不能启动
修改
在elasticsearch.yml中配置bootstrap.system_call_filter为false
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
启动,就可以通过:ip:port访问了
3. 基本概念
Node与Cluster
Elastic本质是一个分布式数据库,允许多台服务器协同工作,每台机器可以运行多个Elastic实例。
单个Elastic实例称为一个节点(Node),一组节点构成一个集群(Cluster)
Index
Elastic会索引所有的字段,经处理后写入一个反向索引。查找数据时,直接查找该索引。
所以Elastic数据管理的顶层单位交Index,它是单个数据库的同义词(注:每个索引必须是小写)
查看当前节点所有的Index
curl -X GET 'http://localhost:9200/_cat/indices?v'
Document
Index里面的记录称为Document,许多Document构成一个Index
Document采用的设计Json格式
同一个Index里的Document不要求有相同的结构,但是最好一样,这样利于提高搜索效率。
Type
Document分组,比如地体有1号线、2号线。不同的Type里应有相同的结构
可以通过以下命令查看一个Index中所有的Type
curl 'localhost:9200/_mapping?pretty=true'
存储结构和关系数据库对比
关系数据库 ⇒ 数据库 ⇒ 表 ⇒ 行 ⇒ 列(Columns) Elasticsearch ⇒ 索引(Index) ⇒ 类型(type) ⇒ 文档(Docments) ⇒ 字段(Fields)
4. 增加和删除Index
curl -X PUT 'localhost:9200/subway'
新建了一个叫subway的Index,返回
{"acknowledged":true,"shards_acknowledged":true}
删除叫subway的Index
curl -X DELETE 'localhost:9200/subway'
5. 中文分词设置
安装中文分词插件,如ik
$ ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.1/elasticsearch-analysis-ik-5.5.1.zip
重启Elastic,就可以加载安装的ik了。
下面新建一个Index,指定需要分词的字段。
$ curl -X PUT 'localhost:9200/accounts' -d '
{
"mappings": {
"person": {
"properties": {
"user": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
}
}'
上述代码新建了名叫accounts的Index,名叫person的Index,person里有3个字段
- user
- title
- desc
这3个都是中文,并且都是文本(text),所以需要指定中文分词,而不使用英文分词器
search_analyzer指定:ik_max_word,指明实用ik对文本进行最大数量的分词
6. 增删改
增
新增3条记录
curl -X PUT 'http://10.125.15.70:9200/accounts/person/zs' -d '{"user":"张三","title":"数据库工程师","desc":"数据库管理"}'
curl -X PUT 'http://10.125.15.70:9200/accounts/person/ls' -d '{"user":"李四","title":"运维工程师","desc":"系统维护"}'
curl -X PUT 'http://10.125.15.70:9200/accounts/person/ww' -d '{"user":"王五","title":"开发工程师","desc":"软件开发"}'
删
curl -X DELETE 'http://10.125.15.70:9200/accounts/person/zs'
改
curl -X PUT 'localhost:9200/accounts/person/ls' -d '{"user" : "李四2","title" : "工程师","desc" : "软件开发"}'
7. 查
返回所有记录
curl 'localhost:9200/accounts/person/_search'
结果
{
"took" : ,
"timed_out" : false,
"_shards" : {
"total" : ,
"successful" : ,
"failed" :
},
"hits" : {
"total" : ,
"max_score" : 1.0,
"hits" : [
{
"_index" : "accounts",
"_type" : "person",
"_id" : "ls",
"_score" : 1.0,
"_source" : {
"user" : "李四2",
"title" : "工程师",
"desc" : "软件开发"
}
},
{
"_index" : "accounts",
"_type" : "person",
"_id" : "ww",
"_score" : 1.0,
"_source" : {
"user" : "王五",
"title" : "开发工程师",
"desc" : "软件开发"
}
}
]
}
}
全文搜索
curl 'localhost:9200/accounts/person/_search' -d '{"query":{"match":{"title":"工程"}}}
结果
{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"failed":
},
"hits": {
"total": ,
"max_score": 0.5649868,
"hits": [{
"_index": "accounts",
"_type": "person",
"_id": "ww",
"_score": 0.5649868,
"_source": {
"user": "王五",
"title": "开发工程师",
"desc": "软件开发"
}
}, {
"_index": "accounts",
"_type": "person",
"_id": "ls",
"_score": 0.5063205,
"_source": {
"user": "李四2",
"title": "工程师",
"desc": "软件开发"
}
}]
}
}
逻辑运算
OR
curl 'localhost:9200/accounts/person/_search' -d '{"query" : { "match":{ "desc":"系统 开发" }}}'
AND
curl 'localhost:9200/accounts/person/_search' -d '"query": {"bool": {"must": [{ "match": { "desc": "软件" } }, { "match": { "desc": "开发" } }]}}}'
参考
全文搜索引擎 Elasticsearch 入门的更多相关文章
- 全文搜索引擎Elasticsearch入门实践
全文搜索引擎Elasticsearch入门实践 感谢阮一峰的网络日志全文搜索引擎 Elasticsearch 入门教程 安装 首先需要依赖Java环境.Elasticsearch官网https://w ...
- 全文搜索引擎 Elasticsearch 入门教程
全文搜索属于最常见的需求,开源的 Elasticsearch (以下简称 Elastic)是目前全文搜索引擎的首选. 它可以快速地储存.搜索和分析海量数据.维基百科.Stack Overflow.Gi ...
- 全文搜索引擎 Elasticsearch 入门:集群搭建
本文主要介绍什么是 ElasticSearch 以及为什么需要它,如何在本机安装部署 ElasticSearch 实例,同时会演示安装 ElasticSearch 插件,以及如何在本地部署多实例集群, ...
- Spring Boot 全文搜索引擎 ElasticSearch
参考 全文搜索引擎ElasticSearch 还是Solr? - JaJian - 博客园
- 3.高并发教程-基础篇-之分布式全文搜索引擎elasticsearch的搭建
高并发教程-基础篇-之分布式全文搜索引擎elasticsearch的搭建 如果大家看了我的上一篇<2.高并发教程-基础篇-之nginx+mysql实现负载均衡和读写分离>文章,如果能很好的 ...
- 全文搜索引擎 Elasticsearch 安装
全文搜索引擎 Elasticsearch 安装 学习了:http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html 拼音:https://www ...
- 搜索引擎ElasticSearch入门
前言 最近项目上需要用到搜索引擎,由于之前自己没有了解过,所以整理了一下搜索引擎的相关概念知识. 正文 想查数据就免不了搜索,搜索就离不开搜索引擎,百度.谷歌都是一个非常庞大复杂的搜索引擎,他们几乎索 ...
- 全文搜索引擎 ElasticSearch 还是 Solr?
最近项目组安排了一个任务,项目中用到了全文搜索,基于全文搜索 Solr,但是该 Solr 搜索云项目不稳定,经常查询不出来数据,需要手动全量同步,而且是其他团队在维护,依赖性太强,导致 Solr 服务 ...
- 分布式全文搜索引擎ElasticSearch
一 什么是 ElasticSearch Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elas ...
随机推荐
- 二 分析easyswoole源码(启动服务)
前文连接,阅读的时候最好参照EasySwoole2.1.2的源码 $inst->run();//启动服务 这里实际调用的是Core的start方法ServerManager::getInstan ...
- 代码之髓读后感——名字&作用域&类型
名字和作用域 为什么要取名 看着代码中遍地都是的变量,函数,或多或少的我们都应该想过,为什么会有这些名字呢? 我们知道,计算机将数据存储到对应的物理内存中去.我们的操作就是基于数据的.我们需要使用这些 ...
- predict predict_proba区别的小例子
predict_proba返回的是一个n行k列的数组,第i行第j列上的数值是模型预测第i个预测样本的标签为j的概率.所以每一行的和应该等于1. 举个例子 >>> from sklea ...
- 接口测试3A原则
手工的功能测试用例也可以用3A原则来编写. Arrange: 准备被测功能相关的测试数据,比如往系统里录入一批工单以便测试工单的分页功能 Act : 调用被测的功能,实际上这就是我们一直讲的测试步骤 ...
- js的事件委托机制
如今的JavaScript技术界里最火热的一项技术应该是‘事件委托(event delegation)’了.使用事件委托技术能让你避免对特定的每个节点添加事件监听器:相反,事件监听器是被添加到它们的父 ...
- linux python 安装到用户目录
在公司服务器中,python可能存在多个版本,而且python中的包也有多个不同版本,由于不同猿的需求不同,经常会引起程序冲突,影响工作效率.因此,给大家分享一个在没有root权限时,将python安 ...
- 渗透测试的理论部分2——OSSTMM的详细描述
昨天休息了一天,今天我要连更两篇博客,作为补充,以下为正文 本章详细描述了OSSTMM内的RAV得分这一理论概念,对日后从事正规安全工作至关重要 OSSTMM为开源安全测试方法论,对OSSTMM不了解 ...
- JAVA 8 主要新特性 ----------------(二)JDK1.8优点概括
一.JDK1.8优点概括 1.速度更快 由于底层结构和JVM的改变,使得JDK1.8的速度提高. 2.代码更少(增加了新的语法 Lambda 表达式) 增加新特性Lambda表达式的 ...
- [转]Ubuntu16.04下ralink rt3290驱动安装
出处:https://askubuntu.com/questions/253632/how-do-i-get-a-ralink-rt3290-wireless-card-working 解决为问题:L ...
- MFC源码解读(一)最原始一个MFC程序,手写不用向导
从这一篇开始,详细记录一下MFC的源码解读 四个文件,分别为: stdafx.h,stdafx.cpp,hello.h,hello.cpp 代码如下: //stdafx.h #include < ...