属性参数(Properties)

Properties 参数 #

properties 参数用于定义对象(object)和嵌套(nested)类型字段的子字段映射。它是构建层级文档结构的核心参数。

相关指南 #

使用位置 #

properties 出现在三个层级:

层级说明
mappings.properties顶层字段定义
mappings.properties.<object>.properties对象字段的子字段
mappings.properties.<nested>.properties嵌套字段的子字段

示例 #

基本层级结构 #

PUT my-index
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "address": {
        "type": "object",
        "properties": {
          "city": { "type": "keyword" },
          "zip": { "type": "keyword" }
        }
      }
    }
  }
}

嵌套类型 #

PUT my-index
{
  "mappings": {
    "properties": {
      "comments": {
        "type": "nested",
        "properties": {
          "author": { "type": "keyword" },
          "content": { "type": "text" },
          "date": { "type": "date" }
        }
      }
    }
  }
}

多层嵌套 #

PUT my-index
{
  "mappings": {
    "properties": {
      "department": {
        "properties": {
          "name": { "type": "keyword" },
          "manager": {
            "properties": {
              "name": { "type": "text" },
              "email": { "type": "keyword" }
            }
          }
        }
      }
    }
  }
}

对应的文档字段以 . 分隔:department.manager.email

object 与 nested 的区别 #

特性object(默认)nested
内部对象独立性数组中的对象会被扁平化,失去关联关系每个对象独立存储,保持内部字段关联
查询方式普通查询需要使用 nested 查询
性能更快较慢(每个嵌套对象作为独立 Lucene 文档)
适用场景非数组对象或不需要精确关联的数组需要精确匹配数组中特定对象内字段关联

注意事项 #

  • properties 不需要指定 type,它本身就是对象结构的定义方式
  • 省略 type 时,字段自动被视为 object 类型
  • 已有字段可以新增子字段(通过 Update Mapping API),但已有子字段的类型不可更改
  • 嵌套对象过多会影响索引和查询性能,建议通过 index.mapping.nested_objects.limit 控制数量