我有一个文档,其中包含许多嵌套字段和对象数组。因此,当我执行搜索时,如何只获得对象数组中的匹配对象,而不是整个文档?
{
"name": "Olivia",
"jobs": [
{
"title": "Accountant",
"offices": [
{
"city": "Paris",
"address": "Her adress"
},
{
"city": "NYC",
"address": "New adress"
}
]
},
{
"title": "Judge",
"offices": [
{
"city": "Paris",
"address": "Judge adress"
},
{
"city": "NYC",
"address": "New judge adress"
}
]
}
]
}
工作和办公室是嵌套的,我想在我的搜索:她的名字,工作和办公室的城市是“巴黎”,在正常的搜索中,我得到了两个办公室,其中城市是巴黎和纽约。
有没有办法只让“巴黎”的办公室成为城市?
发布于 2021-04-13 13:49:01
你需要使用来源,才能得到“巴黎”作为城市的办公室。
使用索引数据添加一个工作示例(与ques中提供的数据相同)、映射、搜索查询和搜索结果
索引映射:
{
"mappings": {
"properties": {
"jobs": {
"type": "nested",
"properties": {
"offices": {
"type": "nested"
}
}
}
}
}
}
搜索查询:
{
"query": {
"nested": {
"path": "jobs.offices",
"query": {
"bool": {
"must": [
{
"match": {
"jobs.offices.city": "Paris"
}
}
]
}
},
"inner_hits": {
"_source": [
"jobs.offices.address"
]
}
}
}
}
搜索结果:
"inner_hits": {
"jobs.offices": {
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.6931471,
"hits": [
{
"_index": "67074755",
"_type": "_doc",
"_id": "1",
"_nested": {
"field": "jobs",
"offset": 0,
"_nested": {
"field": "offices",
"offset": 0
}
},
"_score": 0.6931471,
"_source": {
"address": "Her adress"
}
},
{
"_index": "67074755",
"_type": "_doc",
"_id": "1",
"_nested": {
"field": "jobs",
"offset": 1,
"_nested": {
"field": "offices",
"offset": 0
}
},
"_score": 0.6931471,
"_source": {
"address": "Judge adress"
}
}
]
}
}
}
}
https://stackoverflow.com/questions/67074755
复制相似问题