2015-11-02 76 views
3

我使用Elasticsearch RC 2.0.0。Elasticsearch - Has_Parent或Has_Child查詢返回空結果

我得到了一些親子關係在我的Elasticsearch數據庫。我想檢索與父對象相關的所有Children。我總是收到一個空的結果列表。我遵循elasticsearch文檔的說明,並將我的代碼與幾本書進行了比較。我不明白,爲什麼我的查詢應該返回一個空的結果。

在這種情況下,我建立了一個簡化的例子。我將兩個對象放到elasticsearch中,並將對象a設置爲對象b的父對象。然後我嘗試檢索所有對象,其中有一個父類型爲a的。

這是我輸入:

PUT test 

PUT test/myobject/_mapping 
    { 
     "myobject":{ 
     "_parent" : {"type":"pobject"}, 
     "properties" : { 
     "name" : {"type":"string"} 
     } 

     } 
    } 

    PUT test/pobject/_mapping 
    { 
     "pobject" : { 
      "properties": { 
       "name": {"type":"string"} 
      } 

     } 

    } 

    PUT test/pobject/1 
    { 
     "name":"theParent" 
    } 

    PUT test/myobject/1?_parent=1&routing=_id 
    { 
     "name":"theChild" 
    } 

    POST test/myobject/_search?routing=_id 
    { 
     "query":{ 
      "has_parent":{ 
       "type":"pobject", 
      "query":{ 
       "match_all":{} 
      } 
      } 
     } 


    } 

這將返回

{ 
    "took": 2, 
    "timed_out": false, 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 0, 
     "max_score": null, 
     "hits": [] 
    } 
} 

回答

3

的錯誤是在這裏:PUT test/myobject/1?_parent=1&routing=_id

參數爲parent,不_parent

POST test/myobject/1?parent=1 
{ 
    "name": "theChild" 
} 

而且,你不需要使用routing=_id。請參閱documentation

命令的完整列表測試:

DELETE test 
PUT test 

PUT test/myobject/_mapping 
{ 
    "myobject": { 
    "_parent": { 
     "type": "pobject" 
    }, 
    "properties": { 
     "name": { 
     "type": "string" 
     } 
    } 
    } 
} 

PUT test/pobject/_mapping 
{ 
    "pobject": { 
    "properties": { 
     "name": { 
     "type": "string" 
     } 
    } 
    } 
} 

POST test/pobject/1 
{ 
    "name": "theParent" 
} 

POST test/myobject/1?parent=1 
{ 
    "name": "theChild" 
} 

POST test/myobject/_search 
{ 
    "query": { 
    "has_parent": { 
     "parent_type": "pobject", 
     "query": { 
     "match_all": {} 
     } 
    } 
    } 
} 
+0

您好,感謝,但我仍然會收到一個空resultlist。無論如何,當我打電話給測試/ myobject/1我看到有一個家長註冊這個項目。 – Goot

+0

@Goot更新了我的答案。 –

+0

原因:我使用父類型參數的錯誤名稱(「類型」而不是「parent_type」),我在使用映射參數時也犯了一個錯誤。謝謝 – Goot