elasticsearch - 查询 Elasticsearch 文档,其中嵌套对象中的属性值为空

标签 elasticsearch elasticsearch-query elasticsearch-7

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

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

[
    {
        "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"
          }
        }
      ]
    }
  }
}

我们使用 flex 搜索7.7

最佳答案

正如@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限制返回的响应。

关于elasticsearch - 查询 Elasticsearch 文档,其中嵌套对象中的属性值为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61079978/

相关文章:

java - 使用 Elasticsearch 7 java api 使用 Where 语句进行部分查询

datetime - 具有日期格式的索引映射,使用curl引发解析异常

amazon-web-services - 有没有办法一次恢复多个快照?

elasticsearch - 与Elasticsearch 7中的双引号完全匹配

json - 使用连接参数的Elasticsearch存储桶聚合

elasticsearch - 从Elasticsearch的命中结果中删除排序

elasticsearch - 合并多个聚合的结果

elasticsearch - Elasticsearch:全文搜索和按对象嵌套数组过滤

elasticsearch - Elasticsearch 7.7 版本中跨多个索引的查询

python - 在 python Elasticsearch 中滚动不起作用