#Elasticsearch容許給文檔創建父子關係,這篇博客介紹文檔的父子關係是如何映射的(Parent-Child Mapping)、如何索引父子文檔(Indexing Parents and Children)、如何經過子文檔查詢父文檔
(Finding Parents by Their Children)、如何經過父文檔查詢子文檔(Finding Children by Their Parents)。json
創建文檔的父子關係最重要的一步是在建立索引的時候在mapping中聲明哪一個是父文檔哪一個是子文檔。官網文檔給了一個公司在不少城市的分支(branch)和每一個分支有相關員工(employee)的例子,若是想把員工和他們工做的分公司關聯起來,咱們須要告訴Elasticsearch文檔之間的父子關係,這裏employee是child type,branch是parent type,在mapping中聲明:app
curl -XPUT "http://192.168.0.224:9200/company" -d ' { "mappings": { "branch": {}, "employee": { "_parent": { "type": "branch" } } } } '
這樣咱們建立了一個索引,並制定了2個type和它們之間的父子關係.curl
索引父文檔和索引通常的文檔沒有任何區別。
準備幾條公司的數據,存在company.json文件中:url
{ "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" }
使用Bulk端點批量導入:spa
curl -XPOST "http://192.168.0.224:9200/company/branch/_bulk?pretty" --data-binary @company.json
索引子文檔須要制定子文檔的父ID,給子文檔的每條文檔設置parent屬性的value爲父文檔id便可:code
curl -XPUT "http://192.168.0.224:9200/company/employee/1?parent=london&pretty" -d ' { "name": "Alice Smith", "dob": "1970-10-24", "hobby": "hiking" } '
執行bulk命令:索引
curl -XPOST "http://192.168.0.224:9200/company/employee/_bulk?pretty" --data-binary @employee.json
搜索含有1980年之後出生的employee的branch:ci
curl -XGET "http://192.168.0.224:9200/company/branch/_search?pretty" -d ' { "query": { "has_child": { "type": "employee", "query": { "range": { "dob": { "gte": "1980-01-01" } } } } } } ' { "took" : 19, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 1.0, "hits" : [ { "_index" : "company", "_type" : "branch", "_id" : "paris", "_score" : 1.0, "_source" : { "name" : "Champs Élysées", "city" : "Paris", "country" : "France" } }, { "_index" : "company", "_type" : "branch", "_id" : "london", "_score" : 1.0, "_source" : { "name" : "London Westminster", "city" : "London", "country" : "UK" } } ] } }
查詢name中含有「Alice Smith」的branch:文檔
curl -XGET "http://192.168.0.224:9200/company/branch/_search?pretty" -d ' { "query": { "has_child": { "type": "employee", "score_mode": "max", "query": { "match": { "name": "Alice Smith" } } } } }' { "took" : 20, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 1.2422675, "hits" : [ { "_index" : "company", "_type" : "branch", "_id" : "london", "_score" : 1.2422675, "_source" : { "name" : "London Westminster", "city" : "London", "country" : "UK" } }, { "_index" : "company", "_type" : "branch", "_id" : "liverpool", "_score" : 0.30617762, "_source" : { "name" : "Liverpool Central", "city" : "Liverpool", "country" : "UK" } } ] } }
搜索最少含有2個employee的branch:博客
curl -XGET "http://192.168.0.224:9200/company/branch/_search?pretty" -d '{ "query": { "has_child": { "type": "employee", "min_children": 2, "query": { "match_all": {} } } } } ' { "took" : 17, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "company", "_type" : "branch", "_id" : "london", "_score" : 1.0, "_source" : { "name" : "London Westminster", "city" : "London", "country" : "UK" } } ] } }
搜搜工做在UK的employee:
curl -XGET "http://192.168.0.224:9200/company/employee/_search?pretty" -d '{ "query": { "has_parent": { "type": "branch", "query": { "match": { "country": "UK" } } } } }' { "took" : 15, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 3, "max_score" : 1.0, "hits" : [ { "_index" : "company", "_type" : "employee", "_id" : "3", "_score" : 1.0, "_parent" : "liverpool", "_routing" : "liverpool", "_source" : { "name" : "Barry Smith", "dob" : "1979-04-01", "hobby" : "hiking" } }, { "_index" : "company", "_type" : "employee", "_id" : "1", "_score" : 1.0, "_parent" : "london", "_routing" : "london", "_source" : { "name" : "Alice Smith", "dob" : "1970-10-24", "hobby" : "hiking" } }, { "_index" : "company", "_type" : "employee", "_id" : "2", "_score" : 1.0, "_parent" : "london", "_routing" : "london", "_source" : { "name" : "Mark Thomas", "dob" : "1982-05-16", "hobby" : "diving" } } ] } }