ES 基本概念和基本操作

说明

本文来自于博客园,用以记录通过访问接口的方式来访问 es 的简单操作。

大部分源自原文,但是因为ES版本不同,做了部分内容修改。此文ES版本7.6

img

es & mysql 概念对比表。type类型将会在以后的版本中被移除

es 接口访问示例

1.文档操作

1.索引操作

建立索引就相当于创建数据库,创建数据库就要创建表结构。es的建立索引就是创建数据库这一操作。

创建索引
PUT http://localhost:9200/zq_test  # 建立一个名为 zq_test 的索引

返回结果:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "zq_test"
}
获取索引
GET http://localhost:9200/zq_test

返回结果:

{
    "zq_test": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1623325887846",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "V1Ewmh1sSAmPwtiUqgfY-Q",
                "version": {
                    "created": "7060099"
                },
                "provided_name": "zq_test"
            }
        }
    }
}

2.设置mapping

设置Index的Mapping,也就是设置数据的表结构

删除索引
DELEET http://localhost:9200/zq_test

返回:

{
    "acknowledged": true
}
重新创建索引
PUT http://localhost:9200/zq_test  #  并提交一下参数,content-type:application/json

{
    "mappings": {
        "properties": {
            "name": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "age": {
                "type": "long",
                "index": true
            },
            "gender": {
                "type": "keyword"
            }
        }
    }
}

返回:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "zq_test"
}

name:text类型,会进行分词,支持模糊检索。

name.keyword : 这相当于是嵌套了一个字段,keyword类型,只能精确匹配,不支持分词。超过256字符长度不索引,也就没法搜索到。

age:long类型,支持精确匹配。index 为 true 才可以匹配

gender:keyword类型,只能精确匹配,不支持分词。

3.插入文档

插入文档,即使用接口,将 json 文档放入 es 索引中。

请求必须指定文档的索引名称,唯一的文档 ID, 以及请求体中一个或多个键值对。

PUT http://localhost:9200/zq_test/_doc/2

# json 
{
    "name": "tom",
    "age": 5,
    "gender": "man"
}

返回:

{
    "_index": "zq_test",
    "_type": "_doc",
    "_id": "2",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

4.获取文档

根据 id 获取文档。
GET http://localhost:9200/zq_test/_doc/1

返回:

{
    "_index": "zq_test",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "yezi test",
        "age": 25,
        "gender": "man"
    }
}

5.删除文档

根据 id 删除文档
DELETE http://localhost:9200/zq_test/_doc/2

返回:

{
    "_index": "zq_test",
    "_type": "_doc",
    "_id": "2",
    "_version": 2,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}
加入两个测试数据
PUT http://localhost:9200/zq_test/_doc/3

{
    "name": "ceshi01",
    "age": 35,
    "gender": "man"
}

PUT http://localhost:9200/zq_test/_doc/4

{
    "name": "ceshi02",
    "age": 40,
    "gender": "woman"
}

根据筛选条件删除数据
# 根据年龄删除数据
Post  http://localhost:9200/zq_test/_doc/_delete_by_query 
        
# json body
{
    "query": {
        "match": {
            "age": 35
        }
    }
}

返回:

{
    "took": 13,
    "timed_out": false,
    "total": 1,
    "deleted": 1,
    "batches": 1,
    "version_conflicts": 0,
    "noops": 0,
    "retries": {
        "bulk": 0,
        "search": 0
    },
    "throttled_millis": 0,
    "requests_per_second": -1.0,
    "throttled_until_millis": 0,
    "failures": []
}

可使用 {"query": {"match_all":{}}} 的方式删除所有

6.更新文档

覆盖更新(先删除后添加):

更新完成后,可以看到原有 name age gender只剩下 age,相当于删除原有。

Put  http://localhost:9200/zq_test/_doc/4
        
# json body
{
    "age": 500
}

返回:

{
    "_index": "zq_test",
    "_type": "_doc",
    "_id": "4",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}
部分更新(不删除,更新相应字段) 建议用此
POST  http://localhost:9200/zq_test/_doc/4/_update
        
# json body
{
    "doc": {
        "age": 10000,
        "gender": "aha"
    }
}

返回:

{
    "_index": "zq_test",
    "_type": "_doc",
    "_id": "4",
    "_version": 6,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 13,
    "_primary_term": 1
}

