Skip to content

Instantly share code, notes, and snippets.

Created November 1, 2011 23:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1332314 to your computer and use it in GitHub Desktop.
Save anonymous/1332314 to your computer and use it in GitHub Desktop.
CustomScoreQueryBuilder param values cast to java.lang.String
package test
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest
import org.elasticsearch.action.search.SearchType
import org.elasticsearch.common.settings.ImmutableSettings
import org.elasticsearch.index.query.QueryBuilders._
import org.elasticsearch.node.NodeBuilder._
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest
import org.elasticsearch.client.Requests._
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest
import org.elasticsearch.action.search._
import org.elasticsearch.script.AbstractDoubleSearchScript
import org.elasticsearch.script.NativeScriptFactory
import org.elasticsearch.index.field.data.strings.StringDocFieldData
class Test {
override def toString() = {
"foobar"
}
}
// using java.lang.Object instead of AnyRef does not affect the results
class MyScript(params: java.util.Map[String, AnyRef]) extends AbstractDoubleSearchScript {
override def runAsDouble() = {
try {
params.get("test").asInstanceOf[Test] // String (?) cannot be cast to Test
} catch {
case e: Exception => e.printStackTrace
}
1.0
}
}
class MyScriptFactory extends NativeScriptFactory {
override def newScript(params: java.util.Map[String, AnyRef]) = {
new MyScript(params)
}
}
object Bug extends App {
val settings = ImmutableSettings.settingsBuilder
.put("script.native.myscript.type", "test.MyScriptFactory")
.put("script.native.myscript.lang", "native")
val node = nodeBuilder().settings(settings).local(true).node
val client = node.client
try {
client.admin.indices.create(new CreateIndexRequest("twitter")).actionGet()
} catch {
case e => println("Index already exists " + e);
}
client.admin.cluster.health(new ClusterHealthRequest("twitter").waitForYellowStatus).actionGet()
val source = """{"message": "hi1", "field": "1", "obj":{"abc": "123"}}"""
val indexResponse = client.prepareIndex("twitter", "tweet")
.setSource(source)
.setId("id1")
.execute()
.actionGet()
client.admin().indices().refresh(refreshRequest()).actionGet()
val searchHits = client.prepareSearch("twitter")
.setTypes("tweet")
.setSearchType(SearchType.QUERY_AND_FETCH)
.setQuery(
customScoreQuery(queryString("hi*").field("message"))
.lang("native")
.script("myscript")
.param("test", new test.Test)
)
.execute()
.actionGet()
.hits.hits
node.close ()
}
java.lang.ClassCastException: java.lang.String cannot be cast to test.Test
at test.MyScript.runAsDouble(Bug.scala:26)
at org.elasticsearch.script.AbstractDoubleSearchScript.runAsFloat(AbstractDoubleSearchScript.java:39)
at org.elasticsearch.index.query.CustomScoreQueryParser$ScriptScoreFunction.score(CustomScoreQueryParser.java:113)
at org.elasticsearch.common.lucene.search.function.FunctionScoreQuery$CustomBoostFactorScorer.score(FunctionScoreQuery.java:165)
at org.apache.lucene.search.FilteredQuery$1$1.score(FilteredQuery.java:164)
at org.apache.lucene.search.TopScoreDocCollector$InOrderTopScoreDocCollector.collect(TopScoreDocCollector.java:47)
at org.apache.lucene.search.Scorer.score(Scorer.java:90)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:526)
at org.elasticsearch.search.internal.ContextIndexSearcher.search(ContextIndexSearcher.java:198)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:391)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:298)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:286)
at org.elasticsearch.search.query.QueryPhase.execute(QueryPhase.java:217)
at org.elasticsearch.search.SearchService.executeFetchPhase(SearchService.java:304)
at org.elasticsearch.search.action.SearchServiceTransportAction.sendExecuteFetch(SearchServiceTransportAction.java:224)
at org.elasticsearch.action.search.type.TransportSearchQueryAndFetchAction$AsyncAction.sendExecuteFirstPhase(TransportSearchQueryAndFetchAction.java:71)
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:205)
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:192)
at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$2.run(TransportSearchTypeAction.java:178)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment