ElasticSearch入门篇-操作1

背景

如果一切正常,Elastic 就会在默认的9200端口运行。这时,打开另一个命令行窗口,请求该端口,会得到说明信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ curl localhost:9200
{
"name" : "10eaed25cea5",
"cluster_name" : "my-es",
"cluster_uuid" : "dk5aq0evTjaXc2LEHIGtsA",
"version" : {
"number" : "7.7.1",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "ad56dce891c901a492bb1ee393f12dfff473a423",
"build_date" : "2020-05-28T16:30:01.040088Z",
"build_snapshot" : false,
"lucene_version" : "8.5.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}

基本概念

Node 与 Cluster

Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例。

单个 Elastic 实例称为一个节点(node)。一组节点构成一个集群(cluster)。

Index

Elastic 会索引所有字段,经过处理后写入一个反向索引(Inverted Index)。查找数据的时候,直接查找该索引。

所以,Elastic 数据管理的顶层单位就叫做 Index(索引)。它是单个数据库的同义词。每个 Index (即数据库)的名字必须是小写。

下面的命令可以查看当前节点的所有 Index。

1
$ curl -X GET 'http://localhost:9200/_cat/indices?v'

显示下面的结果:

Document

Index 里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index。

Document 使用 JSON 格式表示,下面是一个例子。

1
2
3
4
5
6
{
"type": "user",
"name": "Richard Keeper",
"user_name": "richkp",
"email": "richkp@sample.com"
}

同一个 Index 里面的 Document,不要求有相同的结构(scheme),但是最好保持相同,这样有利于提高搜索效率。

Type

Document 可以分组,比如weather这个 Index 里面,可以按城市分组(北京和上海),也可以按气候分组(晴天和雨天)。这种分组就叫做 Type,它是虚拟的逻辑分组,用来过滤 Document。

不同的 Type 应该有相似的结构(schema),举例来说,id字段不能在这个组是字符串,在另一个组是数值。这是与关系型数据库的表的一个区别。性质完全不同的数据(比如productslogs)应该存成两个 Index,而不是一个 Index 里面的两个 Type(虽然可以做到)。

下面的命令可以列出每个 Index 所包含的 Type。

1
$ curl 'localhost:9200/_mapping?pretty=true'

根据规划,Elastic 6.x 版只允许每个 Index 包含一个 Type,7.x 版将会彻底移除 Type。

新建和删除 Index

新建 Index,可以直接向 Elastic 服务器发出 PUT 请求。下面的例子是新建一个名叫weather的 Index。

1
curl -X PUT 'localhost:9200/weather'

服务器返回一个 JSON 对象,里面的acknowledged字段表示操作成功。

1
2
3
4
5
{
"acknowledged":true,
"shards_acknowledged":true,
"index":"weather"
}

我们发出 DELETE 请求,删除这个 Index。

1
curl -X DELETE 'localhost:9200/weather'

服务器返回一个 JSON 对象,里面的acknowledged字段表示操作成功。

1
{"acknowledged":true}

建立Index mapping

首先建立Index:twitter的mapping的数据项目,保存为 twitter.json 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"mappings": {
"_doc": {
"properties": {
"type": { "type": "keyword" },
"name": { "type": "text" },
"user_name": { "type": "keyword" },
"email": { "type": "keyword" },
"content": { "type": "text" },
"tweeted_at": { "type": "date" }
}
}
}
}

调用PUT请求:

1
2
curl -H 'Content-Type: application/json' -X PUT 'localhost:9200/twitter?include_type_name=true' -d @
twitter.json

返回以下值,命令执行成功:

1
2
3
4
5
{
"acknowledged":true,
"shards_acknowledged":true,
"index":"twitter"
}

数据操作

新增记录

假设我们需要添加user数据和tweet数据,我们创建两个文件,分别导入es

user1.json :

1
2
3
4
5
6
{
"type": "user",
"name": "Richard Keeper",
"user_name": "richkp",
"email": "richkp@sample.com"
}

导入:

1
curl -H 'Content-Type: application/json' -X PUT 'localhost:9200/twitter/_doc/user1' -d @user1.json

返回结果出现 “successful”:1 说明创建成功:

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"_index":"twitter",
"_type":"_doc",
"_id":"user1",
"_version":1,
"result":"created",
"_shards":{
"total":2,
"successful":1,
"failed":0},
"_seq_no":0,
"_primary_term":1
}

tweet1.json

1
2
3
4
5
6
{
"type": "tweet",
"user_name": "richkp",
"tweeted_at": "2022-10-24T10:00:00Z",
"content": "The first tweet, Hello world"
}

导入:

1
curl -H 'Content-Type: application/json' -X PUT 'localhost:9200/twitter/_doc/tweet1' -d @tweet1.json

查看记录

/Index/Type/Id发出 GET 请求,就可以查看这条记录。

