2017-04-14 8 views
0

offersの配列をidフィールドでフィルタリングし、検索したIDで結果オブジェクトに戻す方法は?idでフィルタリングし、id、Elasticsearchの項目を返す1.7

現在の検索正しく申し出配列内のIDによってデータを見つけるが、それはまた、すべてのオブジェクトを返します。

GET activities/activity/_search 
{ 
    "query": { 
     "filtered": { 
     "filter": { 
      "bool": { 
       "must": [ 
        { 
        "term": { 
         "offers.id": "12" 
        } 
        } 
       ] 
      } 
     } 
     } 
    } 
} 

現在の結果を、私はオファーをフィルタリングし"id": 12でのみこれを取得したいと思います:

"hits": [ 
    { 
     "_index": "activities", 
     "_type": "activity", 
     "_id": "AVtr4-UV81wMr8KFD246", 
     "_score": null, 
     "_source": { 
     "offers": [ 
      { 
       "title": "merge", 
       "id": 11 
      }, 
      { 
       "title": "order test", 
       "id": 12 
      } 
     ], 
     "event": "candidate_remove", 
     "created_at": "2017-04-14T09:55:49.115174Z" 
     } 
    } 
] 
活動タイプで提供のための

マッピング:

"offers": { 
    "type": "nested", 
    "include_in_parent": true, 
    "properties": { 
     "id": { 
     "type": "long" 
     }, 
     "title": { 
     "type": "string", 
     "index": "not_analyzed" 
     } 
    } 
}, 
+1

この答えは役立つかもしれない:http://stackoverflow.com/questions/32773542/fetch単列フィルタリングされた入れ子オブジェクトfrom index-in-elasticsearch/32774267#32774267 – Val

+0

可能な複製http://stackoverflow.com/q/32773542/5936696 – avr

答えて

1

{ 
    "query": { 
    "filtered": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "nested": { 
       "path": "offers", 
       "query": { 
        "term": { 
        "offers.id": "12" 
        } 
       }, 
       "inner_hits":{} 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

これは、それが一致するネストされた書類を見せてinner_hitsと呼ばれる応じて別のセクションを追加します:あなたはinner_hits機能とnestedクエリを必要としています。

あなたが元offers値を必要としない場合は、元のクエリにこのような何かを追加することができます。

{ 
    "_source": {"exclude": "offers"}, 
    "query": { 
    "filtered": { 
関連する問題