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

4

1 回答 1

1

正如@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.

于 2020-04-07T13:43:36.130 回答