1
curl 'localhost:9200/twitter/_doc/tweet1?pretty=true'

返回结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"_index" : "twitter",
"_type" : "_doc",
"_id" : "tweet1",
"_version" : 2,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"type" : "tweet",
"user_name" : "richkp",
"tweeted_at" : "2022-10-24T10:00:00Z",
"content" : "The first tweet, Hello world"
}
}

删除记录

删除记录就是发出 DELETE 请求:

1
curl -X DELETE 'localhost:9200/_doc/tweet1'

更新记录

更新记录就是使用 PUT 请求,重新发送一次数据。执行以下命令:

1
curl -H 'Content-Type: application/json' -X PUT 'localhost:9200/twitter/_doc/tweet1' -d @tweet1.json

数据查询

返回所有记录

使用 GET 方法,直接请求/Index/Type/_search,就会返回所有记录。

1
curl -X GET 'localhost:9200/twitter/_doc/_search'

返回结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "twitter",
"_type": "_doc",
"_id": "user1",
"_score": 1,
"_source": {
"type": "user",
"name": "Richard Keeper",
"user_name": "richkp",
"email": "richkp@sample.com"
}
},
{
"_index": "twitter",
"_type": "_doc",
"_id": "tweet1",
"_score": 1,
"_source": {
"type": "tweet",
"user_name": "richkp",
"tweeted_at": "2022-10-24T10:00:00Z",
"content": "The first tweet, Hello world"
}
}
]
}
}

全文搜索

Elastic 的查询非常特别,使用自己的查询语法,要求 GET 请求带有数据体。

1
2
3
curl -H 'Content-Type: application/json' -X GET 'localhost:9200/twitter/_doc/_search'  -d '{
"query" : { "match" : { "name" : "Richard" }}
}'

返回结果,返回所有名字中带有Richard 的记录:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{
"took": 619,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.6931471,
"hits": [
{
"_index": "twitter",
"_type": "_doc",
"_id": "user1",
"_score": 0.6931471,
"_source": {
"type": "user",
"name": "Richard Keeper",
"user_name": "richkp",
"email": "richkp@sample.com"
}
},
{
"_index": "twitter",
"_type": "_doc",
"_id": "user3",
"_score": 0.6931471,
"_source": {
"type": "user",
"name": "Richard Thomas",
"user_name": "richtm",
"email": "richtm@sample.com"
}
}
]
}
}

Elastic 默认一次返回10条结果,可以通过size字段改变这个设置。还可以通过from字段,指定位移。

1
2
3
4
5
curl -H 'Content-Type: application/json' -X GET 'localhost:9200/twitter/_doc/_search'  -d '{
"query" : { "match" : { "name" : "Richard" }},
"from": 1,
"size": 1
}'

上面代码指定,从位置1开始(默认是从位置0开始),只返回一条结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.6931471,
"hits": [
{
"_index": "twitter",
"_type": "_doc",
"_id": "user3",
"_score": 0.6931471,
"_source": {
"type": "user",
"name": "Richard Thomas",
"user_name": "richtm",
"email": "richtm@sample.com"
}
}
]
}
}

逻辑运算

如果有多个搜索关键字, Elastic 认为它们是or关系。

1
2
3
curl -H 'Content-Type: application/json' -X GET 'localhost:9200/twitter/_doc/_search'  -d '{
"query" : { "match" : { "name" : "Richard Keeper" }}
}'

可以看到,还是返回了2条记录:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{
"took": 104,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.89712,
"hits": [
{
"_index": "twitter",
"_type": "_doc",
"_id": "user1",
"_score": 1.89712,
"_source": {
"type": "user",
"name": "Richard Keeper",
"user_name": "richkp",
"email": "richkp@sample.com"
}
},
{
"_index": "twitter",
"_type": "_doc",
"_id": "user3",
"_score": 0.6931471,
"_source": {
"type": "user",
"name": "Richard Thomas",
"user_name": "richtm",
"email": "richtm@sample.com"
}
}
]
}
}

如果要执行多个关键词的and搜索,必须使用布尔查询

1
2
3
4
5
6
7
8
9
10
curl -H 'Content-Type: application/json' -X GET 'localhost:9200/twitter/_doc/_search'  -d '{
"query" : {
"bool": {
"must": [
{ "match": { "name": "Richard" } },
{ "match": { "name": "Keeper" } }
]
}
}
}'

可以看到,达到效果,只命中了一个记录:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
"took": 31,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.89712,
"hits": [
{
"_index": "twitter",
"_type": "_doc",
"_id": "user1",
"_score": 1.89712,
"_source": {
"type": "user",
"name": "Richard Keeper",
"user_name": "richkp",
"email": "richkp@sample.com"
}
}
]
}
}

请我喝杯咖啡吧~

支付宝
微信