ElasticSeacrch的基本操作
本篇主要讲述 ES 相对于传统数据库的增删改查操作。
目录
基本概念
拿 MySQL 数据库进行对比,数据库相当于 ES 中的索引(Indices),数据库中的表相当于 ES 中的类型(types),而表中的每一行相当于一个类型里面文档(Documents),而表中的每一列相当于文档中的每个字段(Fields)
Relational DB -> Databases -> Tables -> Rows -> Columns Elasticsearch -> Indices(数据库)-> Types(表)-> Documents(行)-> Fields(字段)
Node 与 Cluster
Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例。
单个 Elastic 实例称为一个节点(node)。一组节点构成一个集群(cluster)。
Index
Elastic 会索引所有字段,经过处理后写入一个反向索引(Inverted Index)。查找数据的时候,直接查找该索引。
所以,Elastic 数据管理的顶层单位就叫做 Index(索引)。它是单个数据库的同义词。每个 Index (即数据库)的名字必须是小写。
下面的命令可以查看当前节点的所有 Index。
$ curl -X GET 'http://localhost:9200/_cat/indices?v'
Document
Index 里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index。
Document 使用 JSON 格式表示,下面是一个例子。
{ "user": "张三", "title": "工程师", "desc": "数据库管理" }
同一个 Index 里面的 Document,不要求有相同的结构(scheme),但是最好保持相同,这样有利于提高搜索效率。
Type
Document 可以分组,比如weather这个 Index 里面,可以按城市分组(北京和上海),也可以按气候分组(晴天和雨天)。这种分组就叫做 Type,它是虚拟的逻辑分组,用来过滤 Document。
不同的 Type 应该有相似的结构(schema),举例来说,id字段不能在这个组是字符串,在另一个组是数值。这是与关系型数据库的表的一个区别。
性质完全不同的数据(比如products和logs)应该存成两个 Index,而不是一个 Index 里面的两个 Type(虽然可以做到)。
下面的命令可以列出每个 Index 所包含的 Type。
$ curl 'localhost:9200/_mapping?pretty=true'
根据规划,Elastic 6.x 版只允许每个 Index 包含一个 Type,7.x 版将会彻底移除 Type。
ES数据操作
创建和删除索引
新建 Index,可以直接向 Elastic 服务器发出 PUT 请求。下面的例子是新建一个名叫weather的 Index。
curl -X PUT 'localhost:9200/accounts'
返回值
{ "acknowledged": true, "shards_acknowledged": true }
插入一条记录
[root@controller config]# curl -X PUT 'localhost:9200/accounts/person/2?pretty' -d ' {
"user": "李四", "title": "工程师", "desc": "数据库管理" }'
返回值:
{"_index":"company", "_type":"user", "_id":"1", "_version":1,"result": "created","_shards":{"total":2,"successful":1,"failed":0},"created":true}
服务器返回的 JSON 对象,会给出 Index、Type、Id、Version 等信息。
如果你仔细看,会发现请求路径是/accounts/person/1,最后的1是该条记录的 Id。它不一定是数字,任意字符串(比如abc)都可以。
新增记录的时候,也可以不指定 Id,这时要改成 POST 请求。
curl -X POST 'localhost:9200/accounts/person' -d '
{ "user": "李四", "title": "工程师", "desc": "系统管理" }'
上面代码中,向/accounts/person发出一个 POST 请求,添加一个记录。这时,服务器返回的 JSON 对象里面,_id字段就是一个随机字符串。
{"_index":"accounts","_type":"person","_id":"AWvPsf8COLI8LAL_MEKc","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"created":true}[
注意,如果没有先创建 Index(这个例子是accounts),直接执行上面的命令,Elastic 也不会报错,而是直接生成指定的 Index。所以,打字的时候要小心,不要写错 Index 的名称。
查看记录
向/Index/Type/Id发出 GET 请求,就可以查看这条记录。
curl 'localhost:9200/accounts/person/1?pretty=true'
上面代码请求查看/accounts/person/1这条记录,URL 的参数pretty=true表示以易读的格式返回。
返回的数据中,found字段表示查询成功,_source字段返回原始记录。
{ "_index" : "accounts", "_type" : "person", "_id" : "1", "_version" : 1, "found" : true, "_source" : { "user" : "张三", "title" : "工程师", "desc" : "数据库管理" } }
如果 Id 不正确,就查不到数据,found字段就是false。
[root@controller config]# curl 'localhost:9200/accounts/person/2?pretty=true' { "_index" : "accounts", "_type" : "person", "_id" : "2", "found" : false }
删除记录
删除记录就是发出 DELETE 请求。
curl -X DELETE 'localhost:9200/accounts/person/1'
这里先不要删除这条记录,后面还要用到。
更新记录
更新记录就是使用 PUT 请求,重新发送一次数据。
curl -X PUT 'localhost:9200/accounts/person/1?pretty=true' -d '
{ "user" : "张三", "title" : "工程师", "desc" : "数据库管理,软件开发" }'
{ "_index" : "accounts", "_type" : "person", "_id" : "1", "_version" : 3, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "created" : false }
上面代码中,我们将原始数据从"数据库管理"改成"数据库管理,软件开发"。 返回结果里面,有几个字段发生了变化。
"_version" : 2, "result" : "updated", "created" : false
可以看到,记录的 Id 没变,但是版本(version)从1变成2,操作类型(result)从created变成updated,created字段变成false,因为这次不是新建记录
ES数据查询
返回所有记录
使用 GET 方法,直接请求/Index/Type/_search,就会返回所有记录。
curl 'localhost:9200/accounts/person/_search?pretty'
{ "took" : 4, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 1.0, "hits" : [ { "_index" : "accounts", "_type" : "person", "_id" : "AWvPsf8COLI8LAL_MEKc", "_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:返回的记录组成的数组。
返回的记录中,每条记录都有一个_score字段,表示匹配的程序,默认是按照这个字段降序排列。
curl 'localhost:9200/accounts/person/_search?pretty' -d ' {
"query" : { "match" : { "desc" : "管理" }}
}'
上面代码使用 Match 查询,指定的匹配条件是desc字段里面包含"管理"这个词。返回结果如下。
{ "took" : 112, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 0.5446649, "hits" : [ { "_index" : "accounts", "_type" : "person", "_id" : "1", "_score" : 0.5446649, "_source" : { "user" : "张三", "title" : "工程师", "desc" : "数据库管理,软件开发" } } ] } }
Elastic 默认一次返回10条结果,可以通过size字段改变这个设置。
curl 'localhost:9200/accounts/person/_search?pretty' -d ' { "query" : { "match" : { "desc" : "管理" }}, "size": 1 }'
还可以通过from字段,指定位移。
curl 'localhost:9200/accounts/person/_search?pretty' -d ' { "query" : { "match" : { "desc" : "管理" }}, "from":1, "size": 1 }'
上面代码指定,从位置1开始(默认是从位置0开始),只返回一条结果。
逻辑运算
(以下操作尚未验证)
如果有多个搜索关键字, Elastic 认为它们是or关系。
$ curl 'localhost:9200/accounts/person/_search' -d ' { "query" : { "match" : { "desc" : "软件 系统" }} }'
上面代码搜索的是软件 or 系统。
如果要执行多个关键词的and搜索,必须使用布尔查询。
$ curl 'localhost:9200/accounts/person/_search' -d ' { "query": { "bool": { "must": [ { "match": { "desc": "软件" } }, { "match": { "desc": "系统" } } ] } } }'
参考文档
[1] http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html
[2] https://gitbook.cn/books/5cad51b2aea01a433bdfe2e7/index.html