Skip to content

Commit

Permalink
Added Lucene support for parsed queries
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed May 28, 2024
1 parent f2181f9 commit 5df094d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions all/src/test/scala/spec/SimpleHaloAndLuceneSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ class SimpleHaloAndLuceneSpec extends AsyncWordSpec with AsyncIOSpec with Matche
distances should be(List(28.555228128634383.miles, 1316.1223938032729.miles))
}
}
"search using tokenized data" in {
"search using tokenized data and a parsed query" in {
Person.query
.filter(Person.search === "john 21")
.filter(Person.search parsed "joh% 21")
.toList
.map { results =>
results.map(_.name) should be(List("John Doe"))
Expand Down
11 changes: 11 additions & 0 deletions lucene/src/main/scala/lightdb/lucene/LuceneIndex.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import lightdb.spatial.GeoPoint
import org.apache.lucene.index.Term
import org.apache.lucene.search._
import org.apache.lucene.document._
import org.apache.lucene.queryparser.classic.QueryParser

case class LuceneIndex[F, D <: Document[D]](fieldName: String,
indexSupport: IndexSupport[D],
Expand All @@ -17,6 +18,8 @@ case class LuceneIndex[F, D <: Document[D]](fieldName: String,
sorted: Boolean,
tokenized: Boolean)
(implicit val rw: RW[F]) extends IndexedField[F, D] {
private def lucene: LuceneSupport[D] = indexSupport.asInstanceOf[LuceneSupport[D]]

lazy val fieldSortName: String = {
val separate = rw.definition.className.collect {
case "lightdb.spatial.GeoPoint" => true
Expand All @@ -34,6 +37,14 @@ case class LuceneIndex[F, D <: Document[D]](fieldName: String,
case json => throw new RuntimeException(s"Unsupported equality check: $json (${rw.definition})")
})

def parsed(query: String, allowLeadingWildcard: Boolean = false): LuceneFilter[D] = {
LuceneFilter(() => {
val parser = new QueryParser(fieldName, lucene.index.analyzer)
parser.setAllowLeadingWildcard(allowLeadingWildcard)
parser.parse(query)
})
}

def IN(values: Seq[F]): LuceneFilter[D] = {
val b = new BooleanQuery.Builder
b.setMinimumNumberShouldMatch(1)
Expand Down

0 comments on commit 5df094d

Please sign in to comment.