Elasticsearch缺少aggs

时间:2016-01-15 13:02:14

标签: elasticsearch

我正在进行Elasticsearch查询,并且遇到了缺少aggs的问题。

如果我执行下面的查询,我会毫无问题地收回aggs:

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "*wet*",
          "fields": [
            "Name",
            "Summary",
            "Description",
            "Location",
            "Features",
            "TypeName",
            "CategoryName"
          ]
        },
        "filter": {
          "term": {
            "TypeID": "13"
          }
        }
      }
    }
  },
  "aggs": {
    "type": {
      "terms": {
        "field": "TypeID"
      }
    },
    "category": {
      "terms": {
        "field": "CategoryID"
      }
    },
    "max_price": {
      "max": {
        "field": "Price"
      }
    },
    "min_price": {
      "min": {
        "field": "Price"
      }
    },
    "filter-type": {
      "term": {
        "TypeID": "13"
      }
    }
  },
  "from": 0,
  "size": 50,
  "sort": {
    "_score": {
      "order": "desc"
    }
  },
  "explain": false
}

但是,只要添加过滤器,就不再返回aggs。我看不出我做错了什么,所以任何帮助都会非常感激。

不返回aggs的那个看起来像这样:

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "*wet*",
          "fields": [
            "Name",
            "Summary",
            "Description",
            "Location",
            "Features",
            "TypeName",
            "CategoryName"
          ]
        },
        "filter": {
          "term": {
            "TypeID": "13"
          }
        }
      }
    }
  },
  "aggs": {
    "type": {
      "terms": {
        "field": "TypeID"
      }
    },
    "category": {
      "terms": {
        "field": "CategoryID"
      }
    },
    "max_price": {
      "max": {
        "field": "Price"
      }
    },
    "min_price": {
      "min": {
        "field": "Price"
      }
    },
    "filter-type": {
      "term": {
        "TypeID": "13"
      }
    }
  },
  "from": 0,
  "size": 50,
  "sort": {
    "_score": {
      "order": "desc"
    }
  },
  "explain": false
}

1 个答案:

答案 0 :(得分:0)

这是filters aggregation的正确语法。你必须将所有聚合包装在像这样的

{
  "size": 0,
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "*wet*",
          "fields": [
            "Name",
            "Summary",
            "Description",
            "Location",
            "Features",
            "TypeName",
            "CategoryName"
          ]
        }
      },
      "filter": {
        "term": {
          "TypeID": "13"
        }
      }
    }
  },
  "aggs": {
    "filter-type": {
      "filter": {
        "term": {
          "TypeID": "13"
        }
      },
      "aggs": {
        "type": {
          "terms": {
            "field": "TypeID"
          }
        },
        "category": {
          "terms": {
            "field": "CategoryID"
          }
        },
        "max_price": {
          "max": {
            "field": "Price"
          }
        },
        "min_price": {
          "min": {
            "field": "Price"
          }
        }
      }
    }
  }
}

同样在这种情况下,filter中的aggs是多余的,因为您已在查询中使用相同的过滤器。