Home > OS >  How to change default value size parameter of the terms aggregation when multiple values in field?
How to change default value size parameter of the terms aggregation when multiple values in field?

Time:05-11

According to the Elasticsearch documentation, the "terms" aggregation returns the top ten terms with the most documents. In my particular case, I am sending 14 required values to count on.

{
  "size": 0,
  "aggs": {
    "counts": {
      "filters": {
        "filters": {
          "respondents": {
            "bool": {
              "should": [
                {
                  "terms": {
                    "my_field": [
                      "1",
                      "2",
                      "3",
                      "4",
                      "5",
                      "6",
                      "7",
                      "8",
                      "9",
                      "10",
                      "11",
                      "12",
                      "13",
                      "14"
                    ],
                    "size": 20 // this is not working
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}

But then 4 of them are not returned. If I add the "size" property side by side with "my_field" property, then an error is returned:

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[terms] query does not support [size]",
        "line": 40,
        "col": 49
      }
    ],
    "type": "parsing_exception",
    "reason": "[terms] query does not support [size]",
    "line": 40,
    "col": 49
  },
  "status": 400
}

What should I do to be able to get all 14 required values?

CodePudding user response:

You are missing term aggregation and filter aggregation doesn't support size param as it will apply filter on aggregation query.

{
  "size": 0,
  "aggs": {
    "counts": {
      "filters": {
        "filters": {
          "respondents": {
            "bool": {
              "should": [
                {
                  "terms": {
                    "my_field": [
                      "1",
                      "2",
                      "3",
                      "4",
                      "5",
                      "6",
                      "7",
                      "8",
                      "9",
                      "10",
                      "11",
                      "12",
                      "13",
                      "14"
                    ]
                  }
                }
              ]
            }
          }
        }
      },
      "aggs": {
        "count": {
          "terms": {
            "field": "my_field",
            "size": 20
          }
        }
      }
    }
  }
}
  • Related