ElasticSearch日常开发使用

:::tip 在CentOS上安装Headless Chromium的分步指南。 :::

索引操作

索引(也就是index)的常用的相关操作包括:新建索引、删除索引、

如何新建索引?

新建索引, 可以直接向 ElasticSearch 服务器发送 PUT 请求。例如,下面的例子是新建一个名为 users 的索引。

1
curl -XPUT "http://localhost:9200/users?pretty=true"

服务器将返回一个JSON对象,其中 acknowledged 代表是否操作成功,shards_acknowledged 是否分片成功,index 表示操作的索引名称,当前就是 users

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

如何删除索引?

删除索引,直接向 ElasticSearch 服务器发送 DELETE 请求,即可删除索引。例如,下面的例子是删除一个名为 users 的索引。

1
curl -XDELETE "http://localhost:9200/users?pretty=true"

服务器将返回一个JSON对象,具体如下所示:

1
2
3
{
  "acknowledged" : true
}

如何查看所有索引?

查看所有索引可以通过 _cat_indices 的方式向 ElasticSearch 服务器发送一个 GET 请求,即可查看 ElasticSearch 服务器的所有索引列表,具体可以执行如下请求:

1
curl -XGET 'http://localhost:9200/_cat/indices?v'

如何查看某个索引下的所有文档总数?

统计一个索引下的所有文档总数,也是一个非常常见的操作,可以直接通过 索引名称_count 方式,向 ElasticSearch 服务器发送一个 GET 快速实现,具体可以执行如下命令:

1
curl -XGET "http://localhost:9200/{index_name}/_count?pretty=true"

分组聚合数据

1
$ curl -XGET http://localhost:9200/fofaee_service/_search?pretty=true&q={"size":0,"aggs":{"group_by_ip":{"terms":{"field":"ip"}}}}

文档操作

ElasticSearch中索引就是关系型数据库中的表,而文档就相当于关系型数据库中的记录,这样理解起来也非常简单。

在elasticsearch 7.0.0版本必须使用单index,单type,多type结构则会完全移除。

如何添加文档?

在 ElasticSearch 7.0.0 及以后的版本中,官方要求必须使用单index,单type索引数据,并且默认type是 _doc,添加文档有两种方式:

  • 第一种是指定id值,id可以是整型,也可以是字符串,这个时候HTTP请求类型只能是 PUT
  • 第二种是不指定id值,这个时候HTTP请求类型只能是 POST,最终的文档数据 _id 字段是一个随机字符串。

如下面示例是通过明确指定id值的方式索引文档。注意这里的索引文档,与索引不是一个概念,索引作为名词是表示索引,索引作为动词时表示将文档添加到索引。

1
2
3
4
5
6
curl -XPUT "http://localhost:9200/users/_doc/1?pretty=true" -H "Content-Type: application/json" -d '
{
  "username": "helloshaohua",
  "title": "developer",
  "sex": "male"
}'

ElasticSearch 服务器,将会返回一个 JSON 对象,具体如下所示,其中 result,表示文档的添加状态,当前的执行结果为 created

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

如何查询文档?

对于查询指定id值的文档,还是比较简单的,可以向 ElasticSearch 服务器发送一个 GET 请求,即可获取到指定id值的文档。例如,如下示例则是查询索引为 users 并且 id1 的文档操作。

1
curl -XGET "http://localhost:9200/users/_doc/1?pretty=true"

ElasticSearch 服务器,则会返回如下 JSON 对象。注意,如果文档存在则 found 字段值为 true,否则为 false

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
{
  "_index" : "users",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 9,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "username" : "helloshaohua",
    "title" : "developer",
    "sex" : "male"
  }
}

这里只是简单的通过指定 id 查询文档数据,后面会介绍数据检索查询文档操作。

如何更新文档?

对于上面添加的 user 用户,id1,现在想将其用户名 username 修改为 tom,你可以指定全部字段,也可以指定部分字段,例如下面这个例子仅仅只是指定了提交的JSON数据对象为 {"username": "tom"},并没有指定 titlesex 字段,这完全是可以的。

1
2
3
4
curl -XPUT "http://localhost:9200/users/_doc/1?pretty=true" -H "Content-Type: application/json" -d '
{
  "username": "tom"
}'

ElasticSearch 服务器将会返回如下JSON对象,请注意当前的 resultupdated,表示数据已经更新成功,而 _version 版本号也发生了变化,之前添加数据时,_version 版本号为 1,现在因为有数据更新了,它的数值也就更新了,具体数值为 2,注意哦,只要文档更新一次,_version 版本号也会随之加 1 更新,它表示当前文档存在多少次更新操作。

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

如何删除文档?

删除文档操作,通过向 ElasticSearch 服务器发送一个 DELETE 请求来完成该操作。例如,如下示例则是删除 _id1 的文档操作。

1
curl -XDELETE "http://localhost:9200/users/_doc/1?pretty=true"

ElasticSearch 服务器,将返回如下所示 JSON 对象。注意,当前 result 字段值为 deleted,表示 _id1 的文档存在并且已经成功删除。如果文档记录不存在,则 result 字段值将为 not_found

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

文档检索

如何查看某个索引下的所有文档?

查看一个索引下的所有文档,几乎是最简单的搜索了,可以直接通过 索引名称_search 方式,向 ElasticSearch 服务器发送一个 GET 请求即可快速实现,例如,如下示例则是查询索引为 users 下的所示文档。

1
curl -XGET "http://localhost:9200/users/_search?pretty=true"

ElasticSearch 服务器,则会返回如下所示 JSON 对象。

 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
{
  "took" : 15,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "LW4KbnsBtXWyKufeRPdK",
        "_score" : 1.0,
        "_source" : {
          "username" : "helloshaohua",
          "title" : "developer",
          "sex" : "male"
        }
      },
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "username" : "tom",
          "title" : "developer, gopher, phper",
          "sex" : "male"
        }
      }
    ]
  }
}

其中 took 表示检测操作耗时,单位是毫秒。timed_out 字段表示该检索操作是否超时。hits 字段表示命中的记录,hits 子字段的具体含义如下所示:

  • total: 返回记录数。
    • value: 具体的记录条数,当前示例是 2 条。
    • relation: 亲属关系,当前示例是 eq 相等。
  • max_score: 最高的匹配程度,当前示例是 1.0。
  • hits: 命中的记录组成的数组。

命中的 hits 文档数组中的每一条文档,都有一个 _score 字段,表示匹配的程度,值越大说明匹配程度就越相似,默认是按照这个 _score 字段降序排列。

如何进行全文搜索?

1
2
3
4
curl -XGET "http://localhost:9200/users/_search?pretty=true" -H "Content-Type: application/json" -d '
{
  "query": {"match": {"title": "gopher"}}
}'

相关参考

comments powered by Disqus