即使没有子记录查询匹配,也返回父记录



(这实际上是AWS OpenSearch,我认为它是Elastic Search 7.x的一个分支(

因此,在这个人为的例子中,我在制造商和产品之间有一种父子关系。我想返回";acme";信息和所有产品。有些产品可能会被禁运(尚未准备好向公众上市(。对于像acme这样的新公司,它只有新的禁运产品,所以当我运行这个查询时,我不会返回公司信息。我试过使用"min_children": 0,,但仍然没有找到制造商。

对于这个查询,如果其他制造商至少有一种产品没有被禁运,他们就会被退回,所以这是关于has_child hits没有退回任何产品的问题。

{
"track_total_hits": true,
"query": {
"bool": {
"must": [
{
"has_child": {
"inner_hits": {
"name": "manf_products",
"size": 100
},
"min_children": 0,
"query": {
"bool": {
"should": [
{
"range": {
"embargo_date": {
"lt": "now/s"
}
}
}
]
}
},
"type": "product"
}
},
{
"bool": {
"should": [
{
"term": {
"manuf": {
"value": "acme"
}
}
}
]
}
}
]
}
}
}

min_children的最小值为1。这可以为您提供更多信息。

以下查询将返回未禁运的父文档和子文档。如果不存在未禁运的子级,它将返回父级

{
"track_total_hits": true,
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"has_child": {
"inner_hits": {
"name": "manf_products",
"size": 100
},
"query": {
"bool": {
"should": [
{
"range": {
"embargo_date": {
"lt": "now/s"
}
}
}
]
}
},
"type": "product"
}
},
{
"bool": {
"must_not": [
{
"has_child": {
"inner_hits": {
"name": "manf_products",
"size": 100
},
"query": {
"bool": {
"should": [
{
"range": {
"embargo_date": {
"lt": "now/s"
}
}
}
]
}
},
"type": "product"
}
}
]
}
}
],
"filter": [
{
"term": {
"manf": {
"value": "acme"
}
}
}
]
}
}
}

最新更新