Elasticsearch (ES) 搜索引擎: 基础操作:索引操作、映射操作、文档操作

原文链接:https://xiets.blog.csdn.net/article/details/132348791

版权声明:原创文章禁止转载

专栏目录:Elasticsearch 专栏(总目录)

1. 索引操作

索引操作 API:Index APIs

1.1 创建/删除索引

创建/删除索引 API:Create indexDelete index

请求:

  • 创建索引:PUT /<index>
  • 删除索引:DELETE /<index>

创建索引时,除了指定映射格式外,还可以设置别名信息、索引设置等,创建索引格式:

// 创建索引和映射
PUT /<index>
{
    
    
    "aliases": {
    
    
        // 索引的别名
    },
    "settings": {
    
    
        // 索引的设置
    },
    "mappings": {
    
    
        // 索引的映射
    }
}

// 也可以先创建索引, 然后再创建(扩展)映射
PUT /<index>

PUT /<index>/_mapping
{
    
    
    "properties": {
    
    
        // 映射的属性
    }
}

创建索引和映射示例:

PUT /topic
{
    
    
    "settings": {
    
               // 索引设置
        "index": {
    
    
            "number_of_shards": 3,      // 指定主分片个数
            "number_of_replicas": 2     // 指定副分片个数
        }
    },
    "mappings": {
    
                                   // 映射
        "properties": {
    
    
            "title": {
    
                              // 字段1, 包含子字段的字段
                "type": "text",
                "fields": {
    
    
                    "title_keyword": {
    
      // 子字段
                        "type": "keyword"
                    }
                }
            },
            "content": {
    
                            // 字段2
                "type": "text"
            },
            "create_time": {
    
                        // 字段3, 默认格式的日期字段
                "type": "date"
            },
            "tag": {
    
                                // 字段4
                "type": "keyword"
            },
            "statistics": {
    
                         // 字段5, 对象类型字段
                "properties": {
    
         // 对象的属性
                    "comment_count": {
    
    
                        "type": "integer"
                    },
                    "like_count": {
    
    
                        "type": "integer"
                    }
                }
            }
        }
    }
}

写入文档:

PUT /topic/_doc/001
{
    
    
    "title": "主题标题001",
    "content": "主题内容002",
    "create_time": "2023-07-01T20:00:00",
    "tag": ["标签1", "标签2"],
    "statistics": {
    
    
        "comment_count": 888,
        "like_count": 999
    }
}

1.2 关闭/打开索引

关闭/打开索引 API:Close indexOpen index

请求:

  • 关闭索引:POST /<index>/_close
  • 打开索引:POST /<index>/_open

索引关闭后,写入文档 和 数据查询 都将报错。

1.3 查看索引

查看索引 API:Get index API

请求:

  • 查看指定索引:GET /<index>
  • 查看所有索引:GET /_cat/indices?v
  • 判断索引是否存在:HEAD /<index>

使用示例,查看指定索引:

GET /topic
// 返回
{
    
    
    "topic": {
    
    
        "aliases": {
    
    },
        "mappings": {
    
    
            "properties": {
    
    
                "content": {
    
    
                    "type": "text"
                },
                "create_time": {
    
    
                    "type": "date"
                },
                "statistics": {
    
    
                    "properties": {
    
    
                        "comment_count": {
    
    
                            "type": "integer"
                        },
                        "like_count": {
    
    
                            "type": "integer"
                        }
                    }
                },
                "tag": {
    
    
                    "type": "keyword"
                },
                "title": {
    
    
                    "type": "text",
                    "fields": {
    
    
                        "title_keyword": {
    
    
                            "type": "keyword"
                        }
                    }
                }
            }
        },
        "settings": {
    
    
            "index": {
    
    
                "routing": {
    
    
                    "allocation": {
    
    
                        "include": {
    
    
                            "_tier_preference": "data_content"
                        }
                    }
                },
                "number_of_shards": "3",
                "provided_name": "topic",
                "creation_date": "1690447268704",
                "number_of_replicas": "2",
                "uuid": "v68kGuitRr-nwpwgtz_umw",
                "version": {
    
    
                    "created": "8090099"
                }
            }
        }
    }
}

使用示例,查看所有索引:

