ElasticSearch简写ES,ES是一个高扩展、开源的全文检索和分析引擎,它能够准实时地快速存储、搜索、分析海量的数据。html
应用场景java
Elastic 须要 Java 8 环境。若是你的机器还没安装 Java
,能够参考JAVA安装node
安装完Java环境后,咱们能够开始如下ElasticSearch
安装或者根据官方文档安装git
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.zip unzip elasticsearch-5.5.1.zip cd elasticsearch-5.5.1/
进入解压目录以后,运行下面命令,启动ElasticSearch
github
./bin/elasticsearch
shell
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
打开: elasticsearch-5.5.1/config/jvm.options
数据库
在末尾添加:json
-XX:-AssumeMP
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000085330000, 2060255232, 0) failed; error='Cannot allocate memory' (errno=12)
先执行:bootstrap
sysctl -w vm.max_map_count=262144
再打开elasticsearch-5.5.1/config/jvm.options
数组
-Xmx512m -Xms512m
[2019-06-27T15:01:43,165][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
缘由:elasticsearch自5版本以后,处于安全考虑,不容许使用root用户运行。
解决:建立一个普通用户,将elasticsearch 安装目录权限修改一下,切换至普通用户运行elasticsearch就能够了
useradd elk chown -R elk.elk /usr/local/share/applications/elasticsearch-5.5.1 su - elk cd /usr/local/share/applications/elasticsearch-5.5.1
./bin/elasticsearch
若是一切正常,Elastic
就会在默认的9200
端口运行。这时,打开另外一个命令行窗口,请求该端口,会获得说明信息。
$ curl 'localhost:9200'
{ "name" : "cWyaT72", "cluster_name" : "elasticsearch", "cluster_uuid" : "A7akNm1SRw2Gm-BdSBkdaw", "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" }
Elastic
默认状况下,只容许本地访问,若是须要远程访问,能够修改 config/elasticsearch.yml
文件,去掉network.host
的注释,将它的值改为0.0.0.0
,而后从新启动 Elastic。
network.host: 0.0.0.0
上面代码中,设成0.0.0.0
让任何人均可以访问。线上服务不要这样设置,要设成具体的 IP。
Elastic
本质上是一个分布式数据库,容许多台服务器协同工做,每台服务器能够运行多个 Elastic
实例。
单个 Elastic
实例称为一个节点(node)
。一组节点构成一个集群(cluster)
。
curl -X GET 'http://localhost:9200/_cat/health?v'
curl -X GET 'http://localhost:9200/_cat/nodes?v'
Elastic
会索引全部字段,通过处理后写入一个反向索引(Inverted Index)。查找数据的时候,直接查找该索引。(一个 Index
相似于传统关系数据库中的一个 数据库
,是一个存储关系型文档的地方)。
因此,Elastic 数据管理的顶层单位就叫作 Index(索引)。它是单个数据库的同义词。每一个 Index (即数据库)的名字必须是小写。
下面的命令能够查看当前节点的全部 Index。
curl -X GET 'http://localhost:9200/_cat/indices?v'
Index里的单条记录称为Document
,多条Document
构成一个Index
.
Document
使用JSON格式表示,如:
{ "goods_name": "空调", "category_name": "家电分类", "price": "3999.00" }
同一个 Index 里面的 Document
,不要求有相同的结构(scheme),可是最好保持相同,这样有利于提升搜索效率。
Document
是能够分组的,如goods_list
这个Index
,能够按照category(家电、衣服)
分类,也能够按照price(>1000、 <1000)
分类。这种分组叫Type
它是虚拟的逻辑分组,用于过滤Document
。
列出每一个Index
下面的Type
curl 'http://localhost:9200/_mapping?pretty=true'
根据规划,Elastic 6.x 版只容许每一个 Index 包含一个 Type,7.x 版将会完全移除 Type。
新建 Index
,能够直接向 Elastic
服务器发出 PUT 请求。下面的例子是新建一个名叫goods_list
的 Index
。
curl -X PUT 'http://localhost:9200/goods_list'
服务器返回一个 JSON 对象,里面的acknowledged
字段表示操做成功。
{ "acknowledged": true, "shards_acknowledged": true }
curl -X DELETE 'http://localhost:9200/goods_list'
{ "acknowledged": true }
上面介绍了Index
和Type
的一些基本的概念和Index
的基本操做,如今先来建立一个完整的Index
结构,并对数据进行操做。
curl -X PUT 'localhost:9200/goods_list' -d ' { "mappings": { "goods_info": { "properties": { "goods_name": { "type": "keyword" }, "category_name": { "type": "keyword" }, "price": { "type": "float" } } } } } ' { "acknowledged": true }
执行上面命名,从新建立一个新的Index
向指定的 /Index/Type
发送 PUT
请求,就能够在 Index
里面新增一条记录。好比,向/goods_list/goods_info
发送请求,就能够新增一条商品记录。
curl -X PUT 'localhost:9200/goods_list/goods_info/1' -d ' { "goods_name": "华为笔记本", "category_name": "计算机", "price": "1000" }'
服务器返回的 JSON 对象,会给出 Index、Type、Id、Version 等信息:
{ "_index": "goods_list", "_type": "goods_info", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true }
相信细心的你会发现/goods_list/goods_info/1
,后面多了一个1
,这个1
是该条记录的 ID。能够是任意字符串
新增记录的时候,也能够不指定 Id,这时要改为 POST 请求。
curl -X POST 'localhost:9200/goods_list/goods_info' -d ' { "goods_name": "洗衣机", "category_name": "家电", "price": "899.99" }'
若是没有指定ID
,那么Elastic
会随机生成一串字符串做为ID
{ "_index": "goods_list", "_type": "goods_info", "_id": "AWub5f7FFq1D5epJJhqT", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true }
curl 'localhost:9200/goods_list/goods_info/1?pretty=true'
上面代码请求查看/goods_list/goods_info/1
这条记录,URL 的参数pretty=true
表示以易读的格式返回。
返回的数据中,found
字段表示查询成功,_source
字段返回原始记录:
{ "_index" : "goods_list", "_type" : "goods_info", "_id" : "1", "_version" : 1, "found" : true, "_source" : { "goods_name" : "华为笔记本", "category_name" : "计算机", "price" : "1000" } }
若是 ID
不正确,就查不到数据,found
字段就是false
。
curl 'localhost:9200/goods_list/goods_info/2?pretty=true'
ID=2
并不存在,因此会返回如下结果:
{ "_index" : "goods_list", "_type" : "goods_info", "_id" : "2", "found" : false }
curl -X DELETE 'localhost:9200/goods_list/goods_info/1'
PS:这里先不要删除这条记录,后面还要用到。
curl -X PUT 'localhost:9200/goods_list/goods_info/1' -d ' { "user" : "华为笔记本", "title" : "计算机", "desc" : "5000" }'
更新记录就是使用 PUT 请求,从新发送一次数据。
{ "_index": "goods_list", "_type": "goods_info", "_id": "1", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": false }
返回结果里面,有几个字段发生了变化:
"_version" : 2, "result" : "updated", "created" : false
curl 'localhost:9200/goods_list/goods_info/_search'
{ "took": 127, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "goods_list", "_type": "goods_info", "_id": "AWub5f7FFq1D5epJJhqT", "_score": 1, "_source": { "goods_name": "洗衣机", "category_name": "家电", "price": "899.99" } }, { "_index": "goods_list", "_type": "goods_info", "_id": "1", "_score": 1, "_source": { "user": "华为笔记本", "title": "计算机", "desc": "5000" } } ] } }
上面代码中,返回结果的 took
字段表示该操做的耗时(单位为毫秒),timed_out
字段表示是否超时,hits
字段表示命中的记录,里面子字段的含义以下:
total
:返回记录数,本例是2条。max_score
:最高的匹配程度,本例是1.0
。hits
:返回的记录组成的数组。
返回的记录中,每条记录都有一个_score
字段,表示匹配的程序,默认是按照这个字段降序排列。
这里主要介绍了Elastic
的安装、基本概念以及数据的基本操做,在下一章带来Elastic
的分词和全文搜索以及相关的技术点。
https://www.elastic.co/guide/...
http://www.ruanyifeng.com/blo...