-
Notifications
You must be signed in to change notification settings - Fork 49
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
1 parent
f1cb38a
commit f4d0a68
Showing
6 changed files
with
120 additions
and
27 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
client-elastic4s/src/main/scala/com/klibisz/elastiknn/client/Elastic4sCompatibility.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,22 @@ | ||
package com.klibisz.elastiknn.client | ||
|
||
import com.klibisz.elastiknn.ELASTIKNN_NAME | ||
import com.klibisz.elastiknn.api.{ElasticsearchCodec, Mapping, NearestNeighborsQuery} | ||
import com.sksamuel.elastic4s.{XContentBuilder, XContentFactory} | ||
import com.sksamuel.elastic4s.requests.searches.queries.{CustomQuery, Query} | ||
|
||
import scala.language.implicitConversions | ||
|
||
object Elastic4sCompatibility { | ||
|
||
implicit def convertQuery(nnq: NearestNeighborsQuery): Query = nnq.toQuery | ||
|
||
implicit class NearestNeighborsQueryCompat(nnq: NearestNeighborsQuery) { | ||
def toQuery: Query = new CustomQuery { | ||
override def buildQueryBody(): XContentBuilder = | ||
XContentFactory.jsonBuilder.rawField(s"${ELASTIKNN_NAME}_nearest_neighbors", ElasticsearchCodec.nospaces(nnq)) | ||
|
||
} | ||
} | ||
|
||
} |
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
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
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
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
66 changes: 66 additions & 0 deletions
66
testing/src/test/scala/com/klibisz/elastiknn/query/NearestNeighborsQuerySpec.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,66 @@ | ||
package com.klibisz.elastiknn.query | ||
|
||
import com.klibisz.elastiknn.api._ | ||
import com.klibisz.elastiknn.client.Elastic4sCompatibility._ | ||
import com.klibisz.elastiknn.testing.{ElasticAsyncClient, SilentMatchers} | ||
import com.sksamuel.elastic4s.ElasticDsl._ | ||
import com.sksamuel.elastic4s.XContentFactory | ||
import com.sksamuel.elastic4s.requests.common.RefreshPolicy | ||
import org.scalatest.{AsyncFunSpec, Inspectors, Matchers} | ||
|
||
import scala.util.Random | ||
|
||
class NearestNeighborsQuerySpec extends AsyncFunSpec with Matchers with Inspectors with ElasticAsyncClient with SilentMatchers { | ||
|
||
// https://github.com/alexklibisz/elastiknn/issues/60 | ||
describe("Vectors in nested fields") { | ||
implicit val rng: Random = new Random(0) | ||
val index = "test-queries-nested-fields" | ||
val vec = Vec.DenseFloat.random(10) | ||
val mapping = Mapping.DenseFloat(vec.values.length) | ||
val nestedFields = Seq( | ||
"vec", | ||
"foo.vec", | ||
"foo.bar.vec", | ||
"foo.bar.baz.vec" | ||
) | ||
|
||
for { | ||
nestedField <- nestedFields | ||
} yield { | ||
val (mappingSource, docSource) = { | ||
val subFields = nestedField.split('.') | ||
val xcbMapping = XContentFactory.obj() | ||
val xcbDoc = XContentFactory.obj() | ||
xcbMapping.startObject("properties") | ||
subFields.init.foreach { f => | ||
xcbMapping.startObject(f) | ||
xcbMapping.startObject("properties") | ||
xcbDoc.startObject(f) | ||
} | ||
xcbMapping.rawField(subFields.last, ElasticsearchCodec.mapping(mapping).spaces2) | ||
xcbDoc.rawField(subFields.last, ElasticsearchCodec.vector(vec).spaces2) | ||
subFields.init.foreach { _ => | ||
xcbMapping.endObject() | ||
xcbMapping.endObject() | ||
xcbDoc.endObject() | ||
} | ||
xcbMapping.endObject() | ||
xcbDoc.endObject() | ||
(xcbMapping.string(), xcbDoc.string()) | ||
} | ||
it(s"works with nested field: $nestedField") { | ||
for { | ||
_ <- deleteIfExists(index) | ||
_ <- eknn.execute(createIndex(index)) | ||
_ <- eknn.execute(putMapping(index).rawSource(mappingSource)) | ||
_ <- eknn.execute(indexInto(index).source(docSource).refresh(RefreshPolicy.IMMEDIATE)) | ||
res <- eknn.execute(search(index).query(NearestNeighborsQuery.Exact(nestedField, Similarity.L2, vec))) | ||
} yield { | ||
res.result.hits.hits should have length 1 | ||
res.result.hits.maxScore shouldBe 1.0 | ||
} | ||
} | ||
} | ||
} | ||
} |