Elasticsearch:获取路径下的嵌套对象不是嵌套类型

Sur*_*esh 1 java elasticsearch

我是弹性搜索世界的新手。基本上,我尝试根据 ID 检索嵌套对象。这是我的文档的 JSON 表示形式。

 {
"_index": "xyz",
"_type": "abc",
"_id": "12",
"_version": 1,
"found": true,
"_source":
{
    "lastModifiedBy": "12",
    "lastModifiedDate": "2015-12-31T19:45:29.493Z",
    "profile":
    [
        {
            "type": "nested",
            "views":
            [

                {
                    "type": "nested",
                    "id": "view1",
                    "name": "view1",
                    "properties":
                    [
                        {
                            "name": "default",
                            "value": false
                        }
                    ],
                    "widgets":
                    [
                        {
                            "type": "nested",
                            "id": "graph",
                            "name": "graph",
                            "elementId": "ui_graph",
                            "properties":
                            [
                                {
                                    "name": "currency",
                                    "value": "YEN"
                                }
                            ]
                        }

                    ]
                }
 }    ]    }    ] 
Run Code Online (Sandbox Code Playgroud)

我正在尝试根据视图 ID 获取小部件。这是我的搜索查询。

"query" : {
"term" : {
  "_id" : "12"
}
 },
"post_filter" : {
"nested" : {
  "query" : {
    "filtered" : {
      "query" : {
        "match_all" : { }
      },
      "filter" : {
        "term" : {
          "profile.views.id" : "view1"
        }
      }
    }
  },
  "path" : "profile.views"
 }
}
}
Run Code Online (Sandbox Code Playgroud)

我不确定这里出了什么问题。但是得到“路径 [profile.views] 下的嵌套对象不是嵌套类型]”。下面是我的映射结构

     {
  "xyz": {
     "mappings": {
  "abc": {
    "properties": {
      "lastModifiedBy": {
        "type": "string"
      },
      "lastModifiedDate": {
        "type": "date",
        "format": "dateOptionalTime"
      },
      "name": {
        "type": "string"
      },
      "profile": {
        "properties": {
          "lastModifiedBy": {
            "type": "string"
          },
          "lastModifiedDate": {
            "type": "date",
            "format": "dateOptionalTime"
          },
          "type": {
            "type": "string"
          },
          "views": {
            "properties": {
              "id": {
                "type": "string"
              },
              "isDefault": {
                "type": "boolean"
              },
              "name": {
                "type": "string"
              },
              "properties": {
                "properties": {
                  "name": {
                    "type": "string"
                  },
                  "type": {
                    "type": "string"
                  },
                  "value": {
                    "type": "string"
                  }
                }
              },
              "type": {
                "type": "string"
              },
              "viewId": {
                "type": "string"
              },
              "widgets": {
                "properties": {
                  "elementId": {
                    "type": "string"
                  },
                  "id": {
                    "type": "string"
                  },
                  "name": {
                    "type": "string"
                  },
                  "properties": {
                    "properties": {
                      "name": {
                        "type": "string"
                      },
                      "type": {
                        "type": "string"
                      },
                      "value": {
                        "type": "string"
                      }
                    }
                  },
                  "type": {
                    "type": "string"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

} } 请帮忙!

Chi*_*h25 6

您收到错误是因为您没有指定typefornestedprofile。如何创建views请参阅文档nested objects。您应该像这样定义type每个nested嵌套对象

{
  "xyz": {
    "mappings": {
      "abc": {
        "properties": {
          "lastModifiedBy": {
            "type": "string"
          },
          "lastModifiedDate": {
            "type": "date",
            "format": "dateOptionalTime"
          },
          "name": {
            "type": "string"
          },
          "profile": {
            "type": "nested", <--- here, you need this for every nested object
            "properties": {
              "lastModifiedBy": {
                "type": "string"
              },
              "lastModifiedDate": {
                "type": "date",
                "format": "dateOptionalTime"
              },
              "type": {
                "type": "string"
              },
              "views": {
                "type": "nested",
                "properties": {
                  "id": {
                    "type": "string"
                  },
                  "isDefault": {
                    "type": "boolean"
                  },
                  "name": {
                    "type": "string"
                  },
                  "properties": {
                    "type": "nested",
                    "properties": {
                      "name": {
                        "type": "string"
                      },
                      "type": {
                        "type": "string"
                      },
                      "value": {
                        "type": "string"
                      }
                    }
                  },
                  "type": {
                    "type": "string"
                  },
                  "viewId": {
                    "type": "string"
                  },
                  "widgets": {
                    "type": "nested",
                    "properties": {
                      "elementId": {
                        "type": "string"
                      },
                      "id": {
                        "type": "string"
                      },
                      "name": {
                        "type": "string"
                      },
                      "properties": {
                        "type": "nested",
                        "properties": {
                          "name": {
                            "type": "string"
                          },
                          "type": {
                            "type": "string"
                          },
                          "value": {
                            "type": "string"
                          }
                        }
                      },
                      "type": {
                        "type": "string"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!!