GET /_cat/indices?v
// 返回
health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   book    XhDqHqAgSumKjUiqGscesg   1   1          2            0      8.2kb          8.2kb
yellow open   hotel   AOgaS61PT3yKx366VVEqnQ   1   1          2            0      9.2kb          9.2kb
yellow open   topic   v68kGuitRr-nwpwgtz_umw   3   2          1            0      6.8kb          6.8kb

1.4 索引别名

别名操作 API:Alias management

别名是指给索引增加另一个名称,之后可以把该别名名称当做索引名称使用。例如一个按月份保存日志记录的索引,每个月份的日志一个索引,如果要查询当前月份的日志,就需要先获取到当前月份,然后获取当前月份的索引,每个月查询的索引都不一样。这样就可以创建一个别名,指向当前月份的索引,之后每次更换月份只需要把最新的当前月份索引设置为这个别名即可,业务代码中只需要对别名索引操作。

别名操作请求:POST /_aliases

先创建几个索引:

PUT /person_v1
{
    
    
    "mappings": {
    
    
        "properties": {
    
    
            "name": {
    
    
                "type": "keyword"
            },
            "age": {
    
    
                "type": "integer"
            }
        }
    }
}

PUT /logs_202307
{
    
    
    "mappings": {
    
    
        "properties": {
    
    
            "tag": {
    
    
                "type": "keyword"
            },
            "message": {
    
    
                "type": "text"
            }
        }
    }
}

PUT /logs_202308
{
    
    
    "mappings": {
    
    
        "properties": {
    
    
            "tag": {
    
    
                "type": "keyword"
            },
            "message": {
    
    
                "type": "text"
            }
        }
    }
}

1.4.1 创建别名

创建索引别名:

POST /_aliases
{
    
    
    "actions": [        // 可以一次性执行多个操作别名的动作
        {
    
    
            "add": {
    
                        // 增加别名
                "index": "person_v1",   // 原索引
                "alias": "person"       // 给索引增加的别名
            }
        },
        {
    
    
            "add": {
    
                        // 一个索引可以有多个别名
                "index": "person_v1",
                "alias": "person_current"
            }
        },
        {
    
    
            "add": {
    
    
                "index": "logs_202307",
                "alias": "logs"
            }
        }
    ]
}

1.4.2 别名读写文档

创建别名后,就可以把别名当做索引名称操作,写入文档和搜索文档:

// 写入文档
PUT /person_v1/_doc/001
{
    
    
    "name": "张三",
    "age": 30
}

PUT /person/_doc/002
{
    
    
    "name": "李四",
    "age": 32
}

// 获取文档
GET /person_current/_doc/001
// 返回
{
    
    
    "_index": "person_v1",
    "_id": "001",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
    
    
        "name": "张三",
        "age": 30
    }
}

