数组

编辑

在 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",
    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" 
    }
  }
}

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

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

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

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