-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2941/implement script score query (#2955)
* [#2941] Implement script score query Implements the script score query to fix #2941. For now the ScriptScoreQueryBodyFn2 class has the terrible name because I suspect that the current ScriptScoreQueryBodyFn is never exercised. The ScriptScore that it accepts isn't actually a query, so will never hit the pattern match? * Remove ScriptScoreQueryBodyFn From inspection I suspect this case would never be hit, because ScriptScore doesn't implement the Query trait. This was the only call site for ScriptScoreQueryBodyFn (which has an odd name, given it accepted a ScriptScore and not a ScriptScoreQuery). Therefore I've removed it, and put the ScriptScoreQueryBodyFn2 in its place * Add to query API and test TODO: this test is failing currently --------- Co-authored-by: Sam Briggs <[email protected]>
- Loading branch information
1 parent
cf176de
commit 962c515
Showing
5 changed files
with
92 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 8 additions & 1 deletion
9
...in/src/main/scala/com/sksamuel/elastic4s/requests/searches/queries/ScriptScoreQuery.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
package com.sksamuel.elastic4s.requests.searches.queries | ||
|
||
case class ScriptScoreQuery(script: String) extends Query | ||
import com.sksamuel.elastic4s.requests.script.Script | ||
|
||
case class ScriptScoreQuery(query: Option[Query] = None, script: Option[Script] = None, minScore: Option[Double] = None, boost: Option[Double] = None) extends Query { | ||
def boost(boost: Double): ScriptScoreQuery = copy(boost = Option(boost)) | ||
def minScore(min: Double): ScriptScoreQuery = copy(minScore = Option(min)) | ||
def query(query: Query): ScriptScoreQuery = copy(query = Some(query)) | ||
def script(script: Script): ScriptScoreQuery = copy(script = Some(script)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 9 additions & 3 deletions
12
.../main/scala/com/sksamuel/elastic4s/handlers/searches/queries/ScriptScoreQueryBodyFn.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...c4s-tests/src/test/scala/com/sksamuel/elastic4s/search/queries/ScriptScoreQueryTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.sksamuel.elastic4s.search.queries | ||
|
||
import com.sksamuel.elastic4s.requests.script.Script | ||
import com.sksamuel.elastic4s.testkit.DockerTests | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
import scala.util.Try | ||
|
||
class ScriptScoreQueryTest extends AnyWordSpec with DockerTests with Matchers { | ||
|
||
Try { | ||
client.execute { | ||
deleteIndex("person") | ||
}.await | ||
} | ||
|
||
client.execute( | ||
bulk( | ||
indexInto("person") fields( | ||
"name" -> "reese", | ||
"age" -> 1.0 | ||
), | ||
indexInto("person") fields( | ||
"name" -> "finch", | ||
"age" -> 1.0 | ||
), | ||
indexInto("person") fields( | ||
"name" -> "finch", | ||
"age" -> 2.0 | ||
), | ||
indexInto("person") fields( | ||
"name" -> "finch", | ||
"age" -> 3.0 | ||
) | ||
).refreshImmediately | ||
).await | ||
|
||
"script score query" should { | ||
"filter by query" in { | ||
client.execute { | ||
search("person") query { | ||
scriptScoreQuery(termQuery("name", "finch")) | ||
.script(Script("doc['age'].value % 3")) | ||
} | ||
}.await.result.totalHits shouldBe 3 | ||
} | ||
"rank by custom score" in { | ||
client.execute { | ||
search("person") query { | ||
scriptScoreQuery(termQuery("name", "finch")) | ||
.script(Script("doc['age'].value % 3")) | ||
} | ||
}.await.result.hits.hits.map(_.sourceAsString) shouldBe Array( | ||
"""{"name":"finch","age":2.0}""", | ||
"""{"name":"finch","age":1.0}""", | ||
"""{"name":"finch","age":3.0}""" | ||
) | ||
} | ||
"return correct custom score" in { | ||
client.execute { | ||
search("person") query { | ||
scriptScoreQuery(termQuery("name", "finch")) | ||
.script(Script("doc['age'].value % 3")) | ||
} | ||
}.await.result.hits.hits.map(_.score) shouldBe Array(2.0, 1.0, 0.0) | ||
} | ||
} | ||
} |