0
的父源如果在索引中有兩個映射:ElasticSearch:檢索has_parent查詢
- 公司
- 員工
,其中公司員工的父母,你怎麼能執行「has_parent」查詢,它也會在響應中返回父記錄的來源(公司記錄)?
這是公司的映射:
{
"company" : {
"dynamic" : "false",
"_meta" : {
"version" : "1.0",
"author" : "Gil"
},
"_all" : {
"enabled" : false
},
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
這是僱員映射:
{
"employee" : {
"dynamic" : "false",
"_meta" : {
"version" : "1.0",
"author" : "Gil"
},
"_all" : {
"enabled" : false
},
"_parent" : {
"type" : "company"
},
"_routing" : {
"required" : true
},
"properties" : {
"firstName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"lastName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
兩個公司和僱員都在相同的索引。
這是一個示例has_parent查詢中使用前綴的查詢返回基於公司的名稱員工:
{
"query" : {
"has_parent" : {
"parent_type" : "company",
"query" : {
"prefix" : {
"name" : "one"
}
}
}
}
}
這是此查詢的典型結果:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [ {
"_index" : "companies",
"_type" : "employee",
"_id" : "476d8080-2cd2-4a4f-a992-852dabb5ece5",
"_score" : 1.0,
"_routing" : "54574774-671e-4c0a-bcc0-cb25ab02b125",
"_parent" : "54574774-671e-4c0a-bcc0-cb25ab02b125",
"_source" : {
"firstName" : "Gil",
"lastName" : "Fernandes"
}
} ]
}
}