2017-03-16 73 views
0

我有以下格式查詢在不同層次

{ 
    "mappings": { 
    "blog": { 
     "properties": { 
     "comments": { 
      "type": "nested", 
      "properties": { 
      "subComments": { 
       "type": "nested" 
      } 
      } 
     } 
     } 
    } 
    } 
} 

數據和我有數據的多個文檔一樣

{ 
    "blog_post_id": "blog1", 
    "comments": [ 
    { 
     "id": "c1", 
     "user_id": "u1", 
     "timestamp": 1487781975676, 
     "value": "CVLA1", 
     "subComments": [ 
     { 
      "value": "sub comment 1" 
     }, 
     { 
      "value": "sub comment 2" 
     } 
     ] 
    }, 
    { 
     "id": "c2", 
     "user_id": "u1", 
     "timestamp": 1487781975686, 
     "value": "CVLA2", 
     "subComments": [ 
     { 
      "value": "sub comment 3" 
     }, 
     { 
      "value": "sub comment 4" 
     } 
     ] 
    } 
    ] 
} 

我想匹配博客文檔的多級嵌套的文件,該文件具有評論值CVLA1和具有值「子評論2」的評論。

我寫了一個查詢像

{ 
    "query": { 
    "nested": { 
     "path": "comments", 
     "query": { 
     "bool": { 
      "must": [ 
      { 
       "match": { 
       "comments.value": "CVLA1" 
       } 
      }, 
      { 
       "nested": { 
       "path": "comments.subComments", 
       "query": { 
        "match": { 
        "commnets.subComments.value": "sub comment 2" 
        } 
       } 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

但預期這一個不工作。任何幫助如何在不同級別的多級嵌套文檔中查詢。

回答

0

您的查詢中有錯字commnets.subComments.value。它應該是comments.subComments.value。因此,整個查詢將如下所示:

{ 
    "query": { 
    "nested": { 
     "path": "comments", 
     "query": { 
     "bool": { 
      "must": [ 
      { 
       "match": { 
       "comments.value": "CVLA1" 
       } 
      }, 
      { 
       "nested": { 
       "path": "comments.subComments", 
       "query": { 
        "match": { 
        "comments.subComments.value": "sub comment 2" 
        } 
       } 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

我重複檢查 - 它對我來說工作正常。