unknown type for collapse field `xxx`, only keywords and numbers are accepted error

Viewed 2545

If I add a collapse clause to the following query the error

"unknown type for collapse aircraft_type_search, only keywords and numbers are accepted"

is returned.

 {
"query": {
  "bool": {
    "must": {
      "bool": {
        "must": [
          {
            "match": {
              "aircraft_type_search": {
                "query": "piper"
              }
            }
          }
        ]
      }
    },
    "filter": {
      "bool": {
        "must": {
          "term": {
            "display_picture": "yes"
          }
        }
      }
    }
  }
},
"collapse": {
  "field": "aircraft_type_search"
}
}

I have also tried 'aircraft_id' as the collapse field as that is an integer:

'aircraft_id' => [ 'type' => 'integer', "null_value" => "-1", "ignore_malformed" => 'true', 'include_in_all' => 'false', 'index' => 'not_analyzed' ]

but that resulted in the error.

ES7.1 on AWS Elastic Search service running through the elasticsearch php sdk.

If I remove the collapse clause the query works fine.

Any ideas on why this isn't working?

1 Answers

This is because collapse is only for keyword and numbers fields as the error says.

You have two sollutions :

  1. Provide a keyword mapping of your field at the index creation

    PUT my-index
    {
      "mappings": {
        "properties": {
          "aircraft_type_search" : {
            "type": "keyword"
          }
        }
      }
    }

  1. Use the Elastic default mapping that include text and keyword and call the keyword on the query :
    "collapse": {
        "field": "aircraft_type_search.keyword"
      }

In order to pick the best solution, you must understand what is a text or a keyword type. If you don't want to deepen the subject, the easiest way is sollution 2.

Related