# 对应的数据如下
{
    "_index": "zq_test",
    "_type": "_doc",
    "_id": "4",
    "_version": 6,
    "_seq_no": 13,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "jerry",
        "age": 10000,
        "gender": "aha"
    }
}
根据查询条件更新
POST http://localhost:9200/zq_test/_doc/_update_by_query
        
# json body  2021-06-18 18:18  试验失败
{
    "script": {
        "source": "ctx._source.age=5",  # ctx._source 指源文档
        "lang": "painless"
    },
    "query": {
        "term": {
            "name": "ceshi01"
        }
    }
}

返回:

{
    "_index": "zq_test",
    "_type": "_doc",
    "_id": "_update_by_query",
    "found": false
}

7.批量获取

批量获取文档(不指定索引库)

GET http://localhost:9200/_mget
        
# json  链接中没有指明索引库,但是在 json body 中进行了指定
{
    "docs": [
        {
            "_index": "zq_test",
            "_type": "_doc",
            "_id": 4    
        },
        {
            "_index": "zq_test_copy",
            "_type": "_doc",
            "_id": 4    
        }
    ]
}

返回:

{
    "docs": [
        {
            "_index": "zq_test",
            "_type": "_doc",
            "_id": "4",
            "_version": 3,
            "_seq_no": 4,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name": "jerry",
                "age": 500,
                "gender": "man"
            }
        },
        {
            "_index": "zq_test_copy",
            "_type": "_doc",
            "_id": "4",
            "_version": 2,
            "_seq_no": 1,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name": "jerry",
                "age": 500,
                "gender": "man"
            }
        }
    ]
}
批量获取文档(指定索引库或类型)
# 一
GET  http://localhost:9200/zq_test/_mget

# json 中指明 _id, 链接中指明 index
{
    "docs": [
        {
            "_id": 4    
        },
        {
            "_id": 3    
        }
    ]
}

返回:
{
    "docs": [
        {
            "_index": "zq_test",
            "_type": "_doc",
            "_id": "4",
            "_version": 3,
            "_seq_no": 4,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name": "jerry",
                "age": 500,
                "gender": "man"
            }
        },
        {
            "_index": "zq_test",
            "_type": "_doc",
            "_id": "3",
            "_version": 1,
            "_seq_no": 5,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name": "ceshi01",
                "age": 35,
                "gender": "man"
            }
        }
    ]
}     

# 二
GET  http://localhost:9200/zq_test/_doc/_mget
        
# json 中指明 _id, 链接中指明 index 和 _doc
{
    "ids": [3, 4]
}

返回:
{
    "docs": [
        {
            "_index": "zq_test",
            "_type": "_doc",
            "_id": "3",
            "_version": 1,
            "_seq_no": 5,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name": "ceshi01",
                "age": 35,
                "gender": "man"
            }
        },
        {
            "_index": "zq_test",
            "_type": "_doc",
            "_id": "4",
            "_version": 3,
            "_seq_no": 4,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name": "jerry",
                "age": 500,
                "gender": "man"
            }
        }
    ]
}

8.批处理操作

ES允许我们在一次请求中做多种操作,比如在一次请求中同时进行增删改查操作:

批处理操作
POST http://localhost:9200/_bulk  #  下边进行了一个批处理操作,分别包含添加、删除、更新操作,

但是提交的参数必须是一行一行的json,且结尾必须有换行符,要这样ES才好处理,因为不是标准的json格式,这里PostMan识别json格式错误,但是问题不大。

json body:

{"index": {"_index": "zq_test", "_id": 5}}
{"name": "bulk", "age": 16, "gender": "woman"}
{"delete": {"_id": 5, "_index": "zq_test"}}
{"update": {"_id": 4, "_index": "zq_test"}}
{"doc": {"name": "jiuge", "age": 333, "gender": "sex"}}

# 注意,上面的 json body 必须每行都有换行符,即要多一个空行。另外已经删除的 5 不能 update

返回:

