Elastic 的底层是开源库 Lucene。但是,你没法直接用 Lucene,必须自己写代码去调用它的接口。Elastic 是 Lucene 的封装,提供了 REST API 的操作接口,开箱即用。
Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例。 单个 Elastic 实例称为一个节点(node)。一组节点构成一个集群(cluster)。
-
Index Elastic 会索引所有字段,经过处理后写入一个反向索引(Inverted Index)。查找数据的时候,直接查找该索引。 所以,Elastic 数据管理的顶层单位就叫做 Index(索引)。它是单个数据库的同义词。每个 Index (即数据库)的名字必须是小写。
-
Document Index 里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index。
-
Type Document 可以分组,比如weather这个 Index 里面,可以按城市分组(北京和上海),也可以按气候分组(晴天和雨天)。这种分组就叫做 Type,它是虚拟的逻辑分组,用来过滤 Document。
Elastic 6.x 版只允许每个 Index 包含一个 Type,7.x 版将会彻底移除 Type。
Elastic 需要 Java 8 环境。注意要保证环境变量JAVA_HOME正确设置。
安装完 Java,就可以跟着官方文档安装 Elastic。直接下载压缩包比较简单。
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.zip
unzip elasticsearch-5.5.1.zip
cd elasticsearch-5.5.1/
./bin/elasticsearch
报错max virtual memory areas vm.maxmapcount [65530] is too low
:
sudo sysctl -w vm.max_map_count=262144
如果一切正常,打开另一个命令行窗口,请求 9200 端口,会得到说明信息:
curl localhost:9200
{
"name" : "atntrTf",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "tf9250XhQ6ee4h7YI11anA",
"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
- _cat
- GET /_cat/indices?help
- GET /_cat/nodes: 查看所有节点
- GET/_cat/health:查看es健康状况
- GET /_cat/master:查看主节点
- GET /_cat/indice: 查看所有索引show databases;
下面的命令可以查看当前节点的所有 Index。
curl -X GET 'http://localhost:9200/_cat/indices?v'
下面的命令可以列出每个 Index 所包含的 Type。
curl 'localhost:9200/_mapping?pretty=true'
新建 Index,可以直接向 Elastic 服务器发出 PUT 请求。下面的例子是新建一个名叫weather的 Index。
curl -X PUT 'localhost:9200/weather'
curl -X DELETE 'localhost:9200/weather'
服务器返回一个 JSON 对象,里面的acknowledged字段表示操作成功。
{
"acknowledged":true,
"shards_acknowledged":true
}
然后,我们发出 DELETE 请求,删除这个 Index。
首先,安装中文分词插件。这里使用的是 ik (https://github.com/medcl/elasticsearch-analysis-ik),也可以考虑其他插件(比如 https://www.elastic.co/guide/en/elasticsearch/plugins/current/analysis-smartcn.html)。
sudo ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.1/elasticsearch-analysis-ik-5.5.1.zip
sudo ./bin/elasticsearch-plugin install analysis-smartcn
重新启动 Elastic,就会自动加载这个新安装的插件。
- 新建一个 Index,指定需要分词的字段
新建一个名称为
accounts
的Index
,里面有一个名称为person
的Type
有三个字段。这三个字段都是中文,而且类型都是文本(text),所以需要指定中文分词器,不能使用默认的英文分词器。analyzer
是字段文本的分词器,search_analyzer
是搜索词的分词器。ik_max_word
分词器是插件ik提供的,可以对文本进行最大数量的分词。
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"
}
}
}
}
}'
向指定的 /Index/Type 发送 PUT 请求,就可以在 Index 里面新增一条记录。比如,向/accounts/person发送请求,就可以新增一条人员记录。
curl -X PUT 'localhost:9200/accounts/person/1' -d '
{
"user": "张三",
"title": "工程师",
"desc": "数据库管理"
}'
服务器返回的 JSON 对象,会给出 Index、Type、Id、Version 等信息。
{
"_index":"accounts",
"_type":"person",
"_id":"1",
"_version":1,
"result":"created",
"_shards":{"total":2,"successful":1,"failed":0},
"created":true
}
如果 Id 已存在,返回的版本version
从1变成2,操作类型result
从 created
变成updated
,created
字段变成false
。
新增记录的时候,也可以不指定 Id
,这时要改成 POST 请求。
curl -X POST 'localhost:9200/accounts/person' -d '
{
"user": "李四",
"title": "工程师",
"desc": "系统管理"
}'
向 /Index/Type/Id
发出 GET
请求,就可以查看这条记录。
curl 'localhost:9200/accounts/person/1?pretty=true'
上面代码请求查看 /accounts/person/1
这条记录,URL 的参数 pretty=true
表示以易读的格式返回。
{
"_index" : "accounts",
"_type" : "person",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"user" : "张三",
"title" : "工程师",
"desc" : "数据库管理"
}
}
返回的数据中,found
字段表示查询成功,_source
字段返回原始记录。
删除记录就是发出 DELETE 请求。
curl -X DELETE 'localhost:9200/accounts/person/1'
使用 GET 方法,直接请求/Index/Type/_search,就会返回所有记录。
curl 'localhost:9200/accounts/person/_search'
{
"took":2,
"timed_out":false,
"_shards":{"total":5,"successful":5,"failed":0},
"hits":{
"total":2,
"max_score":1.0,
"hits":[
{
"_index":"accounts",
"_type":"person",
"_id":"AV3qGfrC6jMbsbXb6k1p",
"_score":1.0,
"_source": {
"user": "李四",
"title": "工程师",
"desc": "系统管理"
}
},
{
"_index":"accounts",
"_type":"person",
"_id":"1",
"_score":1.0,
"_source": {
"user" : "张三",
"title" : "工程师",
"desc" : "数据库管理,软件开发"
}
}
]
}
}
返回结果的 took
字段表示该操作的耗时(单位为毫秒),timed_out
字段表示是否超时,hits
字段表示命中的记录,里面子字段的含义如下。
- total:返回记录数,本例是2条。
- max_score:最高的匹配程度,本例是1.0。
- hits:返回的记录组成的数组。
https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl.html
Elastic 的查询非常特别,使用自己的查询语法,要求 GET 请求带有数据体。 Elastic 默认一次返回10条结果,可以通过size字段改变这个设置。通过from字段,指定位移。
curl 'localhost:9200/accounts/person/_search' -d '
{
"query" : { "match" : { "desc" : "软件" }},
"from": 1,
"size": 1
}'
- 如果有多个搜索关键字, Elastic 认为它们是
or
关系。
curl 'localhost:9200/accounts/person/_search' -d '
{
"query" : { "match" : { "desc" : "软件 系统" }}
}'
curl 'localhost:9200/accounts/person/_search' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "desc": "软件" } },
{ "match": { "desc": "系统" } }
]
}
}
}'
- 索引操作:
PUT {index}/{type}/{id}
需要修改成PUT {index}/_doc/{id}
- Mapping 操作:
PUT {index}/{type}/_mapping
则变成PUT {index}/_mapping
- 所有增删改查搜索操作返回结果里面的关键字
_type
都将被移除 - 父子关系使用 join 字段来构建
#创建索引
PUT twitter
{
"mappings": {
"_doc": {
"properties": {
"type": { "type": "keyword" },
"name": { "type": "text" },
"user_name": { "type": "keyword" },
"email": { "type": "keyword" },
"content": { "type": "text" },
"tweeted_at": { "type": "date" }
}
}
}
}
#修改索引
PUT twitter/_doc/user-kimchy
{
"type": "user",
"name": "Shay Banon",
"user_name": "kimchy",
"email": "shay@kimchy.com"
}
#搜索
GET twitter/_search
{
"query": {
"bool": {
"must": {
"match": {
"user_name": "kimchy"
}
},
"filter": {
"match": {
"type": "tweet"
}
}
}
}
}
#重建索引
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}