使用过滤器

时间:2015-12-17 01:28:06

标签: elasticsearch

我不确定此查询有什么问题,但我无法找到解决方案,并希望您的见解。我试图用过滤器查询进行嵌套对象的聚合。我想计算fields.nameAMORTIZED_COST的字段对象的数量。以某种方式聚合不会返回任何计数。我确实在主搜索查询中得到了实际文档 - 它返回4个文档,但总共有6个字段行符合AMORTIZED_COST条件,因此在聚合中我希望它会显示6行。在此先感谢您的帮助。

仅供参考 - 我使用的是ES 1.5

我正在分享部分类型映射

   "templ01": {
      "mappings": {
         "tempobjects": {
            "properties": {
               "description": {
                  "type": "string"
               },
               "fields": {
                  "type": "nested",
                  "properties": {
                     "id": {
                        "type": "string"
                     },
                     "name": {
                        "type": "string"
                     },
                     "objid": {
                        "type": "string"
                     }
                  }
               },
               "formulas": {
                  "type": "nested",
                  "properties": {
                     "formula": {
                        "type": "string"
                     },
                     "id": {
                        "type": "string"
                     },
                     "name": {
                        "type": "string"
                     },
                     "objid": {
                        "type": "string"
                     }
                  }
               },

这是搜索查询

{
  "query" : {
    "filtered" : {
      "query" : {
        "match_all" : { }
      },
      "filter" : {
        "query" : {
          "nested" : {
            "query" : {
              "match" : {
                "fields.name" : {
                  "query" : "AMORTIZED_COST",
                  "type" : "boolean"
                }
              }
            },
            "path" : "fields",
            "inner_hits" : {
              "name" : "fields"
            }
          }
        }
      }
    }
  },
  "fields" : "_sourceall",
  "aggregations" : {
    "3ztDl2iVtc" : {
      "nested" : {
        "path" : "fields"
      },
      "aggregations" : {
        "filter6yXyIpHISm" : {
          "filters" : {
            "filters" : [ {
              "query" : {
                "match" : {
                  "name" : {
                    "query" : "AMORTIZED_COST",
                    "type" : "boolean"
                  }
                }
              }
            } ]
          },
          "aggregations" : {
            "groupIBa1xQNOuK" : {
              "terms" : {
                "field" : "fields.name"
              }
            }
          }
        }
      }
    }
  }
}

以下是输出。

"aggregations": {
      "3ztDl2iVtc": {
         "doc_count": 282,
         "filter6yXyIpHISm": {
            "buckets": [
               {
                  "doc_count": 0,
                  "groupIBa1xQNOuK": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": []
                  }
               }
            ]

1 个答案:

答案 0 :(得分:0)

在您的汇总过滤器中,您需要匹配fields.name,而不仅仅是name,因为过滤器位于nested聚合内:

  ...
  "aggregations" : {
    "3ztDl2iVtc" : {
      "nested" : {
        "path" : "fields"
      },
      "aggregations" : {
        "filter6yXyIpHISm" : {
          "filters" : {
            "filters" : [ {
              "query" : {
                "match" : {
                  "fields.name" : {              <--- change this
                    "query" : "AMORTIZED_COST",
                    "type" : "boolean"
                  }
                }
              }
            } ]
          },
          "aggregations" : {
            "groupIBa1xQNOuK" : {
              "terms" : {
                "field" : "fields.name"
              }
            }
          }
        }
      }
    }
  }