属性
编辑properties
编辑
类型映射、object
字段 和 nested
字段 包含子字段,称为 properties
。这些属性可以是任何 数据类型,包括 object
和 nested
。属性可以
以下是如何将 properties
添加到映射类型、object
字段和 nested
字段的示例
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
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 将新属性添加到现有字段。
点表示法编辑
可以使用点表示法在查询、聚合等中引用内部字段
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
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 } } } } } }
必须指定内部字段的完整路径。