// 搜索文档
POST /person_current/_search
{
    
    
    "query": {
    
    
        "term": {
    
    
            "name": {
    
    
                "value": "张三"
            }
        }
    }
}
// 返回
{
    
    
    "took": 0,
    "timed_out": false,
    "_shards": {
    
    
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
    
    
        "total": {
    
    
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.6931471,
        "hits": [
            {
    
    
                "_index": "person_v1",
                "_id": "002",
                "_score": 0.6931471,
                "_source": {
    
    
                    "name": "李四",
                    "age": 32
                }
            }
        ]
    }
}

1.4.3 查看别名

查看索引别名:GET /<index>/_aliasGET /<index>/_alias/<alias>

GET /person_v1/_alias
// 返回
{
    
    
    "person_v1": {
    
    
        "aliases": {
    
    
            "person": {
    
    },
            "person_current": {
    
    }
        }
    }
}

// 查看索引的指定别名
GET /person_v1/_alias/person
// 返回
{
    
    
    "person_v1": {
    
    
        "aliases": {
    
    
            "person": {
    
    }
        }
    }
}

其他别名操作:

  • 查看所有索引的别名:GET /_aliasGET /_cat/aliases?v
  • 查看指定别名被哪些索引应用:GET /_alias/<alias>
  • 判断别名是否存在:HEAD <index>/_alias/<alias>HEAD _alias/<alias>

1.4.4 删除别名

删除别名操作:

POST /_aliases
{
    
    
    "actions": [
        {
    
    
            "remove": {
    
                     // 删除索引别名
                "index": "person_v1",   // 原索引
                "alias": "person"       // 要删除的别名
            }
        }
    ]
}

把别名指向另一个索引(先删除再添加):

POST /_aliases
{
    
    
    "actions": [
        {
    
    
            "remove": {
    
                         // 删除别名
                "index": "logs_202307",
                "alias": "logs"
            }
        },
        {
    
    
            "add": {
    
    
                "index": "logs_202308",   // 把别名指向另个索引
                "alias": "logs"
            }
        }
    ]
}

2. 映射操作

映射操作 API:Mapping management

先创建一个索引:

PUT /person
{
    
    
    "mappings": {
    
    
        "properties": {
    
    
            "name": {
    
    
                "type": "keyword"
            },
            "age": {
    
    
                "type": "integer"
            }
        }
    }
}

2.1 查看映射

查看映射:

  • 查看所有索引映射:GET /_mapping
  • 查看指定索引映射:GET /<index>/_mapping
GET /person/_mapping
// 返回
{
    
    
    "person": {
    
    
        "mappings": {
    
    
            "properties": {
    
    
                "age": {
    
    
                    "type": "integer"
                },
                "name": {
    
    
                    "type": "keyword"
                }
            }
        }
    }
}

查看某个字段的映射:

  • 查看所有索引的指定字段:GET /_mapping/field/<field>
  • 查看指定索引的某个字段:GET /<index>/_mapping/field/<field>
GET /person/_mapping/field/age
// 返回
{
    
    
    "person": {
    
    
        "mappings": {
    
    
            "age": {
    
    
                "full_name": "age",
                "mapping": {
    
    
                    "age": {
    
    
                        "type": "integer"
                    }
                }
            }
        }
    }
}

2.2 扩展映射

映射字段更新操作,参考:Update mapping API

映射操作 API 请求:PUT /<index>/_mapping。使用此 API 可以将新字段添加到现有索引中,还可以更改现有字段的搜索设置。

增加字段、更改现有字段设置:

PUT /person/_mapping
{
    
    
    "properties": {
    
    
        "desc": {
    
                   // 扩展的属性
            "type": "text"
        },
        "name": {
    
                   // 更改现有字段的设置
            "type": "keyword",
            "ignore_above": 20
        }
    }
}

// 查看映射
GET /person/_mapping
// 返回
{
    
    
    "person": {
    
    
        "mappings": {
    
    
            "properties": {
    
    
                "age": {
    
    
                    "type": "integer"
                },
                "desc": {
    
    
                    "type": "text"
                },
                "name": {
    
    
                    "type": "keyword",
                    "ignore_above": 20
                }
            }
        }
    }
}

3. 文档操作

文档操作 API:Document APIs

先创建两个索引:

PUT /person
{
    
    
    "mappings": {
    
    
        "properties": {
    
    
            "name": {
    
    
                "type": "keyword",
                "ignore_above": 10
            },
            "desc": {
    
    
                "type": "text"
            },
            "time": {
    
    
                "type": "date",
                "format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
            }
        }
    }
}

PUT /topic
{
    
    
    "mappings": {
    
    
        "properties": {
    
    
            "title": {
    
    
                "type": "text"
            },
            "content": {
    
    
                "type": "text"
            }
        }
    }
}

3.1 写入文档

写入文档 API:Index API

写入文档 的请求:

  • PUT /<index>/_doc/<_id>
  • POST /<index>/_doc/
  • PUT /<index>/_create/<_id>
  • POST /<index>/_create/<_id>

写入文档使用 PUT 或 POST 请求方法,可以直接指定文档ID。如果没有指定文档ID,则必须使用 POST 请求。下面将在上面创建的 book 索引中写入一些文档。

PUT 指定 ID 写入文档(如果ID已存在,则会直接覆盖现有文档):

PUT /person/_doc/001
{
    
    
    "name": "张三",
    "desc": "张三的描述信息",
    "time": "2023-05-01 15:00:00"
}
// 返回:
{
    
    
    "_index": "person",
    "_id": "001",
    "_version": 1,
    "result": "created",
    "_shards": {
    
    
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

POST 不指定 ID 写入文档,写入后自动生成ID:

POST /person/_doc
{
    
    
    "name": "李四",
    "desc": "李四的描述信息",
    "time": "2023-05-02 16:00:00"
}
// 返回:
{
    
    
    "_index": "person",
    "_id": "u-tInIkBkPGxKMsw0_oH",
    "_version": 1,
    "result": "created",
    "_shards": {
    
    
    "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

3.2 查看文档

查看文档 API:Get API

查看文档请求:

  • GET <index>/_doc/<_id> 获取文档
  • HEAD <index>/_doc/<_id> 判断文档是否存在
  • GET <index>/_source/<_id> 获取文档,只返回 source 节点
  • HEAD <index>/_source/<_id> 判断文档是否存在

查看文档示例:

GET /person/_doc/001
// 返回
{
    
    
    "_index": "person",
    "_id": "001",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
    
    
        "name": "张三",
        "desc": "张三的描述信息",
        "time": "2023-05-01 15:00:00"
    }
}

GET /person/_source/001
// 返回
{
    
    
    "name": "张三",
    "desc": "张三的描述信息",
    "time": "2023-05-01 15:00:00"
}

搜索全部文档(返回前面部分):

GET /person/_search
// 返回
{
    
    
    "took": 1,
    "timed_out": false,
    "_shards": {
    
    
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
    
    
        "total": {
    
    
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
    
    
                "_index": "person",
                "_id": "001",
                "_score": 1,
                "_source": {
    
    
                    "name": "张三",
                    "desc": "张三的描述信息",
                    "time": "2023-05-01 15:00:00"
                }
            },
            {
    
    
                "_index": "person",
                "_id": "u-tInIkBkPGxKMsw0_oH",
                "_score": 1,
                "_source": {
    
    
                    "name": "李四",
                    "desc": "李四的描述信息",
                    "time": "2023-05-02 16:00:00"
                }
            }
        ]
    }
}

3.3 更新文档

更新文档 API:Update API

请求:POST /<index>/_update/<_id>

更新文档:

POST /person/_update/001
{
    
    
    "doc": {
    
                                // 需要修改的字段, 没有列出的将保存不变
        "name": "zhang san",
        "desc": "zhang san 的描述信息"
    }
}
// 返回
{
    
    
    "_index": "person",
    "_id": "001",
    "_version": 2,
    "result": "updated",
    "_shards": {
    
    
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

// 获取修改后的文档
GET /person/_doc/001
{
    
    
    "_index": "person",
    "_id": "001",
    "_version": 2,
    "_seq_no": 2,
    "_primary_term": 1,
    "found": true,
    "_source": {
    
    
        "name": "zhang san",
        "desc": "zhang san 的描述信息",
        "time": "2023-05-01 15:00:00"
    }
}

3.4 删除文档

删除文档 API:Delete API

请求:DELETE /<index>/_doc/<_id>

删除文档:

DELETE /person/_doc/001
// 返回
{
    
    
    "_index": "person",
    "_id": "001",
    "_version": 3,
    "result": "deleted",
    "_shards": {
    
    
    "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}

3.5 批量操作

批量操作 API:

针对前面创建的 persontopic 索引,现在写入一些数据:

PUT /person/_doc/100
{
    
    
    "name": "AAA",
    "desc": "AAA的描述信息",
    "time": "2023-06-01 15:00:00"
}

PUT /person/_doc/101
{
    
    
    "name": "BBB",
    "desc": "BBB的描述信息",
    "time": "2023-06-02 20:00:00"
}

PUT /topic/_doc/001
{
    
    
    "title": "主题001的标题",
    "content": "主题001的内容"
}

PUT /topic/_doc/002
{
    
    
    "title": "主题002的标题",
    "content": "主题002的内容"
}

使用 Bulk API ,在单个 API 调用中执行多个索引的文档增删改操作,这减少了开销,并且可以大大提高索引速度。

请求:

  • POST /_bulk
  • POST /<index>/_bulk

批量操作请求的格式:

POST /_bulk
{
    
     "index": {
    
     "_index": "index_name", "_id": "doc_id" } }    // 写入文档, 指定操作的索引和文档ID, 相当于: PUT /<index>/_doc/<_id>
{
    
     "field1": "value1", "field2": "value2"}                   // 上一行动作需要的数据, 即写入文档的数据
{
    
     "create": {
    
     "_index": "index_name", "_id": "doc_id" } }   // 写入文档, 相当于: PUT /<index>/_create/<_id>
{
    
     "field1": "value1", "field2": "value2"}                   // 上一行动作需要的数据, 即写入文档的数据
{
    
     "update": {
    
     "_index": "index_name", "_id": "doc_id" } }   // 更新文档, 相当于: POST /<index>/_update/<_id>
{
    
     "doc": {
    
     "name": "aaa", "desc": "aaa的描述信息" } }         // 上一行动作需要的数据, 即更新文档的数据
{
    
     "delete": {
    
     "_index": "person", "_id": "101" } }          // 删除文档 (删除动作不需要数据, 只需一行即可), 相当于: DELETE /<index>/_doc/<_id>

批量请求的数据,由多行 JSON 构成(一个 JSON 只能在同一行,不能换行),先指定要操作的索引和动作,然后下一行提供操作的数据。

批量操作请求示例:

POST /_bulk
{
    
     "index": {
    
     "_index": "person", "_id": "102" } }
{
    
     "name": "Hello", "desc": "Hello的描述信息", "time": "2023-05-03 17:00:00" }
{
    
     "create": {
    
     "_index": "person", "_id": "103" } }
{
    
     "name": "World", "desc": "World的描述信息", "time": "2023-05-04 18:00:00" }
{
    
     "index": {
    
     "_index": "topic", "_id": "003" } }
{
    
     "title": "主题标题AA", "content": "主题内容AA" }
{
    
     "create": {
    
     "_index": "topic", "_id": "004" } }
{
    
     "title": "主题标题BB", "content": "主题内容BB" }
{
    
     "update": {
    
     "_index": "person", "_id": "100" } }
{
    
     "doc": {
    
     "name": "aaa", "desc": "aaa的描述信息" } }
{
    
     "update": {
    
     "_index": "topic", "_id": "001" } }
{
    
     "doc": {
    
     "title": "修改过的主题001的标题", "content": "修改过的主题001的内容" } }
{
    
     "delete": {
    
     "_index": "person", "_id": "101" } }
{
    
     "delete": {
    
     "_index": "topic", "_id": "002" } }

批量操作返回:

{
    
    
    "took": 15,
    "errors": false,
    "items": [
        {
    
    
            "index": {
    
    
                "_index": "person",
                "_id": "102",
                "_version": 1,
                "result": "created",
                "_shards": {
    
    
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 6,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
    
    
            "create": {
    
    
                "_index": "person",
                "_id": "103",
                "_version": 1,
                "result": "created",
                "_shards": {
    
    
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 7,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
    
    
            "index": {
    
    
                "_index": "topic",
                "_id": "003",
                "_version": 1,
                "result": "created",
                "_shards": {
    
    
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 2,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
    
    
            "create": {
    
    
                "_index": "topic",
                "_id": "004",
                "_version": 1,
                "result": "created",
                "_shards": {
    
    
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 3,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
    
    
            "update": {
    
    
                "_index": "person",
                "_id": "100",
                "_version": 2,
                "result": "updated",
                "_shards": {
    
    
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 8,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
    
    
            "update": {
    
    
                "_index": "topic",
                "_id": "001",
                "_version": 2,
                "result": "updated",
                "_shards": {
    
    
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 4,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
    
    
            "delete": {
    
    
                "_index": "person",
                "_id": "101",
                "_version": 2,
                "result": "deleted",
                "_shards": {
    
    
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 9,
                "_primary_term": 1,
                "status": 200
            }
        },
        {
    
    
            "delete": {
    
    
                "_index": "topic",
                "_id": "002",
                "_version": 2,
                "result": "deleted",
                "_shards": {
    
    
                    "total": 2,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 5,
                "_primary_term": 1,
                "status": 200
            }
        }
    ]
}

其他批量操作:

猜你喜欢

转载自blog.csdn.net/xietansheng/article/details/132348791