查询弹性搜索文档,其中嵌套对象的属性为空值

问题描述 投票:1回答:1

我正在努力进行弹性搜索查询。

这些是示例文档,我想查询这些文档。这些是具有通用属性的文档

[
    {
        "field1": "value",
        "properties": [
            {
                "propertyBooleanValue": null,
                "propertyName": "Product name",
                "propertyDateValue": null,
                "propertyType": "TEXT",
                "propertyStringValue": "SUPER Cool Extreme",
                "propertyNumericValue": null
            },
            {
                "propertyBooleanValue": null,
                "propertyName": "Product expiration date",
                "propertyDateValue": null,
                "propertyType": "DATE",
                "languageCode": null,
                "propertyNumericValue": null
            }
        ]
    },
    {
        "field1": "blah blah",
        "properties": [
            {
                "propertyBooleanValue": null,
                "propertyName": "Product name",
                "propertyDateValue": null,
                "propertyType": "TEXT",
                "propertyStringValue": "So boring",
                "propertyNumericValue": null
            },
            {
                "propertyBooleanValue": null,
                "propertyName": "Product expiration date",
                "propertyDateValue": "2020-04-02",
                "propertyType": "DATE",
                "languageCode": null,
                "propertyNumericValue": null
            }
        ]
    },
    {
        "field1": "wow2",
        "properties": [
            {
                "propertyBooleanValue": null,
                "propertyName": "Product name",
                "propertyDateValue": null,
                "propertyType": "TEXT",
                "propertyStringValue": "iPear",
                "propertyNumericValue": null
            },
            {
                "propertyBooleanValue": null,
                "propertyName": "Product expiration date",
                "propertyDateValue": null,
                "propertyType": "DATE",
                "languageCode": null,
                "propertyNumericValue": null
            }
        ]
    }
]

我只想查询带有嵌套对象的文档,该文档的属性为“ propertyName” =“产品有效期”和“ propertyDateValue” = null

我使用查询,但它返回所有文档:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "query": {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "must_not": [
                        {
                          "exists": {
                            "field": "properties.propertyDateValue"
                          }
                        }
                      ]
                    }
                  },
                  {
                    "term": {
                      "properties.propertyName": {
                        "value": "Product expiration date"
                      }
                    }
                  }
                ]
              }
            },
            "path": "properties"
          }
        }
      ]
    }
  }
}

我们使用弹性搜索7.7

elasticsearch elasticsearch-query elasticsearch-7
1个回答
0
投票

如@jaspreet所述,预期结果。为了进一步详细说明,您可以使用inner_hits参数仅检索实际上与两个查询都匹配的那些properties嵌套子文档,即:]]

{
  "_source": "inner_hits",        <---- hiding the default response
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "query": {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "must_not": [
                        {
                          "exists": {
                            "field": "properties.propertyDateValue"
                          }
                        }
                      ]
                    }
                  },
                  {
                    "term": {
                      "properties.propertyName": {
                        "value": "Product expiration date"
                      }
                    }
                  }
                ]
              }
            },
            "path": "properties",
            "inner_hits": {}         <----- needs to be here
          }
        }
      ]
    }
  }
}

屈服

[
      {
        "_index" : "mich",
        "_type" : "_doc",
        "_id" : "6iLSVHEBZbobBB0NSl9x",
        "_score" : 0.6931472,
        "_source" : { },
        "inner_hits" : {
          "properties" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 0.6931472,
              "hits" : [
                {
                  "_index" : "mich",
                  "_type" : "_doc",
                  "_id" : "6iLSVHEBZbobBB0NSl9x",
                  "_nested" : {
                    "field" : "properties",
                    "offset" : 1
                  },
                  "_score" : 0.6931472,
                  "_source" : {
                    "propertyBooleanValue" : null,
                    "propertyName" : "Product expiration date",
                    "propertyDateValue" : null,
                    "propertyType" : "DATE",
                    "languageCode" : null,
                    "propertyNumericValue" : null
                  }
                }
              ]
            }
          }
        }
      },
      ...
    ]

这可能是您想要的。


请注意,上面的查询与以下查询不同,在以下查询中,您有两个单独的bool-must子句,与第一个查询相比,它们忽略了AND连接。在这种情况下,inner_hits将需要具有唯一的名称。

{
  "_source": "inner_hits", 
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "must_not": [
                        {
                          "exists": {
                            "field": "properties.propertyDateValue"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            },
            "inner_hits": {
              "name": "NULL_propertyDateValue"
            }
          }
        },
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "properties.propertyName": {
                        "value": "Product expiration date"
                      }
                    }
                  }
                ]
              }
            },
            "inner_hits": {
                "name": "MATCH_IN_propertyName"
            }
          }
        }
      ]
    }
  }
}

长话短说,请第一个查询,并随时使用inner_hits来限制返回的响应。

© www.soinside.com 2019 - 2024. All rights reserved.