Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type cast exception when doing custom_score query

ElasticSearch Version: 0.90.2

I'm trying to get custom_score query working so that it scores documents by recency, but all I got so far is a

ClassCastException[org.elasticsearch.index.fielddata.ScriptDocValues$Longs cannot be cast to java.lang.Integer]

The idea was to index documents with a field having integer type timestamp and then to compare it passing current timestamp parameter in the query. How come that the indexed integer value became long?

Here are the commands to reproduce:

curl -XPUT 'http://localhost:9200/test_longs_problem/'

curl -XPUT 'http://localhost:9200/test_longs_problem/albums/_mapping' -d '
{
    "albums" : {
        "properties" : {
            "date" : {"type" : "integer", "analyzed" : false}
        }
    }
}'

curl -XPUT 'http://localhost:9200/test_longs_problem/albums/1' -d '
{
  "date" : 1376823903
}'

curl -XGET 'http://localhost:9200/test_longs_problem/albums/_search?pretty' -d '{
    "query": {
        "custom_score": {
            "query": {
                "match_all": {}
            },
            "params": {
                "now": 1376823903
            },
            "script": "_score * 0.2 / (3.16 * pow(10, -9) * abs(now - doc[\"date\"]) + 0.05) + 1.0"
        }
    }
}'

throws

{
  "error" : "SearchPhaseExecutionException[Failed to execute phase [query_fetch], total failure; shardFailures {[NSdeWzwNSIeLtGA1mtLTyA][test_longs_problem][0]: QueryPhaseExecutionException[[test_longs_problem][0]: query[filtered(custom score (ConstantScore(*:*),function=script[_score * 0.2 / (3.16 * pow(10, -9) * abs(now - doc[\"date\"]) + 0.05) + 1.0], params [{_source=org.elasticsearch.search.lookup.SourceLookup@7249d155, now=1376823903, _fields=org.elasticsearch.search.lookup.FieldsLookup@4c4e5e11, _doc=org.elasticsearch.search.lookup.DocLookup@2d01d53a, doc=org.elasticsearch.search.lookup.DocLookup@2d01d53a, _score=1.0}]))->cache(_type:albums)],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: CompileException[[Error: uncomparable values <<1376823903>> and <<org.elasticsearch.index.fielddata.ScriptDocValues$Longs@41983ee7>>]\n[Near : {... _score * 0.2 / (3.16 * pow(10, ....}]\n             ^\n[Line: 1, Column: 1]]; nested: RuntimeException[uncomparable values <<1376823903>> and <<org.elasticsearch.index.fielddata.ScriptDocValues$Longs@41983ee7>>]; nested: ClassCastException[org.elasticsearch.index.fielddata.ScriptDocValues$Longs cannot be cast to java.lang.Integer]; }]",
  "status" : 500
}
like image 496
Max Ivanov Avatar asked Aug 20 '13 11:08

Max Ivanov


1 Answers

You need to use .value property in order to get value of the field. In other words, your script should look like this:

"_score * 0.2 / (3.16 * pow(10, -9) * abs(now - doc[\"date\"].value) + 0.05) + 1.0"
like image 117
imotov Avatar answered Oct 21 '22 19:10

imotov