数组编辑

在 Elasticsearch 中,没有专门的 array 数据类型。默认情况下,任何字段都可以包含零个或多个值,但是数组中的所有值必须是相同的数据类型。例如

  • 字符串数组: [ "one", "two" ]
  • 整数数组: [ 1, 2 ]
  • 数组数组: [ 1, [ 2, 3 ]] 等同于 [ 1, 2, 3 ]
  • 对象数组: [ { "name": "Mary", "age": 12 }, { "name": "John", "age": 10 }]

对象数组

对象数组的工作方式与您预期不同:您无法独立于数组中的其他对象查询每个对象。如果您需要能够做到这一点,那么您应该使用 nested 数据类型而不是 object 数据类型。

这在 嵌套 中有更详细的解释。

在动态添加字段时,数组中的第一个值决定了字段 type。所有后续值必须是相同的数据类型,或者至少必须能够 强制 后续值转换为相同的数据类型。

不支持混合数据类型的数组: [ 10, "some string" ]

数组可以包含 null 值,这些值要么被配置的 null_value 替换,要么完全跳过。空数组 [] 被视为缺失字段——没有值的字段。

无需在文档中使用数组之前进行任何预配置,它们开箱即用

resp = 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"},
        ],
    },
)
print(resp)

resp = client.index(
    index="my-index-000001",
    id="2",
    body={
        "message": "no arrays in this document...",
        "tags": "elasticsearch",
        "lists": {"name": "prog_list", "description": "programming list"},
    },
)
print(resp)

resp = client.search(
    index="my-index-000001",
    body={"query": {"match": {"tags": "elasticsearch"}}},
)
print(resp)
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)
}
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" 
    }
  }
}

tags 字段被动态添加为 string 字段。

lists 字段被动态添加为 object 字段。

第二个文档不包含任何数组,但可以索引到相同的字段中。

查询在 tags 字段中查找 elasticsearch,并匹配两个文档。