数组
编辑数组
编辑在 Elasticsearch 中,没有专门的 array
数据类型。默认情况下,任何字段都可以包含零个或多个值,但是,数组中的所有值必须是相同的数据类型。例如:
- 字符串数组:[
"one"
,"two"
] - 整数数组:[
1
,2
] - 数组的数组:[
1
, [2
,3
]],等同于 [1
,2
,3
] - 对象数组:[
{ "name": "Mary", "age": 12 }
,{ "name": "John", "age": 10 }
]
当动态添加字段时,数组中的第一个值决定了字段的 type
。所有后续值必须具有相同的数据类型,或者至少可以将后续值强制转换为相同的数据类型。
不支持混合数据类型的数组:[ 10
, "some string"
]
数组可能包含 null
值,这些值要么被配置的 null_value
替换,要么完全跳过。空数组 []
被视为缺失字段 - 没有值的字段。
无需预先配置任何内容即可在文档中使用数组,它们是开箱即用的
resp = client.index( index="my-index-000001", id="1", document={ "message": "some arrays in this document...", "tags": [ "elasticsearch", "wow" ], "lists": [ { "name": "prog_list", "description": "programming list" }, { "name": "cool_list", "description": "cool stuff list" } ] }, ) print(resp) resp1 = client.index( index="my-index-000001", id="2", document={ "message": "no arrays in this document...", "tags": "elasticsearch", "lists": { "name": "prog_list", "description": "programming list" } }, ) print(resp1) resp2 = client.search( index="my-index-000001", query={ "match": { "tags": "elasticsearch" } }, ) print(resp2)
response = client.index( index: 'my-index-000001', id: 1, body: { message: 'some arrays in this document...', tags: [ 'elasticsearch', 'wow' ], lists: [ { name: 'prog_list', description: 'programming list' }, { name: 'cool_list', description: 'cool stuff list' } ] } ) puts response response = client.index( index: 'my-index-000001', id: 2, body: { message: 'no arrays in this document...', tags: 'elasticsearch', lists: { name: 'prog_list', description: 'programming list' } } ) puts response response = client.search( index: 'my-index-000001', body: { query: { match: { tags: 'elasticsearch' } } } ) puts response
{ res, err := es.Index( "my-index-000001", strings.NewReader(`{ "message": "some arrays in this document...", "tags": [ "elasticsearch", "wow" ], "lists": [ { "name": "prog_list", "description": "programming list" }, { "name": "cool_list", "description": "cool stuff list" } ] }`), es.Index.WithDocumentID("1"), es.Index.WithPretty(), ) fmt.Println(res, err) } { res, err := es.Index( "my-index-000001", strings.NewReader(`{ "message": "no arrays in this document...", "tags": "elasticsearch", "lists": { "name": "prog_list", "description": "programming list" } }`), es.Index.WithDocumentID("2"), es.Index.WithPretty(), ) fmt.Println(res, err) } { res, err := es.Search( es.Search.WithIndex("my-index-000001"), es.Search.WithBody(strings.NewReader(`{ "query": { "match": { "tags": "elasticsearch" } } }`)), es.Search.WithPretty(), ) fmt.Println(res, err) }
const response = await client.index({ index: "my-index-000001", id: 1, document: { message: "some arrays in this document...", tags: ["elasticsearch", "wow"], lists: [ { name: "prog_list", description: "programming list", }, { name: "cool_list", description: "cool stuff list", }, ], }, }); console.log(response); const response1 = await client.index({ index: "my-index-000001", id: 2, document: { message: "no arrays in this document...", tags: "elasticsearch", lists: { name: "prog_list", description: "programming list", }, }, }); console.log(response1); const response2 = await client.search({ index: "my-index-000001", query: { match: { tags: "elasticsearch", }, }, }); console.log(response2);
PUT my-index-000001/_doc/1 { "message": "some arrays in this document...", "tags": [ "elasticsearch", "wow" ], "lists": [ { "name": "prog_list", "description": "programming list" }, { "name": "cool_list", "description": "cool stuff list" } ] } PUT my-index-000001/_doc/2 { "message": "no arrays in this document...", "tags": "elasticsearch", "lists": { "name": "prog_list", "description": "programming list" } } GET my-index-000001/_search { "query": { "match": { "tags": "elasticsearch" } } }