-
Notifications
You must be signed in to change notification settings - Fork 692
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
2 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
76 changes: 76 additions & 0 deletions
76
...ala/com/sksamuel/elastic4s/requests/searches/queries/SparseVectorQueryBuilderFnTest.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,76 @@ | ||
package com.sksamuel.elastic4s.requests.searches.queries | ||
|
||
import com.sksamuel.elastic4s.JsonSugar | ||
import com.sksamuel.elastic4s.api.QueryApi | ||
import com.sksamuel.elastic4s.handlers.searches.queries.SparseVectorQueryBuilderFn | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
class SparseVectorQueryBuilderFnTest extends AnyFunSuite with QueryApi with Matchers with JsonSugar { | ||
test("Should correctly build minimal sparse vector query") { | ||
val query = SparseVectorQuery("testfield") | ||
|
||
val queryBody = SparseVectorQueryBuilderFn(query) | ||
|
||
queryBody.string shouldBe """{"sparse_vector":{"field":"testfield"}}""" | ||
} | ||
|
||
test("Should correctly build sparse vector query using an nlp model") { | ||
val query = sparseVectorQuery("ml.tokens", "the inference ID to produce the token weights", "the query string") | ||
|
||
val queryBody = SparseVectorQueryBuilderFn(query) | ||
|
||
queryBody.string should matchJson("""{"sparse_vector": { | ||
| "field":"ml.tokens", | ||
| "inference_id":"the inference ID to produce the token weights", | ||
| "query":"the query string" | ||
|}}""".stripMargin.replace("\n", "")) | ||
} | ||
|
||
test("Should correctly build sparse vector query using precomputed vectors") { | ||
val query = sparseVectorQuery("ml.tokens", Map("token1" -> 0.5D, "token2" -> 0.3D, "token3" -> 0.2D)) | ||
|
||
val queryBody = SparseVectorQueryBuilderFn(query) | ||
|
||
queryBody.string should matchJson("""{"sparse_vector": { | ||
| "field": "ml.tokens", | ||
| "query_vector": { "token1": 0.5, "token2": 0.3, "token3": 0.2 } | ||
|}}""".stripMargin.replace("\n", "")) | ||
} | ||
|
||
test("Should correctly build sparse vector query with pruning configuration") { | ||
val query = sparseVectorQuery("ml.tokens", "my-elser-model", "How is the weather in Jamaica?") | ||
.prune(true) | ||
.pruningConfig(PruningConfig( | ||
tokensFreqRatioThreshold = Some(5), tokensWeighThreshold = Some(0.4F), onlyScorePrunedTokens = Some(false) | ||
)) | ||
|
||
val queryBody = SparseVectorQueryBuilderFn(query) | ||
|
||
queryBody.string should matchJson("""{"sparse_vector":{ | ||
| "field": "ml.tokens", | ||
| "inference_id": "my-elser-model", | ||
| "query":"How is the weather in Jamaica?", | ||
| "prune": true, | ||
| "pruning_config": { | ||
| "tokens_freq_ratio_threshold": 5, | ||
| "tokens_weight_threshold": 0.4000000059604645, | ||
| "only_score_pruned_tokens": false | ||
| } | ||
|}}""".stripMargin.replace("\n", "")) | ||
} | ||
|
||
test("Supports boost and queryName") { | ||
val query = SparseVectorQuery("testfield") | ||
.boost(1.0D) | ||
.queryName("abc") | ||
|
||
val queryBody = SparseVectorQueryBuilderFn(query) | ||
|
||
queryBody.string should matchJson("""{"sparse_vector": { | ||
| "field": "testfield", | ||
| "boost": 1.0, | ||
| "_name": "abc" | ||
|}}""".stripMargin.replace("\n", "")) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...n/src/main/scala/com/sksamuel/elastic4s/requests/searches/queries/SparseVectorQuery.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,26 @@ | ||
package com.sksamuel.elastic4s.requests.searches.queries | ||
|
||
import com.sksamuel.elastic4s.ext.OptionImplicits.RichOptionImplicits | ||
|
||
case class PruningConfig(tokensFreqRatioThreshold: Option[Int] = None, | ||
tokensWeighThreshold: Option[Float] = None, | ||
onlyScorePrunedTokens: Option[Boolean] = None) | ||
|
||
case class SparseVectorQuery(field: String, | ||
inferenceId: Option[String] = None, | ||
query: Option[String] = None, | ||
queryVector: Map[String, Double] = Map.empty[String, Double], | ||
boost: Option[Double] = None, | ||
queryName: Option[String] = None, | ||
prune: Option[Boolean] = None, | ||
pruningConfig: Option[PruningConfig] = None) | ||
extends Query { | ||
|
||
def boost(boost: Double): SparseVectorQuery = copy(boost = boost.some) | ||
|
||
def queryName(queryName: String): SparseVectorQuery = copy(queryName = queryName.some) | ||
|
||
def prune(prune: Boolean): SparseVectorQuery = copy(prune = prune.some) | ||
|
||
def pruningConfig(pruningConfig: PruningConfig): SparseVectorQuery = copy(pruningConfig = pruningConfig.some) | ||
} |
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
34 changes: 34 additions & 0 deletions
34
...n/scala/com/sksamuel/elastic4s/handlers/searches/queries/SparseVectorQueryBuilderFn.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,34 @@ | ||
package com.sksamuel.elastic4s.handlers.searches.queries | ||
|
||
import com.sksamuel.elastic4s.json.{XContentBuilder, XContentFactory} | ||
import com.sksamuel.elastic4s.requests.searches.queries.{PruningConfig, SparseVectorQuery} | ||
|
||
object SparseVectorQueryBuilderFn { | ||
def apply(q: SparseVectorQuery): XContentBuilder = { | ||
val builder = XContentFactory.jsonBuilder() | ||
builder.startObject("sparse_vector") | ||
builder.field("field", q.field) | ||
|
||
q.inferenceId.foreach(builder.field("inference_id", _)) | ||
q.query.foreach(builder.field("query", _)) | ||
if (q.queryVector.nonEmpty) { | ||
builder.startObject("query_vector") | ||
q.queryVector.foreach { case (k, v) => builder.field(k, v) } | ||
builder.endObject() | ||
} | ||
q.boost.foreach(builder.field("boost", _)) | ||
q.queryName.foreach(builder.field("_name", _)) | ||
q.prune.foreach(builder.field("prune", _)) | ||
q.pruningConfig.foreach { pc => | ||
if (pc.tokensFreqRatioThreshold.nonEmpty || pc.tokensWeighThreshold.nonEmpty || pc.onlyScorePrunedTokens.nonEmpty) { | ||
builder.startObject("pruning_config") | ||
pc.tokensFreqRatioThreshold.foreach(builder.field("tokens_freq_ratio_threshold", _)) | ||
pc.tokensWeighThreshold.foreach(builder.field("tokens_weight_threshold", _)) | ||
pc.onlyScorePrunedTokens.foreach(builder.field("only_score_pruned_tokens", _)) | ||
builder.endObject() | ||
} | ||
} | ||
builder.endObject() | ||
builder | ||
} | ||
} |
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