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"
}
}
}
]
}
}
}
}
]
}
}

最新文章

  1. Python3.5 Day1作业:实现用户密码登录,输错三次锁定。
  2. OpenStack学习参考
  3. Entity Framework7 有哪些不同?之具体功能
  4. git怎么创建本地版本仓库
  5. 天龙客户端的GameManager
  6. UWP开发入门(十五)——在FlipView中通过手势操作图片
  7. css cursor属性详解
  8. Android的Adapter用法
  9. Vi的基本使用方法
  10. EF 实体关系
  11. Java基础知识强化之IO流笔记50:IO流练习之 复制多级文件夹的案例
  12. VBA:Google翻译(含tk算法)
  13. Java log4j的环境搭建
  14. mysql通过字段注释查找字段名称
  15. jdk安装 java环境配置
  16. 【CF429E】 Points and Segments(欧拉回路)
  17. C++的默认构造函数
  18. [译]RabbitMQ教程C#版 - &quot;Hello World&quot;
  19. 自己动手实现爬虫scrapy框架思路汇总
  20. 【每日一题】Flooded! UVA - 815 模拟阅读格式题

热门文章

  1. 浅谈ContextLoaderListener及其上下文与DispatcherServlet的区别
  2. Robomongo可视化命令
  3. 关于&lt;label&gt;的for属性的简单探索
  4. VUE的双向绑定及局部组件的使用
  5. OSI参考模型与TCP/IP参考模型与TCP/IP协议栈
  6. Flask学习笔记01之环境搭建
  7. linux运维、架构之路-网络基础
  8. 做网站用php还是python
  9. Word文档粘贴到DEDECMS
  10. SQL利用Case When Then Else End 多条件判断