Skip to content

Commit 2cb7bb7

Browse files
authored
Block linkage: allow a block linker with Row to Query (#154)
* [linkage] block linker with => Query * [linkage] block linker is Row => Query * remove Query analyzer on methods
1 parent 63d538d commit 2cb7bb7

6 files changed

Lines changed: 54 additions & 14 deletions

File tree

src/main/scala/org/zouzias/spark/lucenerdd/LuceneRDD.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ object LuceneRDD extends Versionable
469469
*
470470
* @param queries Queries / entities to be linked with @corpus
471471
* @param entities DataFrame of entities to be linked with queries parameter
472-
* @param rowToQueryString Converts each [[Row]] to a 'Lucene Query Syntax'
472+
* @param rowToQuery Function[Row, Query] that converts [[Row]] to a Lucene [[Query]]
473473
* @param queryPartColumns List of query columns for [[HashPartitioner]]
474474
* @param entityPartColumns List of entity columns for [[HashPartitioner]]
475475
* @param topK Number of linked results
@@ -481,7 +481,7 @@ object LuceneRDD extends Versionable
481481
*/
482482
def blockEntityLinkage(queries: DataFrame,
483483
entities: DataFrame,
484-
rowToQueryString: Row => String,
484+
rowToQuery: Row => Query,
485485
queryPartColumns: Array[String],
486486
entityPartColumns: Array[String],
487487
topK : Int = 3,
@@ -521,7 +521,7 @@ object LuceneRDD extends Versionable
521521
queryAnalyzer, similarity)
522522

523523
// Multi-query lucene index
524-
qs.map(q => (q, lucenePart.query(rowToQueryString(q), topK).results.toArray))
524+
qs.map(q => (q, lucenePart.query(rowToQuery(q), topK).results.toArray))
525525
}
526526
}
527527
}
@@ -530,7 +530,7 @@ object LuceneRDD extends Versionable
530530
* Deduplication via blocking
531531
*
532532
* @param entities Entities [[DataFrame]] to deduplicate
533-
* @param rowToQueryString Function that maps [[Row]] to Lucene Query String
533+
* @param rowToQuery Function that maps [[Row]] to Lucene [[Query]]
534534
* @param blockingColumns Columns on which exact match is required
535535
* @param topK Number of top-K query results
536536
* @param indexAnalyzer Lucene analyzer at index time
@@ -541,7 +541,7 @@ object LuceneRDD extends Versionable
541541
* @return
542542
*/
543543
def blockDedup(entities: DataFrame,
544-
rowToQueryString: Row => String,
544+
rowToQuery: Row => Query,
545545
blockingColumns: Array[String],
546546
topK : Int = 3,
547547
indexAnalyzer: String = getOrElseEn(IndexAnalyzerConfigName),
@@ -575,7 +575,7 @@ object LuceneRDD extends Versionable
575575
queryAnalyzer, similarity)
576576

577577
// Multi-query lucene index
578-
iterQueries.map(q => (q, lucenePart.query(rowToQueryString(q), topK).results.toArray))
578+
iterQueries.map(q => (q, lucenePart.query(rowToQuery(q), topK).results.toArray))
579579
}
580580
}
581581
}

src/main/scala/org/zouzias/spark/lucenerdd/partition/AbstractLuceneRDDPartition.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
package org.zouzias.spark.lucenerdd.partition
1818

19-
import org.apache.lucene.search.BooleanClause
19+
import org.apache.lucene.search.{BooleanClause, Query}
2020
import org.zouzias.spark.lucenerdd.models.indexstats.IndexStatistics
2121
import org.zouzias.spark.lucenerdd.models.{SparkFacetResult, TermVectorEntry}
2222
import org.zouzias.spark.lucenerdd.response.LuceneRDDResponsePartition
@@ -62,6 +62,16 @@ private[lucenerdd] abstract class AbstractLuceneRDDPartition[T] extends Serializ
6262
*/
6363
def query(searchString: String, topK: Int): LuceneRDDResponsePartition
6464

