Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type conversion in elasticsearch scripts

I would like to use a script to do the following:

{
  "query": {
  "match_all": {}
},
  "facets": {
    "user_facet": {
      "terms": {
        "field": "user_id",
        "script": "term + \"_\" + _source.code"
      }
    }
  }
}

This is similar to the answer given in this question: elastic search double facet

The only problem I have is my user_id and code fields are long types, rather than strings. Is there a way to make them strings so they can be combined with the script?

The particular error I am getting is this:

nested: ClassCastException[java.lang.String cannot be cast to java.lang.Number];
like image 916
Christopher H Avatar asked Oct 22 '22 06:10

Christopher H


1 Answers

This might sound ridiculous, but I had the same issue, and the way I fixed it (for a VERY similar problems) was to drop the "field" specification so that it looks like this:

{
  "query": {
  "match_all": {}
},
  "facets": {
    "user_facet": {
      "terms": {
        "script": "term + \"_\" + _source.code"
      }
    }
  }
}

I think it was trying to convert the output of the "script" execution back into the type of the field, causing the class cast exception.

like image 96
Kelly Avatar answered Nov 04 '22 19:11

Kelly