nested类型是一种特殊的对象object数据类型(specialised version of the object datatype
),允许对象数组彼此独立地进行索引和查询。

<>1. 对象数组如何扁平化

内部对象object字段的数组不能像我们所期望的那样工作。
Lucene没有内部对象的概念,所以Elasticsearch将对象层次结构扁平化为一个字段名称和值的简单列表。 例如,以下文件:
PUT my_index/_doc/1 { "group" : "fans", "user" : [ { "first" : "John", "last"
: "Smith" }, { "first" : "Alice", "last" : "White" } ] }
说明

user字段被动态的添加为object类型的字段。

在内部其转换成一个看起来像下面这样的文档:
{ "group" : "fans", "user.first" : [ "alice", "john" ], "user.last" : [
"smith", "white" ] }
user.first和user.last字段被扁平化为多值字段,并且alice和white之间的关联已经丢失。 本文档将错误地匹配user.first为
alice和user.last为smith的查询:
GET my_index/_search { "query": { "bool": { "must": [ { "match": {
"user.first": "Alice" }}, { "match": { "user.last": "Smith" }} ] } } }
<>

2. 对对象数组使用嵌套字段

如果需要索引对象数组并维护数组中每个对象的独立性,则应使用nested数据类型而不是object数据类型。
在内部,嵌套对象将数组中的每个对象作为单独的隐藏文档进行索引,这意味着每个嵌套对象都可以使用嵌套查询nested query独立于其他对象进行查询:
PUT my_index { "mappings": { "_doc": { "properties": { "user": { "type":
"nested" } } } } } PUT my_index/_doc/1 { "group" : "fans", "user" : [ { "first"
: "John", "last" : "Smith" }, { "first" : "Alice", "last" : "White" } ] }
说明

user字段映射为nested类型,而不是默认的object类型
GET my_index/_search { "query": { "nested": { "path": "user", "query": {
"bool": { "must": [ { "match": { "user.first": "Alice" }}, { "match": {
"user.last": "Smith" }} ] } } } } }


说明

此查询得不到匹配,是因为Alice和Smith不在同一个嵌套对象中。
GET my_index/_search { "query": { "nested": { "path": "user", "query": {
"bool": { "must": [ { "match": { "user.first": "Alice" }}, { "match": {
"user.last": "White" }} ] } }, "inner_hits": { "highlight": { "fields": {
"user.first": {} } } } } } }


说明

此查询得到匹配,是因为Alice和White位于同一个嵌套对象中。

inner_hits允许我们突出显示匹配的嵌套文档。

输出
{ "took": 151, "timed_out": false, "_shards": { "total": 5, "successful": 5,
"skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 1.3862944,
"hits": [ { "_index": "indextest010", "_type": "my_type", "_id": "1", "_score":
1.3862944, "_source": { "group": "fans", "user": [ { "first": "John", "last":
"Smith" }, { "first": "Alice", "last": "White" } ] }, "inner_hits": { "user": {
"hits": { "total": 1, "max_score": 1.3862944, "hits": [ { "_index":
"indextest010", "_type": "my_type", "_id": "1", "_nested": { "field": "user",
"offset": 1 }, "_score": 1.3862944, "_source": { "first": "Alice", "last":
"White" }, "highlight": { "user.first": [ "<em>Alice</em>" ] } } ] } } } } ] } }
嵌套文档可以:

* 使用nested
<https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html>
查询进行查询
* 使用nested
<https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html>
和reverse_nested
<https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html>
聚合进行分析
* 使用nested
<https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#nested-sorting>
排序进行排序
* 使用nested inner hits
<https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#nested-inner-hits>
进行检索与突出显示 PUT my_index { "mappings": { "_doc": { "properties": { "user": {
"type": "nested", "include_in_parent": true, "include_in_root": true } } } } }

嵌套字段可以包含其他嵌套字段。include_in_parent对象引用字段的直接父对象,而include_in_root参数只引用最顶部的“root”对象或文档。

include_in_parent和include_in_root选项不适用于geo_shape字段,这些字段仅在嵌套文档中索引。

include_in_root:true嵌套文档将仅自动使用根文档_all字段。

<>3. 嵌套字段参数

嵌套字段接受以下参数:

参数 描述
dynamic
<https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic.html>
是否将新属性动态添加到现有的嵌套对象。共有true(默认),false和strict三种参数。
include_in_all
<https://www.elastic.co/guide/en/elasticsearch/reference/current/include-in-all.html>
为嵌套对象中的所有属性设置默认的include_in_all值。嵌套文档没有自己的_all字段。而是将值添加到主“根”文档的_all字段中。
properties
<https://www.elastic.co/guide/en/elasticsearch/reference/current/properties.html>
嵌套对象中的字段,可以是任何数据类型,包括嵌套。新的属性可能会添加到现有的嵌套对象。
备注

类型映射(type mapping)、对象字段和嵌套字段包含的子字段,称之为属性properties。这些属性可以为任意数据类型,包括object和 
nested。属性可以通过以下方式加入:

* 当在创建索引时显式定义他们。
* 当使用PUT mapping API添加或更新映射类型时显式地定义他们。
* 当索引包含新字段的文档时动态的加入。
重要

由于嵌套文档作为单独的文档进行索引,因此只能在nested查询,nested/reverse_nested聚合或者 nested inner hits
<https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#nested-inner-hits>
 的范围内进行访问。

For instance, if a string field within a nested document has index_options set
to offsets to allow use of the postings highlighter, these offsets will not be
available during the main highlighting phase. Instead, highlighting needs to be
performed via nested inner hits.

<>4. 限制嵌套字段的个数


索引一个拥有100个嵌套字段的文档,相当于索引了101个文档,因为每一个嵌套文档都被索引为一个独立的文档.为了防止不明确的映射,每个索引可以定义的嵌套字段的数量已被限制为50个。
具体请参阅 Settings to prevent mappings explosion
<https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html#mapping-limit-settings>

原文:
https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html#nested-params

<https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html#nested-params>

参考:


https://www.elastic.co/guide/cn/elasticsearch/guide/current/multi-match-query.html

<https://www.elastic.co/guide/cn/elasticsearch/guide/current/multi-match-query.html>

http://www.bubuko.com/infodetail-2153060.html
<http://www.bubuko.com/infodetail-2153060.html>

https://www.cnblogs.com/huangfox/p/3544883.html
<https://www.cnblogs.com/huangfox/p/3544883.html>

 

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信