5

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];
Community
  • 1
  • 1
Christopher H
  • 1,674
  • 5
  • 21
  • 29
  • I don't see from your example how a long user_id can ever match the term that you are calculating in the script, which is a sting, even if you did convert it. I must be missing something. – Phil Jul 28 '13 at 20:31
  • One thought to try... Maybe using `doc['user_id']` for the field would work? – Phil Jul 28 '13 at 21:16
  • You can use Java within an mvel script too. I would just convert them to Strings using java then ;) – javanna Sep 10 '13 at 04:52

1 Answers1

3

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.

Kelly
  • 31
  • 2