2016-09-14 5 views
0

私は親オブジェクト(ブログ投稿)およびネストされた項目(コメント) 親とネストされたオブジェクトは、両方のは親によってオブジェクトと子供が

だから私の場合、IDをキーとしているがあり、IDS親オブジェクトを取得し、私はすべての子供たちにあまりにも

GET /my_index/blogpost/1 
{ 
    "id": 1, 
    "title": "Nest eggs", 
    "body": "Making your money work...", 
    "tags": [ "cash", "shares" ], 
    "comments": [ 
    { 
     "id": 2, 
     "comment": "Great article", 
     "age":  28, 
     "stars": 4, 
     "date": "2014-09-01" 
    }, 
    { 
     "id": 4, 
     "comment": "More like this please", 
     "age":  31, 
     "stars": 5, 
     "date": "2014-10-22" 
    } 
    ] 
} 

質問

を取得しますが、私は親と子のIDに基づいて子供のサブセットを取得したいです

私の希望する動作は次のとおりです。

GET /my_index/blogpost/1?onlyGetCommentIds=4 
{ 
    "id": 1, 
    "title": "Nest eggs", 
    "body": "Making your money work...", 
    "tags": [ "cash", "shares" ], 
    "comments": [ 
    { 
     "id": 4, 
     "comment": "More like this please", 
     "age":  31, 
     "stars": 5, 
     "date": "2014-10-22" 
    } 
    ] 
} 
上記の例では、親オブジェクトとともにコメントID == 4が返されます。

このクエリはどのように構成しますか?達成するためにあなたはnested inner_hitsを使用する必要が

答えて

0

あなたは

POST /my_index/blogpost/_search 
{ 
    "_source": { 
    "exclude": "comments" 
    }, 
    "query": { 
    "bool": { 
     "filter": [ 
     { 
      "term": { 
      "id": 1 
      } 
     }, 
     { 
      "nested": { 
      "path": "comments", 
      "query": { 
       "term": { 
       "comments.id": 4 
       } 
      }, 
      "inner_hits": {} 
      } 
     } 
     ] 
    } 
    } 
} 

何をしたいの応答は=(commentsネストされたオブジェクトなし)親オブジェクトとIDを有する唯一の希望、ネストされたコメントを含む別のinner_hitsのセクションが含まれます4.

+0

これで運がいいですか? – Val

関連する問題