{
    "took": 4,
    "errors": false,
    "items": [
        {
            "index": {
                "_index": "zq_test",
                "_type": "_doc",
                "_id": "5",
                "_version": 16,
                "result": "created",
                "_shards": {
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 29,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "delete": {
                "_index": "zq_test",
                "_type": "_doc",
                "_id": "5",
                "_version": 17,
                "result": "deleted",
                "_shards": {
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 30,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
            "update": {
                "_index": "zq_test",
                "_type": "_doc",
                "_id": "4",
                "_version": 7,
                "result": "updated",
                "_shards": {
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 31,
                "_primary_term": 1,
                "status": 200
            }
        }
    ]
}

9.重建索引

重新建立索引,相当于拷贝一份.

POST http://localhost:9200/_reindex

json body:

{
    "source": {
        "index": "zq_test"
    },
    "dest":{
        "index": "zq2"
    }
}

返回:

{
    "took": 184,
    "timed_out": false,
    "total": 2,
    "updated": 0,
    "created": 2,
    "deleted": 0,
    "batches": 1,
    "version_conflicts": 0,
    "noops": 0,
    "retries": {
        "bulk": 0,
        "search": 0
    },
    "throttled_millis": 0,
    "requests_per_second": -1.0,
    "throttled_until_millis": 0,
    "failures": []
}

2.查询操作

1.多索引和多类型查询

# 1 查询所有索引下所有类型的数据,查询条件为 name=jiuge
GET http://localhost:9200/_search?q=name:jiuge  # 可以不带 _all

# 2 获取所有索引下类型为 _doc 的 数据,查询条件为 name = jiuge
GET http://localhost:9200/_all/_doc/_search?q=name:jiuge  
            
# 3 获取zq_test索引和 zq2 索引下类型为_doc 的数据,查询条件下为 name=jiuge
GET http://localhost:9200/zq_test,zq2/_doc/_search?q=name:jiuge
            
# *4 获取zq_test 索引下类型为 teacher和student的数据,查询条件为 name=jiuge
GET http://localhost:9200/zq_test/teacher,student/_search?q=name:jiuge  # 因为es7 及其之后,type默认为_doc, 且在 es8 后,type 将取消,因此此访问方式仅作参考,不必使用。

返回:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 16,
        "successful": 16,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.7917595,
        "hits": [
            {
                "_index": "zq_test",
                "_type": "_doc",
                "_id": "4",
                "_score": 1.7917595,
                "_source": {
                    "name": "jiuge",
                    "age": 333,
                    "gender": "sex"
                }
            },
            {
                "_index": "zq2",
                "_type": "_doc",
                "_id": "4",
                "_score": 0.6931471,
                "_source": {
                    "name": "jiuge",
                    "age": 333,
                    "gender": "sex"
                }
            }
        ]
    }
}

2. 查询字符串查询

通过 GET 访问的方式,在 URL 后面添加查询字符串的方式查询。

GET http://localhost:9200/zq_test/_search?from=0&size=10&q=name:ceshi006 OR name:ceshi007&sort=age:asc&_source=name,age

说明:

  • from=0&size=10 : 分页获取
  • q=name:ceshi006 OR name:ceshi007: 表示查询 name 为 ceshi006 或 ceshi007 的文档。OR 可以换做 AND
  • sort=age:asc排序,表示按照 age 进行排序。asc 降序, desc 升序
  • _source=name,age投影,选择返回的字段,多个字段用 , 隔开

返回:

{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "zq_test",
                "_type": "_doc",
                "_id": "6",
                "_score": null,
                "_source": {
                    "name": "ceshi006",
                    "age": 66
                },
                "sort": [
                    66
                ]
            },
            {
                "_index": "zq_test",
                "_type": "_doc",
                "_id": "7",
                "_score": null,
                "_source": {
                    "name": "ceshi007",
                    "age": 77
                },
                "sort": [
                    77
                ]
            }
        ]
    }
}

可使用插入文档的方式放入多个数据供实验. 更多查询参数和语法[点此查看]([Search API | Elasticsearch Guide 7.13] | Elastic)

3. 请求体查询

通过请求体进行查询。

GET http://localhost:9200/zq_test/_search  # 也通过 json body

json body:

{
    "query":{
        "match":{
            "name": "ceshi006"
        }
    },
    "sort": [
        {
            "age": "asc"
        }
    ],
    "size": 5,
    "from": 0
}

返回:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "zq_test",
                "_type": "_doc",
                "_id": "6",
                "_score": null,
                "_source": {
                    "name": "ceshi006",
                    "age": 66,
                    "gender": "man"
                },
                "sort": [
                    66
                ]
            }
        ]
    }
}

更多操作参数[点此查看]([Request body search | Elasticsearch Guide 7.13] | Elastic)

posted @ 2022-02-21 11:16  小片清风  阅读(1022)  评论(0编辑  收藏  举报