Inner hits

The parent-join and nested 功能允许返回具有不同范围匹配的文档。在父/子案例中,基于子文档中的匹配返回父文档,或者基于父文档中的匹配返回子文档。在嵌套的情况下,基于嵌套内部对象中的匹配返回文档。 在这两种情况下,隐藏了导致文档返回的不同范围中的实际匹配。在许多情况下,知道哪些内部嵌套对象(在嵌套的情况下)或子/父文档(在父/子的情况下)返回某些信息非常有用。内部命中功能可用于此目的。此功能会在搜索响应中返回每次搜索命中,这会导致搜索匹配在不同范围内匹配。

可以通过在nested,has_child或has_parent查询和过滤器上定义inner_hits定义来使用内部命中。结构如下所示:

"<query>" : {
"inner_hits" : {
<inner_hits_options>
}
}

如果inner_hits在支持它的查询上定义,则每个搜索命中将包含inner_hits具有以下结构的json对象:

"hits": [
{
"_index": ...,
"_type": ...,
"_id": ...,
"inner_hits": {
"<inner_hits_name>": {
"hits": {
"total": ...,
"hits": [
{
"_type": ...,
"_id": ...,
...
},
...
]
}
}
},
...
},
...
]

选项

内部命中支持以下选项:

from

inner_hits返回的常规搜索中 每个第一次点击获取的偏移量。

size

每个返回的最大匹配数inner_hits。默认情况下,返回前三个匹配的匹配。

sort

应如何对内部命中进行排序inner_hits。默认情况下,命中按分数排序。

name

用于响应中特定内部命中定义的名称。在单个搜索请求中定义了多个内部命中时很有用。默认值取决于定义内部命中的查询。对于has_child查询和过滤,这是子类型,has_parent查询和过滤器这是父类型,嵌套查询和过滤器这是嵌套路径。

内部命中还支持以下每个文档功能:

Nested inner hits

嵌套的inner_hits可用于包括嵌套的内部对象作为搜索命中的内部命中。

PUT test
{
"mappings": {
"_doc": {
"properties": {
"comments": {
"type": "nested"
}
}
}
}
} PUT test/_doc/1?refresh
{
"title": "Test title",
"comments": [
{
"author": "kimchy",
"number": 1
},
{
"author": "nik9000",
"number": 2
}
]
} POST test/_search
{
"query": {
"nested": {
"path": "comments",
"query": {
"match": {"comments.number" : 2}
},
"inner_hits": {} ①
}
}
}

嵌套查询中的内部命中定义。没有其他选择需要定义。

可以从上述搜索请求生成的响应代码段示例:

{
...,
"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": ...,
"inner_hits": {
"comments": { ①
"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_nested": {
"field": "comments",
"offset": 1
},
"_score": 1.0,
"_source": { ②
"author": "nik9000",
"number": 2
}
}
]
}
}
}
}
]
}
}

搜索请求中内部匹配定义中使用的名称。可以通过该name选项使用自定义键。

在上述示例中,嵌套元数据是至关重要的,因为它定义了从内部嵌套对象中产生的内部嵌套对象。字段定义嵌套命中的对象数组字段和相对于其在源中的位置的偏移量。由于排序和评分,inner_hits中命中对象的实际位置通常与定义嵌套内部对象的位置不同。

默认情况下,在内命中命中对象也返回了_source,但这可以被改变。通过源过滤功能,可以返回或禁用源的一部分。如果在嵌套级别上定义了存储字段,那么这些字段也可以通过字段特性返回。

一个重要的默认值是,在内射命中中的命中返回的_source与嵌套的元数据相对应。因此,在上面的示例中,每次嵌套命中只返回注释部分,而不返回包含注释的顶级文档的整个源。

Hierarchical levels of nested object fields and inner hits.

如果映射具有多级分层嵌套对象字段,则可以通过点标记路径访问每个级别。例如,如果存在comments包含votes嵌套字段的嵌套字段,并且应该直接返回带有根命中的投票,则可以定义以下路径:

PUT test
{
"mappings": {
"_doc": {
"properties": {
"comments": {
"type": "nested",
"properties": {
"votes": {
"type": "nested"
}
}
}
}
}
}
} PUT test/_doc/1?refresh
{
"title": "Test title",
"comments": [
{
"author": "kimchy",
"text": "comment text",
"votes": []
},
{
"author": "nik9000",
"text": "words words words",
"votes": [
{"value": 1 , "voter": "kimchy"},
{"value": -1, "voter": "other"}
]
}
]
} POST test/_search
{
"query": {
"nested": {
"path": "comments.votes",
"query": {
"match": {
"comments.votes.voter": "kimchy"
}
},
"inner_hits" : {}
}
}
}

看起来像是这样的:

{
...,
"hits": {
"total": 1,
"max_score": 0.6931472,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_score": 0.6931472,
"_source": ...,
"inner_hits": {
"comments.votes": {
"hits": {
"total": 1,
"max_score": 0.6931472,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_nested": {
"field": "comments",
"offset": 1,
"_nested": {
"field": "votes",
"offset": 0
}
},
"_score": 0.6931472,
"_source": {
"value": 1,
"voter": "kimchy"
}
}
]
}
}
}
}
]
}
}

仅对嵌套的内部命中支持此间接引用。

Parent/child inner hits

父/子inner_hits可以用于包括父或子:

