原英文版地址: https://www.elastic.co/guide/en/elasticsearch/reference/7.7/array.html, 原文档版权归 www.elastic.co 所有
本地英文版地址: ../en/array.html
本地英文版地址: ../en/array.html
重要: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 当前版本文档。
在Elasticsearch中,没有专用的array(数组)数据类型。
默认情况下,任何字段都可以包含零个或多个值,但是数组中的所有值必须具有相同的数据类型。 例如:
-
一个字符串数组:[
"one","two"] -
一个整数数组:[
1,2] -
一个数组的数组:[
1, [2,3]],其等价于 [1,2,3] -
一个对象数组:[
{ "name": "Mary", "age": 12 },{ "name": "John", "age": 10 }]
对象数组
对象数组并不像能你期望的那样工作:不能独立于数组中的其他对象独立查询每个对象。
如果你需要能够这样做,那么应该使用nested数据类型而不是object(对象)数据类型。
更多详情在nested(嵌套)中解释了。
当动态添加字段时,数组中的第一个值决定字段的类型(type)。
所有后续的值必须是相同的数据类型,或者至少可以将后续的值强制转换为相同的数据类型。
不支持混合类型的数组:[ 10, "some string" ]
数组可能包含null值,这些值要么被配置的null_value替换,要么被完全跳过。
空数组[]被视为缺失字段,即没有值的字段。
不需要预先配置任何东西就可以在文档中使用数组,它们是开箱即用的:
PUT my_index/_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/_doc/2
{
"message": "no arrays in this document...",
"tags": "elasticsearch",
"lists": {
"name": "prog_list",
"description": "programming list"
}
}
GET my_index/_search
{
"query": {
"match": {
"tags": "elasticsearch"
}
}
}