elasticsearch - “[nested] 未能在路径 [steps] 下找到嵌套对象”
问题描述
我已将数据映射到此架构中:
curl -X PUT "localhost:9200/data?pretty" -H 'Content-Type: application/json' -d
'{
"settings":{
"number_of_shards":"1",
"number_of_replicas":"1"
},
"mappings":{
"properties":{
"routines":{
"type":"nested",
"properties":{
"title":{
"type":"text"
},
"sources":{
"type":"keyword"
},
"flags":{
"type":"keyword",
"null_value":"NULL"
},
"steps":{
"type":"nested",
"properties":{
"time":{
"type":"keyword"
},
"products":{
"type":"nested",
"properties":{
"name":{
"type":"text"
},
"link":{
"type":"keyword",
"null_value":"NULL"
},
"type":{
"type":"keyword",
"null_value":"NULL"
},
"ingredients":{
"type":"keyword",
"null_value":"NULL"
},
"flags":{
"type":"keyword",
"null_value":"NULL"
}
}
}
}
}
}
}
}
}
}'
现在,我正在尝试搜索两个字段data.title
并data.steps.products.name
使用以下查询:
curl -X GET "localhost:9200/data/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "data",
"query": {
"nested": {
"path": "steps",
"query": {
"nested": {
"path": "products",
"query": {
"multi_match": {
"query": "XXX",
"fields": [
"name"
]
}
}
}
}
}
}
}
},
{"multi_match": {
"query": "XXX",
"fields": [
"data.title"
]
}
}
]
}
}
}'
它抛出错误,它未能在步骤下找到路径:
{
"error" : {
"root_cause" : [
{
"type" : "query_shard_exception",
"reason" : "failed to create query: [nested] failed to find nested object under path [steps]",
"index_uuid" : "SjQgt4BHStC_APsMnZk8BQ",
"index" : "data"
}
],
"type" : "search_phase_execution_exception",
"reason" : "all shards failed",
"phase" : "query",
"grouped" : true,
"failed_shards" : [
{
"shard" : 0,
"index" : "data",
"node" : "L1azdi09QxanYGnP-y0xbQ",
"reason" : {
"type" : "query_shard_exception",
"reason" : "failed to create query: [nested] failed to find nested object under path [steps]",
"index_uuid" : "SjQgt4BHStC_APsMnZk8BQ",
"index" : "data",
"caused_by" : {
"type" : "illegal_state_exception",
"reason" : "[nested] failed to find nested object under path [steps]"
}
}
}
]
},
"status" : 400
}
你能帮忙找出映射/查询的错误吗?
更新:
我的 json 数据:https ://jsonkeeper.com/b/PSVS
解决方案
查看多级嵌套查询的 Elastic 文档。
您在查询中忘记的是每个子级嵌套查询中嵌套对象的完整路径。(另外,您使用了data
映射中不存在的字段,而您想要使用routines
)
所以你的查询看起来像
curl -X GET "localhost:9200/data/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "routines",
"query": {
"nested": {
"path": "routines.steps",
"query": {
"nested": {
"path": "routines.steps.products",
"query": {
"multi_match": {
"query": "XXX",
"fields": [
"routines.steps.products.name"
]
}
}
}
}
}
}
}
},
{"multi_match": {
"query": "XXX",
"fields": [
"routines.title"
]
}
}
]
}
}
}'
无论如何,请重新考虑首先拥有多级嵌套字段是否是一个好主意,因为它们对性能有重大影响。例如,有一个索引routines
,也许有一个data
标识符,有意义吗?
编辑:将完整路径添加到第一个应该块的多匹配字段
推荐阅读
- kubernetes-ingress - 将静态公共 IP 分配给 AKS 上的 HAProxy 以用于出口流量
- google-sheets - 列出日期范围之间的每个月。谷歌查询
- c++ - 我怎样才能让我的 arduino 睡 12 分钟?
- python - 在整个给定字符串中一致的重复子字符串模式——python
- python - 如何使用熊猫一次计算多列的值
- collections - Automapper - 将不同类的多个属性映射到单个属性
- rpmbuild - 使用 RPMBUILD 和介子系统
- python - 我能知道如何解决python中以下代码的错误吗
- tailwind-css - 与子 Tailwind 交互时在父级上应用样式
- scala - 如何在scala中模拟嵌套类