# Type cast problem when doing custom\_score query

**URL:** <https://discuss.elastic.co/t/type-cast-problem-when-doing-custom-score-query/13246>\
**Category:** Elasticsearch\
**Created:** [August 18, 2013, 12:04pm UTC](https://discuss.elastic.co/t/type-cast-problem-when-doing-custom-score-query/13246 "2013-08-18T12:04:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max\_Ivanov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/max_ivanov/32/1684_2.png) [@Max\_Ivanov](https://discuss.elastic.co/u/Max_Ivanov)\
**Post date:** [August 18, 2013, 12:04pm UTC](https://discuss.elastic.co/t/type-cast-problem-when-doing-custom-score-query/13246/1 "2013-08-18T12:04:33Z")

</div>

Hi folks,

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$Longscannot 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?

And further question, if I change current timestamp in query to something  
more long-like (1376823903 =\> 13768239030000) it still throws an exception:  
ClassCastException[org.elasticsearch.index.fielddata.ScriptDocValues$Longscannot be cast to java  
.lang.Long]

Here's the commands to reproduce:

curl -XPUT '[http://localhost:9200/test\_longs\_problem/](http://localhost:9200/test_longs_problem/)'

curl -XPUT '[http://localhost:9200/test\_longs\_problem/albums/\_mapping](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](http://localhost:9200/test_longs_problem/albums/1)' -d '  
{  
"date" : 1376823903  
}'

curl -XGET '[http://localhost:9200/test\_longs\_problem/albums/\_search?pretty](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](mailto:org.elasticsearch.index.fielddata.ScriptDocValues%24Longs@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](mailto:org.elasticsearch.index.fielddata.ScriptDocValues%24Longs@41983ee7)\>]; nested: ClassCastException[org.elasticsearch.index.fielddata.ScriptDocValues$Longs cannot be cast to java.lang.Integer]; }]",  
"status" : 500  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Max\_Ivanov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/max_ivanov/32/1684_2.png) [@Max\_Ivanov](https://discuss.elastic.co/u/Max_Ivanov)\
**Post date:** [August 18, 2013, 8:14pm UTC](https://discuss.elastic.co/t/type-cast-problem-when-doing-custom-score-query/13246/2 "2013-08-18T20:14:29Z")

</div>

Elasticsearch Version: 0.90.2

On Sunday, August 18, 2013 4:04:33 PM UTC+4, Max Ivanov wrote:

> Hi folks,
> 
> 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$Longscannot 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?
> 
> And further question, if I change current timestamp in query to something  
> more long-like (1376823903 =\> 13768239030000) it still throws an  
> exception:  
> ClassCastException[org.elasticsearch.index.fielddata.ScriptDocValues$Longscannot be cast to java  
> .lang.Long]
> 
> Here's the commands to reproduce:
> 
> curl -XPUT '[http://localhost:9200/test\_longs\_problem/](http://localhost:9200/test_longs_problem/)'
> 
> curl -XPUT '[http://localhost:9200/test\_longs\_problem/albums/\_mapping](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](http://localhost:9200/test_longs_problem/albums/1)' -d '  
> {  
> "date" : 1376823903  
> }'
> 
> curl -XGET '[http://localhost:9200/test\_longs\_problem/albums/\_search?pretty](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](mailto: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](mailto:org.elasticsearch.index.fielddata.ScriptDocValues$Longs@41983ee7)\>]; nested: ClassCastException[org.elasticsearch.index.fielddata.ScriptDocValues$Longs cannot be cast to java.lang.Integer]; }]",  
> "status" : 500  
> }

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:20am UTC](https://discuss.elastic.co/t/type-cast-problem-when-doing-custom-score-query/13246/3 "2017-07-06T02:20:41Z")

</div>


