具有不存在的嵌套字段或嵌套字段的条件存在的记录集

Set of records with non existing nested field or conditiontional existance of a nested field

我正在尝试获取一组 product 文档,其中 product_texts 对象中没有 "product_text.language_id": 1 记录 OR product_texts 带有一个空的 product_texts.description 字段和 "product_text.language_id": 1product_texts是products索引的子文档。

我尝试了下面的查询,但它 returns 一些 product 与现有的 product_texts.description.

"bool": {
  "must": [
    {
      "nested": {
        "path": "product_texts",
        "query": {
          "bool": {
            "should": [
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "product_texts.language_id": 1
                      }
                    }
                  ],
                  "must_not": [
                    {
                      "exists": {
                        "field": "product_texts.description"
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must_not": [
                    {
                      "bool": {
                        "must": [
                          {
                            "term": {
                              "product_texts.language_id": 1
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      }
    }
  ]
}

这是 product 映射;

properties: {
    id: {
      type: 'long',
    },
...
product_texts: {
  type: 'nested',
  properties: {
    description: {
      type: 'text',
      fields: {
        keyword: {
          type: 'keyword',
          ignore_above: 256,
        },
      },
    },
    id: {
      type: 'long',
    },
    language_id: {
      type: 'long',
    },
    name: {
      type: 'text',
      fields: {
        keyword: {
          type: 'keyword',
          ignore_above: 256,
        },
      },
    },
  },
},

示例完整集:

{
  "hits": [
    {
      "_index": "products",
      "_type": "_doc",
      "_id": "131483",
      "_score": null,
      "_source": {
        "product_texts": [
          {
            "description": "Some description",
            "language_id": 1
          },
          {
            "description": "Some description",
            "language_id": 2
          },
          {
            "description": "Some description",
            "language_id": 3
          }
        ]
      }
    },
    {
      "_index": "products",
      "_type": "_doc",
      "_id": "131484",
      "_score": null,
      "_source": {
        "product_texts": [
          {
            "description": "Some description",
            "language_id": 2
          },
          {
            "description": "Some description",
            "language_id": 3
          }
        ]
      }
    },    {
      "_index": "products",
      "_type": "_doc",
      "_id": "131485",
      "_score": null,
      "_source": {
        "product_texts": [
          {
            "description": null,
            "language_id": 1
          },
          {
            "description": "Some description",
            "language_id": 2
          },
          {
            "description": "Some description",
            "language_id": 3
          }
        ]
      }
    }
  ]
}

预期查询结果 _ids 131484 和 131485,NOT 131483 因为它在 product_texts 中有一条记录,描述为非空且 language_id 1。

这个就可以了

{
  "query": {
    "bool": {
      "should": [ /** OR condition **/
        {
          "nested": {
            "path": "product_texts",
            "query": {
              "bool": { /** No change in the first condition **/
                "must": [
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "product_texts.language_id": 1
                          }
                        }
                      ],
                      "must_not": [
                        {
                          "exists": {
                            "field": "product_texts.description"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "bool": { /** Negated the existence query with value - None of the nested array matches - that's where the real problem**/
            "must_not": { /**Negating the logic **/
              "nested": {
                "path": "product_texts",
                "query": {
                  "bool": {
                    "must": {
                      "term": {/**lang id 1 is present **/
                        "product_texts.language_id": 1
                      }
                    }
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

请查找评论。

所以你的第二个条件:key with value 不应该存在。