当前位置:   article > 正文

Elasticsearch--索引映射管理_在elasticsearch中,索引映射关联

在elasticsearch中,索引映射关联

映射(mapping)

映射像关系数据库中的表结构,每一个索引都有一个映射,它定义了索引中的每一个字段类型,以及一个索引范围内的设置。一个映射可以事先被定义,或者在第一次存储文档的时候自动识别。

增加映射

API允许向索引(index)添加文档类型(type),或者向文档类型中添加字段(field)。

PUT /secisland
{
  "mappings": {
    "log":{
      "properties": {
        "message":{
          "type": "text"
        }
      }
    }
  }
}


GET /secisland/_mapping
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

添加索引名为secisland,文档类型为log,其中包含字段message,字段类型为text。


PUT secisland/_mapping/user
{
  "properties": {
    "name": {
      "type": "text"
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

向已经存在的索引secisland添加文档类型为user,包含字段name,字段类型为text。


```js PUT secisland/_mapping/log { "properties": { "user_name": { "type": "text" } } } ``` 已经存在索引secisland,文档类型为log,添加新的字段user_name,字段类型是字符串。
//查看mapping
GET /secisland/_mapping
  • 1
  • 2

返回:

{
  "secisland": {
    "mappings": {
      "user": {
        "properties": {
          "name": {
            "type": "text"
          }
        }
      },
      "log": {
        "properties": {
          "message": {
            "type": "text"
          },
          "user_name": {
            "type": "text"
          }
        }
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

1. 多个索引设置映射
可以一次向多个索引添加文档类型:

PUT /{index}/_mapping/{type}
{body}
  • 1
  • 2
  • {index}可以有多种方式,都好分隔:比如test1,test2,test3。_all表示所有索引。通配符*表示所有。test*表示以test开头。
  • {type}需要添加或更新的文档类型。
  • {body}需要添加的字段或字段类型

2. 更新字段映射
在一般情况下,对现有字段的映射不会更新。对这个规则有一些例外。

  1. 新的属性被添加到对象数据类型的字段。
  2. 新的多域字段被添加到现有的字段。
  3. doc_values可以被禁用。
  4. 增加了ignore_above参数

3. 不同类型之间的冲突
在同一个索引的不同类型中,相同名称的字段中必须有相同的映射,因为它们内部是在同一个领域内,如果试图在这种情况下更新映射参数,系统将会抛出异常。除非在更新的时候指定update_all_types参数。在这种情况下它将更新所有同一索引同名称的映射参数。

示例:

PUT /secisland/
{
  "mappings": {
    "type_one":{
      "properties": {
        "text":{"type": "text", "analyzer": "standard"}
      }
    },
    "type_two":{
      "properties": {
        "text":{"type": "text", "analyzer": "standard"}
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

修改type_one的映射

PUT /secisland/_mapping/type_one
{
  "properties": {
    "text":{
      "type": "text",
      "analyzer": "standard",
      "search_analyzer": "whitespace"
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

报错:
在这里插入图片描述
如果要修改,添加参数update_all_types,同时更新两个类型。

PUT /secisland/_mapping/type_one?update_all_types
{
  "properties": {
    "text":{
      "type": "text",
      "analyzer": "standard",
      "search_analyzer": "whitespace"
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
获取映射

获取文档映射接口允许通过索引或者索引和类型来搜索:

GET /secisland/_mapping/type_one
  • 1

系统支持同时获取多个索引和类型的语法。
获取文档映射接口一次可以获取多个索引或文档映射类型。

格式如下:

GET /{index}/_mapping/{type}

{type}{index}可以接受逗号(,)分隔符,也可以使用_all来表示全部索引。
  • 1
  • 2
  • 3

获取字段映射

获取文档字段接口允许搜索一个或多个字段。这个用来搜索想要搜索的字段,而不是某个索引或者文档类型的全部内容。

示例:搜索secisland索引中type_one所有为text类型的字段

GET /secisland/_mapping/type_one/field/text
  • 1

返回:

{
  "secisland": {
    "mappings": {
      "type_one": {
        "text": {
          "full_name": "text",
          "mapping": {
            "text": {
              "type": "text",
              "analyzer": "standard",
              "search_analyzer": "whitespace"
            }
          }
        }
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

一次获取多个索引或文档映射类型:

GET /{index}/{type}/_mapping/field/{field}
  • 1

{index},{type},{field}可以使用逗号(,)分隔,也可以使用通配符。

GET /secisland,kimchy/_mapping/field/message
GET /_all/_mapping/secilog,book/field/message,user.id
GET /_all/_mapping/tw*/field/*.id
  • 1
  • 2
  • 3

指定字段的操作如下。获取文档字段接口,可以使用逗号(,)分隔符或者通配符(*)。

对象类型的字段可以使用(.)来指定具体字段。

GET /secisland/_mapping/article/field/author.id
  • 1

判断类型是否存在

检查索引或文档类型是否存在:

HEAD /secisland/secilog
  • 1

存在返回200,不存在返回404

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/853892
推荐阅读
相关标签
  

闽ICP备14008679号