嵌套的嵌套查询,过滤器聚合失败

时间:2014-09-20 01:47:36

标签: elasticsearch

我正在尝试在嵌套的过滤器聚合中使用嵌套查询过滤器。当我这样做时,聚合返回没有项目。如果我将查询更改为一个普通的旧match_all过滤器,我会在项目中找回项目。

以下是我正在使用的映射的简化版本:

"player": {
  "properties": {
    "rating": {
      "type": "float"
    },
    "playerYears": {
      "type": "nested",
      "properties": {
        "schoolsOfInterest": {
          "type": "nested",
          "properties": {
            "name": {
                "type": "string",
                "index": "not_analyzed"
            }
          }
        }
      }
    }
  }
}

此查询,在聚合上使用match_all过滤器:

GET /players/_search
{
  "size": 0,
  "aggs": {
    "rating": {
      "nested": {
        "path": "playerYears"
      },
      "aggs": {
        "rating-filtered": {
          "filter": {
                "match_all": {}
          },
          "aggs": {
            "rating": {
              "histogram": {
                "field": "playerYears.rating",
                "interval": 1
              }
            }
          }
        }
      }
    }
  },
  "query": {
    "filtered": {
      "filter": {
        "match_all": {}
      }
    }
  }
}

返回以下内容:

{
   "took": 16,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 167316,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "rating": {
         "doc_count": 363550,
         "rating-filtered": {
            "doc_count": 363550,
            "rating": {
               "buckets": [
                  {
                     "key_as_string": "-1",
                     "key": -1,
                     "doc_count": 20978
                  },
                  {
                     "key_as_string": "0",
                     "key": 0,
                     "doc_count": 312374
                  },
                  {
                     "key_as_string": "1",
                     "key": 1,
                     "doc_count": 1162
                  },
                  {
                     "key_as_string": "2",
                     "key": 2,
                     "doc_count": 12104
                  },
                  {
                     "key_as_string": "3",
                     "key": 3,
                     "doc_count": 9558
                  },
                  {
                     "key_as_string": "4",
                     "key": 4,
                     "doc_count": 5549
                  },
                  {
                     "key_as_string": "5",
                     "key": 5,
                     "doc_count": 1825
                  }
               ]
            }
         }
      }
   }
}

但是这个在聚合中有嵌套过滤器的查询返回一个空桶:

GET /players/_search
{
  "size": 0,
  "aggs": {
    "rating": {
      "nested": {
        "path": "playerYears"
      },
      "aggs": {
        "rating-filtered": {
          "filter": {
              "nested": {
                "query": {
                  "match_all": {}
                },
                "path": "playerYears.schoolsOfInterest"
            }
          },
          "aggs": {
            "rating": {
              "histogram": {
                "field": "playerYears.rating",
                "interval": 1
              }
            }
          }
        }
      }
    }
  },
  "query": {
    "filtered": {
      "filter": {
        "match_all": {}
      }
    }
  }
}

空桶:

{
   "took": 8,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 167316,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "rating": {
         "doc_count": 363550,
         "rating-filtered": {
            "doc_count": 0,
            "rating": {
               "buckets": []
            }
         }
      }
   }
}

是否可以在嵌套的过滤聚合中使用嵌套过滤器?在elasticsearch中有一个已知的错误吗?嵌套过滤器在搜索的查询上下文中工作正常,如果我不使用嵌套聚合,它可以正常工作。

1 个答案:

答案 0 :(得分:12)

根据所提供的信息和一些假设,我想提出两点建议。我希望它有助于解决您的问题。

案例1 :使用反向嵌套聚合:

{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "rating": {
      "nested": {
        "path": "playerYears.schoolsOfInterest"
      },
      "aggs": {
        "rating-filtered": {
          "filter": {
            "match_all": {}
          },
          "aggs": {
            "rating_nested": {
              "reverse_nested": {},
              "aggs": {
                "rating": {
                  "histogram": {
                    "field": "rating",
                    "interval": 1
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

案例2 :对已过滤聚合的更改:

{
  "size": 0,
  "aggs": {
    "rating-filtered": {
      "filter": {
        "nested": {
          "query": {
            "match_all": {}
          },
          "path": "playerYears.schoolsOfInterest"
        }
      },
      "aggs": {
        "rating": {
          "histogram": {
            "field": "playerYears.rating",
            "interval": 1
          }
        }
      }
    }
  },
  "query": {
    "filtered": {
      "filter": {
        "match_all": {}
      }
    }
  }
}

我建议您使用案例1并验证您所需的结果。