PUT test
{
"mappings": {
"_doc": {
"properties": {
"my_join_field": {
"type": "join",
"relations": {
"my_parent": "my_child"
}
}
}
}
}
} PUT test/_doc/1?refresh
{
"number": 1,
"my_join_field": "my_parent"
} PUT test/_doc/2?routing=1&refresh
{
"number": 1,
"my_join_field": {
"name": "my_child",
"parent": "1"
}
} POST test/_search
{
"query": {
"has_child": {
"type": "my_child",
"query": {
"match": {
"number": 1
}
},
"inner_hits": {}
}
}
}

内部命中定义,如嵌套示例中所示。

{
...,
"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"number": 1,
"my_join_field": "my_parent"
},
"inner_hits": {
"my_child": {
"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_routing": "1",
"_source": {
"number": 1,
"my_join_field": {
"name": "my_child",
"parent": "1"
}
}
}
]
}
}
}
}
]
}
}

elasticsearch 基础 —— Inner hits的更多相关文章

  1. ELK(elasticsearch+kibana+logstash)搜索引擎(二): elasticsearch基础教程

    1.elasticsearch的结构 首先elasticsearch目前的结构为 /index/type/id  id对应的就是存储的文档ID,elasticsearch一般将数据以JSON格式存储. ...

  2. Elasticsearch 基础入门

    原文地址:Elasticsearch 基础入门 博客地址:http://www.extlight.com 一.什么是 ElasticSearch ElasticSearch是一个基于 Lucene 的 ...

  3. ElasticSearch 基础 1

    ElasticSearch 基础=============================== 索引创建 ========================== 1. RESTFUL APIAPI 基本 ...

  4. Elasticsearch基础但非常有用的功能之二:模板

    文章转载自: https://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247484584&idx=1&sn=accfb65 ...

  5. 最完整的Elasticsearch 基础教程

    翻译:潘飞(tinylambda@gmail.com) 基础概念 Elasticsearch有几个核心概念.从一开始理解这些概念会对整个学习过程有莫大的帮助. 接近实时(NRT)        Ela ...

  6. ELK 之一:ElasticSearch 基础和集群搭建

    一:需求及基础: 场景: 1.开发人员不能登录线上服务器查看详细日志 2.各个系统都有日志,日志数据分散难以查找 3.日志数据量大,查询速度慢,或者数据不够实时 4.一个调用会涉及到多个系统,难以在这 ...

  7. Elasticsearch基础教程

    Reference: http://blog.csdn.net/cnweike/article/details/33736429 基础概念 Elasticsearch有几个核心概念.从一开始理解这些概 ...

  8. elasticsearch 基础知识汇总

    索引分片: 从策略层面,控制分片分配的选择 磁盘限额 为了保护节点数据安全,ES 会定时(cluster.info.update.interval,默认 30 秒)检查一下各节点的数据目录磁盘使用情况 ...

  9. 搜索引擎框架之ElasticSearch基础详解(非原创)

    文章大纲 一.搜索引擎框架基础介绍二.ElasticSearch的简介三.ElasticSearch安装(Windows版本)四.ElasticSearch操作客户端工具--Kibana五.ES的常用 ...

随机推荐

  1. CSS3属性之 target伪类实现Tab切换效果

    CSS3 :target伪类用来改变页面中锚链接URL所指向的ID样式 代码示例: <!DOCTYPE html> <html lang="en"> < ...

  2. 如何搭建一个spring boot项目

    什么是springboot? Spring Boot俗称微服务.Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特 ...

  3. Clean Docker <none>:<none>

    https://www.projectatomic.io/blog/2015/07/what-are-docker-none-none-images/ Reference: https://www.p ...

  4. Hadoop 学习目录(搁置)

    简介 Hadoop是一个由Apache基金会所开发的分布式系统基础架构.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储.Hadoop实现了一个分布式文件系 ...

  5. URL跳转漏洞

    URL跳转原理: 由于越来越多的需要和其他第三方应用交互,以及在自身应用内部根据不同的逻辑将用户引向到不同的页面,譬如一个典型的登录接口就经常需要在认证成功之后将用户引导到登录之前的页面,整个过程中如 ...

  6. php next()函数 语法

    php next()函数 语法 作用:将内部指针指向数组中的下一个元素,并输出.直线电机滑台 语法:next(array) 参数: 参数 描述 array 必需.规定要使用的数组. 说明:在返回值之前 ...

  7. [14th CSMO Day 1 <平面几何>]

    关于LowBee苦思冥想的结果(仅供参考):

  8. laravel5.6 操作数据 Eloquent ORM

    建立Users模型 <?php namespace App\Model\Eloquent\Admin; use Illuminate\Database\Eloquent\Model; class ...

  9. Dpr ppi 适配 等概念 弹性属性的讲解

    Dpr: Dpr的全称(Device pixel ratio)像素设备比例:就是说每个设备像素上占有的css位像素的个数 苹果手机常见的设备像素比:1.0安卓 iPhone2.0  3.0 如果是1. ...

  10. Linux 系统故障修复和修复技巧

    我发现Linux系统在启动过程中会出现一些故障,导致系统无法正常启动,我在这里写了几个应用单用户模式.GRUB命令操作.Linux救援模式的故障修复案例帮助大家了解此类问题的解决. 一.单用户模式 L ...