文章更新对网站有什么好处,数字营销的优势有哪些,微信公众平台官方网站登录,html5响应式网站建设Retrieve inner hits 是 Elasticsearch 中的一个功能#xff0c;用于在嵌套查询或父子查询中#xff0c;返回导致主文档匹配的具体嵌套对象或子/父文档的详细信息#xff0c;帮助用户更直观地理解查询结果的来源。
在 Elasticsearch 中#xff0c;Retrieve inner hits是一…Retrieve inner hits 是 Elasticsearch 中的一个功能用于在嵌套查询或父子查询中返回导致主文档匹配的具体嵌套对象或子/父文档的详细信息帮助用户更直观地理解查询结果的来源。
在 Elasticsearch 中Retrieve inner hits是一个功能强大的特性用于在嵌套查询nested或父子查询has_child/has_parent中检索匹配的嵌套对象或子/父文档。它允许用户不仅能看到主文档的匹配还能看到导致主文档匹配的具体嵌套对象或子/父文档。
1.什么是inner_hits
inner_hits的主要作用是返回导致主文档匹配的具体嵌套对象或子/父文档。在嵌套查询中主文档可能包含多个嵌套对象而inner_hits可以明确指出是哪些嵌套对象导致了主文档的匹配。
2.使用场景
假设你有一个包含嵌套对象的文档结构例如
json
PUT test/_doc/1?refresh
{ title: Test title, comments: [ { author: kimchy, number: 1 }, { author: nik9000, number: 2 } ]
} 如果你希望查询number字段为2的评论并且想看到是哪个评论导致了主文档的匹配可以使用inner_hits。
3.查询示例
以下是一个使用inner_hits的查询示例
json
POST test/_search
{ query: { nested: { path: comments, query: { match: { comments.number: 2 } }, inner_hits: {} // 添加 inner_hits } }
} 4.响应结构
查询的响应将包含inner_hits部分明确指出匹配的嵌套对象
json
{ took: 1, timed_out: false, _shards: { total: 1, successful: 1, skipped: 0, failed: 0 }, hits: { total: { value: 1, relation: eq }, max_score: 1.0, hits: [ { _index: test, _type: _doc, _id: 1, _score: 1.0, _source: { title: Test title, comments: [ { author: kimchy, number: 1 }, { author: nik9000, number: 2 } ] }, inner_hits: { comments: { hits: { total: { value: 1, relation: eq }, max_score: 1.0, hits: [ { _index: test, _id: 1, _nested: { field: comments, offset: 1 }, _score: 1.0, _source: { author: nik9000, number: 2 } } ] } } } } ] }
} 在这个响应中
• 主文档_id为1的文档被检索出来。
• inner_hits明确指出了是哪个嵌套对象{author: nik9000, number: 2}导致了主文档的匹配。
5.性能优化
为了优化性能可以设置_source: false并使用docvalue_fields避免解析_source
json
POST test/_search
{ query: { nested: { path: comments, query: { match: { comments.number: 2 } }, inner_hits: { _source: false, docvalue_fields: [comments.number] } } }
} 这种方式可以减少查询的解析时间和响应大小。
6.不使用inner_hits的区别
如果不使用inner_hits查询只会返回主文档的_source而不会明确指出是哪个嵌套对象导致了匹配。例如
json
POST test/_search
{ query: { nested: { path: comments, query: { match: { comments.number: 2 } } } }
} 响应中将不包含inner_hits部分只返回主文档的内容。
7.总结
• inner_hits的作用明确指出导致主文档匹配的具体嵌套对象或子/父文档。
• 性能优化通过设置_source: false和docvalue_fields可以减少查询的解析时间和响应大小。
• 适用场景当你需要调试查询或分析具体是哪些嵌套对象导致了主文档匹配时inner_hits是非常有用的工具。
希望这些信息能帮助你更好地理解和使用 Elasticsearch 的Retrieve inner hits功能