2016-11-22 89 views
0

我有索引的映射:Elasticsearch - 條件嵌套取

{ 
    "dev.directory.3" : { 
    "mappings" : { 
     "profile" : { 
     "properties" : { 
      "email" : { 
      "type" : "string", 
      "index" : "not_analyzed" 
      }, 
      "events" : { 
      "type" : "nested", 
      "properties" : { 
       "id" : { 
       "type" : "integer" 
       }, 
       "name" : { 
       "type" : "string", 
       "index" : "not_analyzed" 
       }, 
      } 
      } 
     } 
     } 
    } 
    } 
} 

與數據:

"hits" : [ { 
    "_index" : "dev.directory.3", 
    "_type" : "profile", 
    "_id" : "1", 
    "_score" : 1.0, 
    "_source" : { 
     "email" : "[email protected]", 
     "events" : [ 
     { 
     "id" : 111, 
     "name" : "ABC", 
     }, 
     { 
     "id" : 222, 
     "name" : "DEF", 
     } 
     ], 
    } 
}] 

我想僅過濾匹配嵌套元素,而不是返回所有events陣列 - 這可能在ES?

實施例的查詢:

{ 
     "nested" : { 
      "path" : "events", 
      "query" : { 
       "bool" : { 
        "filter" : [ 
        { "match" : { "events.id" : 222 } }, 
        ] 
       } 
      } 
     } 
    } 

EG。如果我查詢events.id = 222,返回的結果列表中應該只有單個元素。

什麼樣的策略最適合實現這種要求?

回答

1

您可以使用inner_hits僅獲取與查詢匹配的嵌套記錄。

{ 
    "query": { 
    "nested": { 
     "path": "events", 
     "query": { 
     "bool": { 
      "filter": [ 
      { 
       "match": { 
       "events.id": 222 
       } 
      } 
      ] 
     } 
     }, 
     "inner_hits": {} 
    } 
    }, 
    "_source": false 
} 

我也排除了源得到嵌套命中