名为id的嵌套字段上的术语筛选器无法按预期工作



我有一个非常奇怪的问题/bug,我不理解。我有一个孩子(课程)-父母(大学)的关系。我查询某所大学的课程,这些课程效果不错。由于我申请的概念,我还需要查询是否有属于某所大学的课程,然后汇总结果。我将这个概念用于不同的聚合,除了这一种对我来说没有意义的情况外,一切都很好。请注意:我有两种类型-学院和课程,而类型课程还有一个嵌套对象,叫做学院。由于ES中不支持父聚合,因此必须能够对某些聚合执行此操作。此概念对我来说非常有效,我认为它与上述问题/错误无关。

提前感谢Hannes

映射(缩小):

types:
                college:
                    mappings:
                        id: ~
                        premium: { "type": "boolean" }
                        boost: { "type": "boolean" }
                course:
                    mappings:
                        _all: { "analyzer": "text" }
                        id: { "type": "string", "analyzer": "term" }
                        name: { "type": "string", "analyzer": "text" }
                        types: { "type": "string", "analyzer": "term" }
                        college:
                            type: "object"
                            properties:
                                id: { "type": "string", "analyzer": "term" }
                                name: { "type": "string", "analyzer": "text" }
                    _parent:
                        type: "college"

查询:

GET _search
{
  "query": {
    "function_score": {
      "query": {
        "has_child": {
          "type": "course",
          "query": {
            "filtered": {
              "query": {
                "match_all": {}
              },
              "filter": {
                "bool": {
                  "must": [
                      {
                        "terms": {
                          "college.id": [
                            "371"
                          ]
                        }
                      }
                    ]
                }
              }
            }
          }
        }
      }
    }
  },
  "aggs": {
    "children": {
      "children": {
        "type": "course"
      },
      "aggs": {
        "filtered": {
          "filter": {
            "query": {
              "filtered": {
                "query": {
                  "match_all": {}
                },
                "filter": {
                  "bool": {
                    "must": [
                      {
                        "terms": {
                          "college.id": [
                            "371"
                          ]
                        }
                      }
                    ]
                  }
                }
              }
            }
          },
          "aggs": {
            "abschluss": {
              "terms": {
                "field": "degree",
                "size": 0
              },
              "aggs": {
                "unique": {
                  "cardinality": {
                    "field": "_parent",
                    "precision_threshold": 500
                  }
                }
              }
            },
            "coursecount": {
              "terms": {
                "field": "_parent",
                "size": 0
              }
            }
          }
        }
      }
    }
  },
  "from": 0,
  "size": 20
}

结果:

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "studiengaenge_dev",
            "_type": "college",
            "_id": "371",
            "_score": 1,
            "_source": {
               "id": "371",
               "premium": true,
               "boost": null
            }
         }
      ]
   },
   "aggregations": {
      "children": {
         "doc_count": 15,
         "filtered": {
            "doc_count": 0,
            "coursecount": {
               "doc_count_error_upper_bound": 0,
               "sum_other_doc_count": 0,
               "buckets": []
            },
            "abschluss": {
               "doc_count_error_upper_bound": 0,
               "sum_other_doc_count": 0,
               "buckets": []
            }
         }
      }
   }
}

我重命名了这个问题,自己回答,这样其他人就可以找到它。似乎一个名为"id"的字段是保留/预配置的,所以它不能在术语过滤器中使用。将字段重命名为其他名称解决了我的问题。

"filter": {
                "bool": {
                  "must": [
                      {
                        "terms": {
                          "college.myId": [
                            "371"
                          ]
                        }
                      }
                    ]
                }

最新更新