Elasticsearch 嵌套路径查询到对象类型

时间:2021-01-22 20:15:32

标签: elasticsearch elasticsearch-dsl

有这个模板(缩写版)。

{
  "index_patterns": "index_pattern*",
  "order": 1,
  "version": 1,
  "aliases": {
    "some_alias": {}
  },
  "settings": {
    "number_of_shards": 5,
  },
  "mappings": {
    "dynamic": "false",
    "properties": {
      "someId": {
        "type": "keyword"
      },
      "audience": {
        "type": "object",
        "properties": {
          ....
          "ageRanges": {
            "type": "nested",
            "properties": {
              "ageTo": {
                "type": "integer"
              },
              "ageFrom": {
                "type": "integer"
              }
            }
          }
        }
      }
    }
  }
}

我需要查询 audience.ageRanges 是否不存在或者是否存在应用其他过滤器。

假设我们要搜索具有特定 someId 值的文档是否适合 audience.ageRanges 查询子句(为清晰起见已删除)。 它有一些受众属性,但没有 ageRanges

"audience": {
  "genders": [
    "any"
  ],
  "deviceType": "any"
}

下面的查询不应该返回特定的文档吗?

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "someId": {
                            "value": "03183f31"
                        }
                    }
                },
                {
                    "nested": {
                        "path": "audience.ageRanges",
                        "query": {
                            "bool": {
                                "must_not": [
                                    {
                                        "exists": {
                                            "field": "audience.ageRanges"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}

我的结果是 0,它的工作原理有点令人困惑。

尝试使用包含 audience.ageRanges 项的文档 ID 并将 must_not 嵌套查询更改为 must 将返回结果。

1 个答案:

答案 0 :(得分:1)

与其将 must_not 放在嵌套查询中,不如将嵌套查询放在 must_not 中。

将样本索引数据视为

{
  "someId":123,
  "audience": {
    "genders": [
      "any"
    ],
    "deviceType": "any"
  }
}

您需要修改您的搜索查询,如下所示 -

搜索查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "someId": {
              "value": "123"
            }
          }
        },
        {
          "bool": {
            "must_not": {
              "nested": {
                "path": "audience.ageRanges",
                "query": {
                  "bool": {
                    "must": [
                      {
                        "exists": {
                          "field": "audience.ageRanges"
                        }
                      }
                    ]
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "65852173",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "someId": 123,
          "audience": {
            "genders": [
              "any"
            ],
            "deviceType": "any"
          }
        }
      }
    ]