【发布时间】:2018-09-04 05:55:14
【问题描述】:
尊敬的 elasticsearch 专家,
我在查询嵌套对象时遇到问题。让我们使用以下简化映射:
{
"mappings" : {
"_doc" : {
"properties" : {
"companies" : {
"type": "nested",
"properties" : {
"company_id": { "type": "long" },
"name": { "type": "text" }
}
},
"title": { "type": "text" }
}
}
}
}
并将一些文档放入索引中:
PUT my_index/_doc/1
{
"title" : "CPU release",
"companies" : [
{ "company_id" : 1, "name" : "AMD" },
{ "company_id" : 2, "name" : "Intel" }
]
}
PUT my_index/_doc/2
{
"title" : "GPU release 2018-01-10",
"companies" : [
{ "company_id" : 1, "name" : "AMD" },
{ "company_id" : 3, "name" : "Nvidia" }
]
}
PUT my_index/_doc/3
{
"title" : "GPU release 2018-03-01",
"companies" : [
{ "company_id" : 3, "name" : "Nvidia" }
]
}
PUT my_index/_doc/4
{
"title" : "Chipset release",
"companies" : [
{ "company_id" : 2, "name" : "Intel" }
]
}
现在我想执行这样的查询:
{
"query": {
"bool": {
"must": [
{ "match": { "title": "GPU" } },
{ "nested": {
"path": "companies",
"query": {
"bool": {
"must": [
{ "match": { "companies.name": "AMD" } }
]
}
},
"inner_hits" : {}
}
}
]
}
}
}
因此,我想获得具有匹配文件数量的匹配公司。所以上面的查询应该给我:
[
{ "company_id" : 1, "name" : "AMD", "matched_documents:": 1 }
]
以下查询:
{
"query": {
"bool": {
"must": [
{ "match": { "title": "GPU" } }
{ "nested": {
"path": "companies",
"query": { "match_all": {} },
"inner_hits" : {}
}
}
]
}
}
}
应该给我分配给标题包含“GPU”的文档的所有公司以及匹配文档的数量:
[
{ "company_id" : 1, "name" : "AMD", "matched_documents:": 1 },
{ "company_id" : 3, "name" : "Nvidia", "matched_documents:": 2 }
]
有没有可能以良好的性能达到这个结果?我明确对匹配文档不感兴趣,只对匹配文档的数量和嵌套对象感兴趣。
感谢您的帮助。
【问题讨论】:
标签: elasticsearch