[ElasticSearch]嵌套对象之嵌套类型
nested
类型是一种特殊的对象object
数据类型(specialised version of the object datatype ),允许对象数组彼此独立地进行索引和查询。
1. 对象数组如何扁平化
内部对象object
字段的数组不能像我们所期望的那样工作。 Lucene没有内部对象的概念,所以Elasticsearch将对象层次结构扁平化为一个字段名称和值的简单列表。 例如,以下文件:
curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d' { "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
的查询:
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "bool": { "must": [ { "match": { "user.first": "Alice" }}, { "match": { "user.last": "Smith" }} ] } } } '
2. 对对象数组使用嵌套字段
如果需要索引对象数组并维护数组中每个对象的独立性,则应使用nested
数据类型而不是object
数据类型。 在内部,嵌套对象将数组中的每个对象作为单独的隐藏文档进行索引,这意味着每个嵌套对象都可以使用嵌套查询nested query
独立于其他对象进行查询:
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d' { "mappings": { "my_type": { "properties": { "user": { "type": "nested" } } } } } ' curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d' { "group" : "fans", "user" : [ { "first" : "John", "last" : "Smith" }, { "first" : "Alice", "last" : "White" } ] } '
说明
user
字段映射为nested
类型,而不是默认的object
类型
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "nested": { "path": "user", "query": { "bool": { "must": [ { "match": { "user.first": "Alice" }}, { "match": { "user.last": "Smith" }} ] } } } } } '
说明
此查询得不到匹配,是因为Alice
和Smith
不在同一个嵌套对象中。
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d' { "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": 6, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 1.987628, "hits": [ { "_index": "my_index", "_type": "my_type", "_id": "1", "_score": 1.987628, "_source": { "group": "fans", "user": [ { "first": "John", "last": "Smith" }, { "first": "Alice", "last": "White" } ] }, "inner_hits": { "user": { "hits": { "total": 1, "max_score": 1.987628, "hits": [ { "_index": "my_index", "_type": "my_type", "_id": "1", "_nested": { "field": "user", "offset": 1 }, "_score": 1.987628, "_source": { "first": "Alice", "last": "White" }, "highlight": { "user.first": [ "<em>Alice</em>" ] } } ] } } } } ] } }
嵌套文档可以:
- 使用nested查询进行查询
- 使用nested和reverse_nested聚合进行分析
- 使用nested排序进行排序
- 使用nested inner hits进行检索与突出显示
3. 嵌套字段参数
嵌套字段接受以下参数:
参数 | 描述 |
---|---|
dynamic | 是否将新属性动态添加到现有的嵌套对象。共有true (默认),false 和strict 三种参数。 |
include_in_all | Sets the default include_in_all value for all the properties within the nested object. Nested documents do not have their own _all field. Instead, values are added to the _all field of the main “root” document. |
properties | 嵌套对象中的字段,可以是任何数据类型,包括嵌套。新的属性可能会添加到现有的嵌套对象。 |
备注
类型映射(type mapping
)、对象字段和嵌套字段包含的子字段,称之为属性properties
。这些属性可以为任意数据类型,包括object
和 nested
。属性可以通过以下方式加入:
- 当在创建索引时显式定义他们。
- 当使用
PUT mapping API
添加或更新映射类型时显式地定义他们。 - 当索引包含新字段的文档时动态的加入。
重要
由于嵌套文档作为单独的文档进行索引,因此只能在nested
查询,nested
/reverse_nested
聚合或者 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/nested.html#nested-params

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
用Spark和DBSCAN对地理定位数据进行聚类
机器学习,特别是聚类算法,可以用来确定哪些地理区域经常被一个用户访问和签到而哪些区域不是。这样的地理分析使多种服务成为可能,比如基于地理位置的推荐系统,先进的安全系统,或更通常来说,提供更个性化的用户体验。 在这篇文章中,我会确定对每个人来说特定的地理活动区域,讨论如何从大量的定位事件中(比如在餐厅或咖啡馆的签到)获取用户的活动区域来构建基于位置的服务。举例来说,这种系统可以识别一个用户经常外出吃晚饭的区域。 使用DBSCAN聚类算法 首先,我们需要选择一种适用于定位数据的聚类算法,可以基于提供的数据点的局部密度确定用户的活动区域。DBSCAN算法是一个不错的选择,因为它自下而上地选择一个点并在一个给定的距离寻找更多的点。然后通过重复这个过程扩展寻找新的点来扩展类簇,直到无法再扩大为止。 这个算法可以通过两个参数进行调试: ε,用来确定离
- 下一篇
mongo-connector导入数据到Elasticsearch
当前测试环境下Elasticsearch版本为2.3。不同版本的mongo-connector、elastic-doc-manager/elastic2-doc-manager所支持的Elasticsearch版本不同,安装时注意版本的选择。 安装mongo-connector 测试机上Python的默认版本为2.6,由于我采用anonacoda作为Python开发环境,Python默认版本2.7。故使用pip2.7而不是pip命令。 # 安装mongo-connector(当前版本为2.3) ./CONDA-HOME/bin/pip2.7 install mongo-connector # elastic2-doc-manager (当前版本为0.1.0) ./CONDA-HOME/bin/pip2.7 install elastic2-doc-manager 1 2 3 4 5 6 1 2 3 4 5 6 导入mongodb中的数据到Elasticsearch集群 在安装了monog-connector的机子的命令行中执行下面的命令(该命令为测试时的真实命令)。 mongo-con...
相关文章
文章评论
共有0条评论来说两句吧...