properties
编辑properties
编辑类型映射、object
字段 和 nested
字段 包含子字段,称为 properties
。这些属性可以是任何 数据类型,包括 object
和 nested
。属性可以
下面是一个向映射类型、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 } ] }
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 } } } } } }
必须指定内部字段的完整路径。