全文搜索引擎 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 ...
随机推荐
- rsyslog和logrotate
简介 rsyslog 是一个 syslogd 的多线程增强版. 现在Fedora和Ubuntu, rhel6默认的日志系统都是rsyslog了 rsyslog负责写入日志, logrotate负责备份 ...
- centos7切换图像界面和dos界面
在图形界面使用 ctrl+alt+F2切换到dos界面 dos界面 ctrl+alt+F2切换回图形界面 在命令上 输入 init 3 命令 切换到dos界面 输入 init 5命令 切换到图形界面 ...
- 简易数据库实现 UNIX环境高级编程(APUE)第二十章 A Database Library
将课程的源代码 使用C++写了一部分 LINUX WINDOW均可运行 #ifndef MYDB_H #define MYDB_H #include <iostream> #include ...
- java多线程系列11 juc包下的队列
队列分为两类 阻塞队列 BlockingQueue提供如下两个支持阻塞的方法: (1)put(E e): 尝试把e元素放如BlockingQueue中,如果该队列的元素已满,则阻塞该线程. ...
- [C#]GetFloat提示"指定的转换无效"
数据库中没有double型,float就表示double值.sql server数据库字段类型与.net的数据类型的对应关系: real(数据库)<--> float(.NET)float ...
- ScheduledExecutorService--目前最理想的定时任务实现方式
ScheduledExecutorService 理想的定时任务实现方式 : 通过线程池的方式来执行任务的 可以灵活的设定第一次执行任务延迟时间 提供了良好的约定,以便设定定时执行的间隔时间代码实现: ...
- #10072. 「一本通 3.2 例 1」Sightseeing Trip(floyd求最小环+路径)
https://loj.ac/problem/10072 针对无向图 因为Floyd是按照结点的顺序更新最短路的,所以我们在更新最短路之前先找到一个连接点k,当前的点k肯定不存在于已存在的最短路f[i ...
- 【转】linux 查看进程启动路径
在linux下查看进程大家都会想到用 ps -ef|grep XXX可是看到的不是全路径,怎么看全路径呢?每个进程启动之后在 /proc下面有一个于pid对应的路径例如:ps -ef|grep pyt ...
- ios Block详解
一. iOS代码块Block 1.1 概述 代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实现匿名函数的特性,Block是一种特殊的数据类型,其可以正常定义变量.作为参数.作为返回值, ...
- ABP框架系列之九:(Abp-Session-会话)
Introduction ASP.NET Boilerplate provides IAbpSession interface to obtain current user and tenant wi ...