0

We are having a tremendous amount of trouble converting an old ElasticSearch query to a newer version of ElasticSearch. The original query for ES 1.8 is:

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "*",
          "default_operator": "AND"
        }
      },
      "filter": {
        "and": [
          {
            "terms": {
              "organization_id": [
                "fred"
              ]
            }
          }
        ]
      }
    }
  },
  "size": 50,
  "sort": {
    "updated": "desc"
  },
  "aggs": {
    "status": {
      "terms": {
        "size": 0,
        "field": "status"
      }
    },
    "tags": {
      "terms": {
        "size": 0,
        "field": "tags"
      }
    }
  }
}

and we are trying to convert it to ES version 7. Does anyone know how to do that?

  • here is my attempt, but it's not quite right and I don't know what to do with "aggs" - https://gist.github.com/ORESoftware/4fb70d2f0a3b8a0c48e5452efe634f66 –  Jul 06 '20 at 19:56

1 Answers1

1

The Elasicsearch docs for Filtered query in 6.8 (the latest version of the docs I can find that has the page) state that you should move the query and filter to the must and filter parameters in the bool query.

Also, the terms aggregation no longer support setting size to 0 to get Integer.MAX_VALUE. If you really want all the terms, you need to set it to the max value (2147483647) explicitly. However, the documentation for Size recommends using the Composite aggregation instead and paginate.

Below is the closest query I could make to the original that will work with Elasticsearch 7.

{
  "query": {
    "bool": {
      "must": {
        "query_string": {
          "query": "*",
          "default_operator": "AND"
        }
      },
      "filter": {
        "terms": {
          "organization_id": [
            "fred"
          ]
        }
      }
    }
  },
  "size": 50,
  "sort": {
    "updated": "desc"
  },
  "aggs": {
    "status": {
      "terms": {
        "size": 2147483647,
        "field": "status"
      }
    },
    "tags": {
      "terms": {
        "size": 2147483647,
        "field": "tags"
      }
    }
  }
}
markusk
  • 6,477
  • 34
  • 39