elasticsearch - 在 ElasticSearch 中包含嵌套字段作为结果项
问题描述
我有一个数据类型,其中包含与嵌套字段相同的数据类型列表。我想查询类型并从外部和内部项目中获取结果。这是我想要实现的一个小例子:
PUT /testindex?pretty=true
{
"mappings": {
"entry": {
"dynamic": false,
"properties": {
"description": {
"type": "text"
},
"entries": {
"type": "nested",
"dynamic": false,
"properties": {
"description": {
"type": "text"
}
}
}
}
}
}
}
PUT /testindex/entry/1?pretty=true
{
"id": 1,
"description": "",
"entries": [
{
"id": 1, "description": "lorem ipsum 11"
},
{
"id": 1, "description": "lorem ipsum 22"
},
{
"id": 1, "description": "dolor sit amet"
}
]
}
PUT /testindex/entry/2?pretty=true&refresh=true
{
"id": 2, "description": "lorem ipsum", "entries": []
}
父级有描述或列表。内部类型总是只有一个描述和父级的 Id。当我现在搜索时,我想得到总数 4。此外,当我搜索以“lorem”开头的描述时,结果应该包含 3 个项目。有可能吗?
解决方案
您需要使用下面的嵌套inner_hits
功能。
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"description": "lorem"
}
},
{
"nested": {
"path": "entries",
"query": {
"match_phrase": {
"entries.description": "lorem"
}
},
"inner_hits": {}
}
}
]
}
}
}
推荐阅读
- javascript - 使用 useContext 从数组中查找更新对象
- git - Ubuntu git pull 合并选项
- android - Android:Gradle build 不断拉下最新的 NDK 版本,而不是使用已经安装的版本
- python - 使用 Django Channels 立即发送消息
- java - Java - MongoDB 通过多个查询获得第一个结果的最佳方式
- reactjs - useEffect 不更新状态
- date-range - 如何在日期范围选择器上设置所选开始日期和结束日期之间的最短天数?
- css - 如何将两个模块并排放置在divi的同一列中?
- macos - WKWebView 显示白条,直到窗口移动/调整大小
- azure - 什么 Azure 托管会自动扩展 .NET Core Web API 服务?