2017-01-09 118 views
0

彈性文檔聲明可以在查詢中使用_parent字段(請參閱https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-parent-field.html)。ElasticSearch _parent查詢

但是,我一直無法讓它工作。下面是簡單的測試:

PUT /company 
{ 
    "mappings": { 
    "branch": {}, 
    "employee": { 
     "_parent": { 
     "type": "branch" 
     } 
    } 
    } 
} 

POST /company/branch/_bulk 
{ "index": { "_id": "london" }} 
{ "name": "London Westminster", "city": "London", "country": "UK" } 
{ "index": { "_id": "liverpool" }} 
{ "name": "Liverpool Central", "city": "Liverpool", "country": "UK" } 
{ "index": { "_id": "paris" }} 
{ "name": "Champs Élysées", "city": "Paris", "country": "France" } 

PUT /company/employee/1?parent=london 
{ 
    "name": "Alice Smith", 
    "dob": "1970-10-24", 
    "hobby": "hiking" 
} 

驗證該員工有_parent場:

GET /company/employee/_search 
{ 
    "query": { 
    "match_all": {} 
    } 
} 

回報

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 1, 
    "max_score": 1, 
    "hits": [ 
     { 
     "_index": "company", 
     "_type": "employee", 
     "_id": "1", 
     "_score": 1, 
     "_routing": "london", 
     "_parent": "london", 
     "_source": { 
      "name": "Alice Smith", 
      "dob": "1970-10-24", 
      "hobby": "hiking" 
     } 
     } 
    ] 
    } 
} 

但以下幾點:

GET /company/employee/_search 
{ 
    "query": { 
    "term": { 
     "_parent":"london" 
    } 
    } 
} 

回報:

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

使用「has_parent」工作,但爲什麼不使用_parent工作,如文檔中所述。

下面是使用has_parent的作品查詢:

GET /company/employee/_search 
{ 
    "query": { 
    "has_parent": { 
     "parent_type":"branch", 
     "query":{ 
     "match_all": {} 
     } 
    } 
    } 
} 

我缺少什麼?使用ElasticSearch 5.0.2。

回答

0

這是一個文檔錯誤。根據breaking changes in 5.0_parent字段不再編入索引,因此無法在該字段上運行term查詢。你要麼需要使用has_parent查詢或新parent_id查詢來查找子文檔:

POST /company/employee/_search 
{ 
    "query": { 
    "parent_id": { 
     "type": "employee", 
     "id": "london" 
    } 
    } 
} 

對於那些誰想要跟着,我已經filed an issue報告這一點,它得到了修復。更新的文檔即將可用。

+0

有沒有這樣的運氣? – Val