properties

编辑

类型映射、object 字段nested 字段 包含子字段,称为 properties。这些属性可以是任何 数据类型,包括 objectnested。属性可以

  • 创建索引 时通过显式定义来添加。
  • 使用 更新映射 API 添加或更新映射类型时,通过显式定义来添加。
  • 仅通过索引包含新字段的文档来 动态 添加。

下面是一个向映射类型、object 字段和 nested 字段添加 properties 的示例

resp = client.indices.create(
    index="my-index-000001",
    mappings={
        "properties": {
            "manager": {
                "properties": {
                    "age": {
                        "type": "integer"
                    },
                    "name": {
                        "type": "text"
                    }
                }
            },
            "employees": {
                "type": "nested",
                "properties": {
                    "age": {
                        "type": "integer"
                    },
                    "name": {
                        "type": "text"
                    }
                }
            }
        }
    },
)
print(resp)

resp1 = client.index(
    index="my-index-000001",
    id="1",
    document={
        "region": "US",
        "manager": {
            "name": "Alice White",
            "age": 30
        },
        "employees": [
            {
                "name": "John Smith",
                "age": 34
            },
            {
                "name": "Peter Brown",
                "age": 26
            }
        ]
    },
)
print(resp1)
response = client.indices.create(
  index: 'my-index-000001',
  body: {
    mappings: {
      properties: {
        manager: {
          properties: {
            age: {
              type: 'integer'
            },
            name: {
              type: 'text'
            }
          }
        },
        employees: {
          type: 'nested',
          properties: {
            age: {
              type: 'integer'
            },
            name: {
              type: 'text'
            }
          }
        }
      }
    }
  }
)
puts response

response = client.index(
  index: 'my-index-000001',
  id: 1,
  body: {
    region: 'US',
    manager: {
      name: 'Alice White',
      age: 30
    },
    employees: [
      {
        name: 'John Smith',
        age: 34
      },
      {
        name: 'Peter Brown',
        age: 26
      }
    ]
  }
)
puts response
const response = await client.indices.create({
  index: "my-index-000001",
  mappings: {
    properties: {
      manager: {
        properties: {
          age: {
            type: "integer",
          },
          name: {
            type: "text",
          },
        },
      },
      employees: {
        type: "nested",
        properties: {
          age: {
            type: "integer",
          },
          name: {
            type: "text",
          },
        },
      },
    },
  },
});
console.log(response);

const response1 = await client.index({
  index: "my-index-000001",
  id: 1,
  document: {
    region: "US",
    manager: {
      name: "Alice White",
      age: 30,
    },
    employees: [
      {
        name: "John Smith",
        age: 34,
      },
      {
        name: "Peter Brown",
        age: 26,
      },
    ],
  },
});
console.log(response1);
PUT my-index-000001
{
  "mappings": {
    "properties": { 
      "manager": {
        "properties": { 
          "age":  { "type": "integer" },
          "name": { "type": "text"  }
        }
      },
      "employees": {
        "type": "nested",
        "properties": { 
          "age":  { "type": "integer" },
          "name": { "type": "text"  }
        }
      }
    }
  }
}

PUT my-index-000001/_doc/1 
{
  "region": "US",
  "manager": {
    "name": "Alice White",
    "age": 30
  },
  "employees": [
    {
      "name": "John Smith",
      "age": 34
    },
    {
      "name": "Peter Brown",
      "age": 26
    }
  ]
}

顶级映射定义中的属性。

manager 对象字段下的属性。

employees 嵌套字段下的属性。

与上述映射相对应的示例文档。

properties 设置允许在同一索引中对同名字段具有不同的设置。可以使用 更新映射 API 将新属性添加到现有字段。

点表示法

编辑

可以使用点表示法在查询、聚合等中引用内部字段。

resp = client.search(
    index="my-index-000001",
    query={
        "match": {
            "manager.name": "Alice White"
        }
    },
    aggs={
        "Employees": {
            "nested": {
                "path": "employees"
            },
            "aggs": {
                "Employee Ages": {
                    "histogram": {
                        "field": "employees.age",
                        "interval": 5
                    }
                }
            }
        }
    },
)
print(resp)
response = client.search(
  index: 'my-index-000001',
  body: {
    query: {
      match: {
        'manager.name' => 'Alice White'
      }
    },
    aggregations: {
      "Employees": {
        nested: {
          path: 'employees'
        },
        aggregations: {
          "Employee Ages": {
            histogram: {
              field: 'employees.age',
              interval: 5
            }
          }
        }
      }
    }
  }
)
puts response
const response = await client.search({
  index: "my-index-000001",
  query: {
    match: {
      "manager.name": "Alice White",
    },
  },
  aggs: {
    Employees: {
      nested: {
        path: "employees",
      },
      aggs: {
        "Employee Ages": {
          histogram: {
            field: "employees.age",
            interval: 5,
          },
        },
      },
    },
  },
});
console.log(response);
GET my-index-000001/_search
{
  "query": {
    "match": {
      "manager.name": "Alice White"
    }
  },
  "aggs": {
    "Employees": {
      "nested": {
        "path": "employees"
      },
      "aggs": {
        "Employee Ages": {
          "histogram": {
            "field": "employees.age",
            "interval": 5
          }
        }
      }
    }
  }
}

必须指定内部字段的完整路径。