Index

索引可以理解为数据表。
常用操作有:新增、删除、判定存在与否。

新增索引

  1. PUT /my_index

删除索引

  1. DELETE /my_index

查找索引

判定是否存在

  1. HEAD /my_index

获取索引具体信息

  1. GET /my_index

获取的结果可以查看到如下信息

  1. {
  2. "my_index" : {
  3. "aliases" : { },
  4. "mappings" : { },
  5. "settings" : {
  6. "index" : {
  7. "routing" : {
  8. "allocation" : {
  9. "include" : {
  10. "_tier_preference" : "data_content"
  11. }
  12. }
  13. },
  14. "number_of_shards" : "1",
  15. "provided_name" : "my_index",
  16. "creation_date" : "1611050751273",
  17. "number_of_replicas" : "1",
  18. "uuid" : "xOgkSJdsQFyL1EtQAJGz0w",
  19. "version" : {
  20. "created" : "7100199"
  21. }}}}}

同时查询多个索引的信息

  1. GET /my_index,nba

查找当前所有索引

  1. GET /_cat/indices

Mapping

索引创建好后,还需要定义这个数据表的结构,那么Mapping 就是这个作用。
Mapping 可以不用手动创建,ES支持新增字段动态映射,会根据文档内容自动生成。但是不推荐这么做。
ES中 **mapping** 一旦创建,就不能修改,只能 ReIndex(删除后重建)

新增 Mapping

以下是新增mapping的 2 个方式:全局全新设置和为新字段新增
全局全新设置

  1. PUT /nba/_mapping
  2. {
  3. "properties": {
  4. "name": {
  5. "type": "text"
  6. },
  7. "team_name": {
  8. "type": "text"
  9. },
  10. "position": {
  11. "type": "keyword"
  12. },
  13. "play_year": {
  14. "type": "long"
  15. },
  16. "jerse_no": {
  17. "type": "keyword"
  18. }}}

新字段新增

  1. PUT /forum
  2. {
  3. "mappings": {
  4. "properties": {
  5. "articleID": {
  6. "type": "keyword"
  7. }}}}

修改 Maping

ES 是禁止修改已有字段的 Mapping ,就是说对于字段已经存在索引,需要删除后索引后重新建立。
但是如果是新增一个不存在的字段,那么可以作为新增一个字段的索引来进行操作。

删除 Mapping

删除 mapping,其实就是删除整个索引。

  1. DELETE /nba

获取 mapping

  1. GET /nba/_mapping
  2. {
  3. "nba" : {
  4. "mappings" : {
  5. "properties" : {
  6. "allstar" : {
  7. "type" : "long"
  8. },
  9. "country" : {
  10. "type" : "keyword"
  11. },
  12. "name" : {
  13. "type" : "text"
  14. }
  15. ...
  16. }}}}

也支持指定字段进行获取

  1. GET nba/_mapping/field/country
  2. {
  3. "nba" : {
  4. "mappings" : {
  5. "country" : {
  6. "full_name" : "country",
  7. "mapping" : {
  8. "country" : {
  9. "type" : "keyword"
  10. }}}}}}

Document

文档可以理解为传统的关系型数据库中的每一行的数据
Docuemnt具有新增、修改、删除、查询操作
新增文档可以指定 Id 或者选择其自动生成 id,个人感觉使用自定义 Id更好,方便同原始数据关联。

插入操作

插入的时候,可以选择自己定义 id,也可以交给 ES让其帮助生成 id。

自定义id

  1. PUT /nba/_doc/1
  2. {
  3. "name": "哈登",
  4. "team_name": "⽕箭",
  5. "position": "得分后卫",
  6. "play_year": "10",
  7. "jerse_no": "13"
  8. }

ES生成id

  1. POST /nba/_doc
  2. {
  3. "name": "库⾥",
  4. "team_name": "勇⼠",
  5. "position": "组织后卫",
  6. "play_year": "10",
  7. "jerse_no": "30"
  8. }

上述有 2 个区别

  • 使用自动生成的 id 是 POST,自己定义 id 是 PUT
  • 自己定义 id 需要给出具体的值

    批量插入文档

    1. POST /article/_bulk
    2. { "index": { "_id": 1 }}
    3. { "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" }
    4. { "index": { "_id": 2 }}
    5. { "articleID" : "KDKE-B-9947-#kL5", "userID" : 1, "hidden": false, "postDate": "2017-01-02" }
    6. { "index": { "_id": 3 }}
    7. { "articleID" : "JODL-X-1937-#pV7", "userID" : 2, "hidden": false, "postDate": "2017-01-01" }
    8. { "index": { "_id": 4 }}
    9. { "articleID" : "QQPX-R-3956-#aD8", "userID" : 2, "hidden": true, "postDate": "2017-01-02" }

    删除文档

    1. DELETE nba/_doc/3

    修改文档

    基本的修改

    1. POST /nba/_update/1
    2. {
    3. "doc":{
    4. "name":"哈登2"
    5. }
    6. }

    基于原数值的修改

    1. POST /nba/_update/1
    2. {
    3. "script": {
    4. "source": "ctx._source.age += params.age",
    5. "params": {
    6. "age": 4
    7. }
    8. }
    9. }

    新增字段

    下面的 other 就是新增的字段
    1. PUT /nba/_mapping
    2. {
    3. "properties":{
    4. "other":{
    5. "type":"long"
    6. }
    7. }
    8. }