2015-12-08 41 views
0

我有使用父子關係建模的數據。我希望能夠使用內部匹配從父母中獲取特定的孩子 - 至少這是我從內部匹配的定義中瞭解到的。使用inner hits查詢來自父子關係的數據

我的映射爲:

{ 
    "mappings": { 
     "parent_type": { 
     "properties": { 
      "num_prop": { 
       "type": "integer" 
      }, 
      "str_prop": { 
       "type": "string" 
      } 
     } 
     }, 
     "child_type": { 
     "_parent": { 
      "type": "parent_type" 
     }, 
     "properties": { 
      "child_num": { 
       "type": "integer" 
      }, 
      "child_str": { 
       "type": "string" 
      } 
     } 
     } 
    } 
} 

我的數據是:

{"index":{"_type":"parent_type","_id":1}} 
{"num_prop":1,"str_prop":"hello"} 
{"index":{"_type":"child_type","_id":1,"_parent":1}} 
{"child_num":11,"child_str":"foo"} 
{"index":{"_type":"child_type","_id":2,"_parent":1}} 
{"child_num":12,"child_str":"bar"} 
{"index":{"_type":"parent_type","_id":2}} 
{"num_prop":2,"str_prop":"goodbye"} 
{"index":{"_type":"child_type","_id":3,"_parent":2}} 
{"child_num":21,"child_str":"baz"} 
{"index":{"_type":"child_type","_id":4,"_parent":2}} 
{"child_num":13,"child_str":"foo"} 

正如上面可以看出,與ID 1父有兩個孩子 「foo」 的和 「bar」,和父2也有一個孩子「富」。現在我想知道foo及其父母1 - 我正在嘗試使用內部匹配。

{ 
    "query": { 
    "has_child": { 
     "type": "child_type", 
     "query": { 
       "match": { "child_str": "foo"} 
     } } 

     }, 

     "inner_hits": { 

     "parent_type" : { 
      "type" : { 

        "parent_type" : { 
        "query" : { 
         "match" : {"str_prop" : "hello"} 
        } 

       } 
      } 




} 
} 
} 

但是,這是行不通的。有人能讓我知道這裏有什麼問題嗎?

感謝和問候, 普里亞

回答

1

答案是:

{ 
"query": { 
    "filtered": { 
     "query": { 


      "match": { "str_prop": "hello"} 


     }, 
     "filter":{ 
     "has_child": { 
      "type": "child_type", 
      "query" : { 
      "filtered": { 
       "query": { "match_all": {}}, 
       "filter" : { 
       "and": [ 
        {"match": {"child_str": "foo"}} 
       ] 
       } 
      } 
      }, 

      "inner_hits" : {} 
     } 
     } 
    } 
    } 

}