Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。 作为 Elastic Stack 的核心,它集中存储您的数据,帮助您发现意料之中以及意料之外的情况。
Elasticsearch 基于搜索库 Lucene 开发。ElasticSearch 隐藏了 Lucene 的复杂性,提供了简单易用的 REST API / Java API 接口(另外还有其他语言的 API 接口)。
以下简称 ES。
REST API 最详尽的文档应该参考:ES 官方 REST API
新建 Index,可以直接向 ES 服务器发出 PUT 请求。
示例:直接创建索引
curl -X POST 'localhost:9200/user'服务器返回一个 JSON 对象,里面的 acknowledged 字段表示操作成功。
{"acknowledged":true,"shards_acknowledged":true,"index":"user"}示例:创建索引时指定配置
$ curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/user' -d '
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}'示例:创建索引时指定 mappings
$ curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/user' -d '
{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}'然后,我们可以通过发送 DELETE 请求,删除这个 Index。
curl -X DELETE 'localhost:9200/user'可以通过 GET 请求查看索引信息
# 查看索引相关信息
curl -X GET 'localhost:9200/user'
#查看索引的文档总数
CURL -X 'localhost:9200/user/_count'
#查看前10条文档,了解文档格式
POST user/_search
{
}
#_cat indices API
#查看indices
CURL -X /_cat/indices/kibana*?v&s=index
#查看状态为绿的索引
CURL -X /_cat/indices?v&health=green
#按照文档个数排序
CURL -X /_cat/indices?v&s=docs.count:desc
#查看具体的字段
CURL -X /_cat/indices/kibana*?pri&v&h=health,index,pri,rep,docs.count,mt
#How much memory is used per index?
CURL -X /_cat/indices?v&h=i,tm&s=tm:desc通过在 POST 中添加 _close 或 _open 可以打开、关闭索引。
关闭索引
curl -X POST 'localhost:9200/user/_close'打开索引
curl -X POST 'localhost:9200/user/_open'向指定的 /Index/type 发送 PUT 请求,就可以在 Index 里面新增一条记录。比如,向 /user/admin 发送请求,就可以新增一条人员记录。
$ curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/user/admin/1' -d '
{
"user": "张三",
"title": "工程师",
"desc": "数据库管理"
}'服务器返回的 JSON 对象,会给出 Index、Type、Id、Version 等信息。
{
"_index": "user",
"_type": "admin",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": { "total": 3, "successful": 1, "failed": 0 },
"_seq_no": 0,
"_primary_term": 2
}如果你仔细看,会发现请求路径是/user/admin/1,最后的1是该条记录的 Id。它不一定是数字,任意字符串(比如abc)都可以。
新增记录的时候,也可以不指定 Id,这时要改成 POST 请求。
$ curl -X POST -H 'Content-Type: application/json' 'localhost:9200/user/admin' -d '
{
"user": "李四",
"title": "工程师",
"desc": "系统管理"
}'上面代码中,向/user/admin发出一个 POST 请求,添加一个记录。这时,服务器返回的 JSON 对象里面,_id字段就是一个随机字符串。
{
"_index": "user",
"_type": "admin",
"_id": "WWuoDG8BHwECs7SiYn93",
"_version": 1,
"result": "created",
"_shards": { "total": 3, "successful": 1, "failed": 0 },
"_seq_no": 1,
"_primary_term": 2
}注意,如果没有先创建 Index(这个例子是accounts),直接执行上面的命令,Elastic 也不会报错,而是直接生成指定的 Index。所以,打字的时候要小心,不要写错 Index 的名称。
删除记录就是发出 DELETE 请求。
curl -X DELETE 'localhost:9200/user/admin/2'更新记录就是使用 PUT 请求,重新发送一次数据。
$ curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/user/admin/1' -d '
{
"user": "张三",
"title": "工程师",
"desc": "超级管理员"
}'向/Index/Type/Id发出 GET 请求,就可以查看这条记录。
curl 'localhost:9200/user/admin/1?pretty'上面代码请求查看 /user/admin/1 这条记录,URL 的参数 pretty=true 表示以易读的格式返回。
返回的数据中,found 字段表示查询成功,_source字段返回原始记录。
{
"_index": "user",
"_type": "admin",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"user": "张三",
"title": "工程师",
"desc": "超级管理员"
}
}如果 Id 不正确,就查不到数据,found 字段就是 false
使用 GET 方法,直接请求 /index/type/_search,就会返回所有记录。
$ curl 'localhost:9200/user/admin/_search?pretty'
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "user",
"_type" : "admin",
"_id" : "WWuoDG8BHwECs7SiYn93",
"_score" : 1.0,
"_source" : {
"user" : "李四",
"title" : "工程师",
"desc" : "系统管理"
}
},
{
"_index" : "user",
"_type" : "admin",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"user" : "张三",
"title" : "工程师",
"desc" : "超级管理员"
}
}
]
}
}上面代码中,返回结果的 took字段表示该操作的耗时(单位为毫秒),timed_out字段表示是否超时,hits字段表示命中的记录,里面子字段的含义如下。
total:返回记录数,本例是 2 条。max_score:最高的匹配程度,本例是1.0。hits:返回的记录组成的数组。
返回的记录中,每条记录都有一个_score字段,表示匹配的程序,默认是按照这个字段降序排列。
ES 的查询非常特别,使用自己的查询语法,要求 GET 请求带有数据体。
$ curl -H 'Content-Type: application/json' 'localhost:9200/user/admin/_search?pretty' -d '
{
"query" : { "match" : { "desc" : "管理" }}
}'上面代码使用 Match 查询,指定的匹配条件是desc字段里面包含"软件"这个词。返回结果如下。
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.38200712,
"hits" : [
{
"_index" : "user",
"_type" : "admin",
"_id" : "WWuoDG8BHwECs7SiYn93",
"_score" : 0.38200712,
"_source" : {
"user" : "李四",
"title" : "工程师",
"desc" : "系统管理"
}
},
{
"_index" : "user",
"_type" : "admin",
"_id" : "1",
"_score" : 0.3487891,
"_source" : {
"user" : "张三",
"title" : "工程师",
"desc" : "超级管理员"
}
}
]
}
}Elastic 默认一次返回 10 条结果,可以通过size字段改变这个设置,还可以通过from字段,指定位移。
$ curl 'localhost:9200/user/admin/_search' -d '
{
"query" : { "match" : { "desc" : "管理" }},
"from": 1,
"size": 1
}'上面代码指定,从位置 1 开始(默认是从位置 0 开始),只返回一条结果。
如果有多个搜索关键字, Elastic 认为它们是or关系。
$ curl 'localhost:9200/user/admin/_search' -d '
{
"query" : { "match" : { "desc" : "软件 系统" }}
}'上面代码搜索的是软件 or 系统。
如果要执行多个关键词的and搜索,必须使用布尔查询。
$ curl -H 'Content-Type: application/json' 'localhost:9200/user/admin/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "desc": "管理" } },
{ "match": { "desc": "超级" } }
]
}
}
}'TODO:待补充...