65+
66+
/**
67+
* Lucene search using Lucene [[Query]]
68+
* @param query Lucene query, i.e., [[org.apache.lucene.search.BooleanQuery]] or
69+
* [[org.apache.lucene.search.PhraseQuery]]
70+
* @param topK Number of documents to return
71+
* @return
72+
*/
73+
def query(query: Query, topK: Int): LuceneRDDResponsePartition
74+
6575
/**
6676
* Multiple generic Lucene Queries using QueryParser
6777
* @param searchString Lucene query string

src/main/scala/org/zouzias/spark/lucenerdd/partition/LuceneRDDPartition.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ private[lucenerdd] class LuceneRDDPartition[T]
137137
LuceneRDDResponsePartition(results.toIterator)
138138
}
139139

140+
override def query(query: Query,
141+
topK: Int): LuceneRDDResponsePartition = {
142+
val results = LuceneQueryHelpers.searchQuery(indexSearcher, query, topK)
143+
144+
LuceneRDDResponsePartition(results.toIterator)
145+
}
146+
140147
override def queries(searchStrings: Iterable[String],
141148
topK: Int): Iterable[(String, LuceneRDDResponsePartition)] = {
142149
searchStrings.map( searchString =>

src/main/scala/org/zouzias/spark/lucenerdd/query/LuceneQueryHelpers.scala

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,35 @@ object LuceneQueryHelpers extends Serializable {
9595
*
9696
* @param indexSearcher Index searcher
9797
* @param searchString Lucene search query string
98-
* @param topK Number of returned documents
98+
* @param topK Number of documents to return
9999
* @param analyzer Lucene Analyzer
100100
* @return
101101
*/
102102
def searchParser(indexSearcher: IndexSearcher,
103103
searchString: String,
104-
topK: Int, analyzer: Analyzer)
104+
topK: Int,
105+
analyzer: Analyzer)
105106
: Seq[SparkScoreDoc] = {
106107
val q = parseQueryString(searchString, analyzer)
107108
indexSearcher.search(q, topK).scoreDocs.map(SparkScoreDoc(indexSearcher, _))
108109
}
109110

111+
/**
112+
* Lucene search using a Lucene [[Query]]
113+
*
114+
* Important: Query analysis is done during the definition of query
115+
* @param indexSearcher Lucene index searcher
116+
* @param query Lucene query
117+
* @param topK Number of documents to return
118+
* @return
119+
*/
120+
def searchQuery(indexSearcher: IndexSearcher,
121+
query: Query,
122+
topK: Int)
123+
: Seq[SparkScoreDoc] = {
124+
indexSearcher.search(query, topK).scoreDocs.map(SparkScoreDoc(indexSearcher, _))
125+
}
126+
110127
/**
111128
* Faceted search using [[SortedSetDocValuesFacetCounts]]
112129
*

src/test/scala/org/zouzias/spark/lucenerdd/BlockingDedupSpec.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.zouzias.spark.lucenerdd
1818

1919
import com.holdenkarau.spark.testing.SharedSparkContext
20+
import org.apache.lucene.index.Term
21+
import org.apache.lucene.search.{Query, TermQuery}
2022
import org.apache.spark.SparkConf
2123
import org.apache.spark.sql.{Row, SparkSession}
2224
import org.scalatest.{BeforeAndAfterEach, FlatSpec, Matchers}
@@ -44,10 +46,11 @@ class BlockingDedupSpec extends FlatSpec
4446
}
4547
val df = sc.parallelize(people).repartition(2).toDF()
4648

47-
val linker: Row => String = { row =>
49+
val linker: Row => Query = { row =>
4850
val name = row.getString(row.fieldIndex("name"))
51+
val term = new Term("name", name)
4952

50-
s"name:$name"
53+
new TermQuery(term)
5154
}
5255

5356

src/test/scala/org/zouzias/spark/lucenerdd/BlockingLinkageSpec.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.zouzias.spark.lucenerdd
1818

1919
import com.holdenkarau.spark.testing.SharedSparkContext
20+
import org.apache.lucene.index.Term
21+
import org.apache.lucene.search.{Query, TermQuery}
2022
import org.apache.spark.SparkConf
2123
import org.apache.spark.sql.{Row, SparkSession}
2224
import org.scalatest.{BeforeAndAfterEach, FlatSpec, Matchers}
@@ -52,11 +54,12 @@ class BlockingLinkageSpec extends FlatSpec
5254
val leftDF = sc.parallelize(peopleLeft).repartition(2).toDF()
5355
val rightDF = sc.parallelize(peopleRight).repartition(3).toDF()
5456

55-
56-
val linker: Row => String = { row =>
57+
// Define a Lucene Term linker
58+
val linker: Row => Query = { row =>
5759
val name = row.getString(row.fieldIndex("name"))
60+
val term = new Term("name", name)
5861

59-
s"name:$name"
62+
new TermQuery(term)
6063
}
6164

6265

0 commit comments

Comments
 (0)