全文搜索引擎 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 ...
随机推荐
- linux就该这么学,第十一天了
今天讲了,网卡绑,定,两块网卡同时工作,自动备源,理论上速度提升一倍,工作中可以用到的技术 还有sshd服务,端口22,远程连接使用,还可以设置root是否可以直接登录,主要配置文件在,/etc/ss ...
- small_trick_on_IT/PC
1.浏览器下ctrl+F可实现文本查找 其余还有 2.将软件目录放到环境变量Path下,Ctrl+R输入.exe软件名即可调用. (tips:可把常用软件建立快捷方式,统一放在某一目录下噢!)
- 一句话shell【php】
1.mysql执行语句拿shell Create TABLE a (cmd text NOT NULL); Insert INTO a (cmd) VALUES('<?php @eval($_P ...
- php5.6 phpmystudy 版本出问题
No input file specified的解决方法 https://jingyan.baidu.com/article/f7ff0bfccce11c2e26bb1381.html
- spring用注解配置,不用XML
//首先装载一个配置类AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyCon ...
- js--sort()排序方法的使用--(笔记)
情况1: var arr = [ 'c', 'd', 'a', 'e' ]; //都是字母的情况arr.sort();//alert( arr ); ...
- spring中aop事务
一.事务 二.spring封装了事务管理代码 1.事务操作 2.事务操作对象 (1)因为在不同平台,操作事务的代码各不相同.spring提供了一个接口 (2) PlatformTransactionM ...
- ABP框架系列之三十三:(Module-System-模块系统)
Introduction ASP.NET Boilerplate provides an infrastructure to build modules and compose them to cre ...
- 虚拟机 django 端口无法连接
我的虚拟机django服务器为192.168.27.100,使用启动命令python manage.py runserver 9001启动后,发现笔记本电脑的游览器无法连接 python@qinhan ...
- javascript 取整,取余数 math方法
1.丢弃小数部分,保留整数部分 parseInt() 函数可解析一个字符串,并返回一个整数. parseInt(string, radix) 参数 描述 string 必需.要被解析的字符串. rad ...