2015-05-13 80 views
1

當我使用查詢的「fields」選項時,我爲每個字段獲得單獨的數組。是否有可能找回「完整的」嵌套對象而不僅僅是字段?elasticsearch檢索嵌套對象 - 不是單個字段

在下面的示例中,如果我嘗試執行"fields": ["cast"]它告訴我cast不是葉節點。如果我做"fields": ["cast.firstName", "cast.middleName", "cast.lastName"]它返回3個數組。

是否還有另一種檢索文檔部分量的方法?或者是否有辦法將單獨的字段「重新組合」爲完整的「投射」對象?

實例索引和數據:

POST /movies 
{ 
    "mappings": { 
     "movie": { 
     "properties": { 
      "cast": { 
       "type": "nested" 
      } 
     } 
     } 
    } 
} 

POST /movies/movie 
{ 
    "title": "The Matrix", 
    "cast": [ 
     { 
     "firstName": "Keanu", 
     "lastName": "Reeves", 
     "address": { 
      "street": "somewhere", 
      "city": "LA" 
     } 
     }, 
     { 
     "firstName": "Laurence", 
     "middleName": "John", 
     "lastName": "Fishburne", 
     "address": { 
      "street": "somewhere else", 
      "city": "NYC" 
     } 
     } 
    ] 
} 

實例查詢:

GET /movies/_search 
{ 
    "query": { 
     "filtered": { 
     "query": { 
      "match_all": {} 
     }, 
     "filter": { 
      "nested": { 
       "path": "cast", 
       "filter": { 
        "bool": { 
        "must": [ 
         { "term": { "firstName": "laurence"} }, 
         { "term": { "lastName": "fishburne"} } 
        ] 
        } 
       } 
      } 
     } 
     } 
    }, 
    "fields": [ 
     "cast.address.city", 
     "cast.firstName", 
     "cast.middleName", 
     "cast.lastName" 
    ] 
} 

例如查詢的結果:

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 1, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "movies", 
      "_type": "movie", 
      "_id": "AU1JeyBseLgwMCOuOLsZ", 
      "_score": 1, 
      "fields": { 
       "cast.firstName": [ 
        "Keanu", 
        "Laurence" 
       ], 
       "cast.lastName": [ 
        "Reeves", 
        "Fishburne" 
       ], 
       "cast.address.city": [ 
        "LA", 
        "NYC" 
       ], 
       "cast.middleName": [ 
        "John" 
       ] 
      } 
     } 
     ] 
    } 
} 

回答

5

我認爲這是你在找什麼:

POST /movies/_search 
{ 
    "_source": { 
    "include": [ 
     "cast.address.city", 
     "cast.firstName", 
     "cast.middleName", 
     "cast.lastName" 
    ] 
    }, 
    "query": { 
    "filtered": { 
     "query": { 
     "match_all": {} 
     }, 
     "filter": { 
     "nested": { 
      "path": "cast", 
      "filter": { 
      "bool": { 
       "must": [ 
       { 
        "term": { 
        "firstName": "laurence" 
        } 
       }, 
       { 
        "term": { 
        "lastName": "fishburne" 
        } 
       } 
       ] 
      } 
      } 
     } 
     } 
    } 
    } 
} 

結果:

{ 
    "took": 2, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 1, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "movies", 
      "_type": "movie", 
      "_id": "AU1PIJgBA_0Cyshym7-m", 
      "_score": 1, 
      "_source": { 
       "cast": [ 
        { 
        "lastName": "Reeves", 
        "address": { 
         "city": "LA" 
        }, 
        "firstName": "Keanu" 
        }, 
        { 
        "middleName": "John", 
        "lastName": "Fishburne", 
        "address": { 
         "city": "NYC" 
        }, 
        "firstName": "Laurence" 
        } 
       ] 
      } 
     } 
     ] 
    } 
} 

您也可以選擇在此領域排除在外,而不是包括或兩者,請參閱文檔:http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html

+0

這就是它 - 感謝! – jdh2550

相關問題