Apache Solr open-source search software
LUCENE-5929: highlight terms from block join queries too
Michael McCandless8af67c9a95b4
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex a79a32894fb..a7d22dc8aab 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -95,6 +95,9 @@ New Featureseach context passed to Analyzing/BlendedInfixSuggester (ArcadiusAhouansou, jane chang via Mike McCandless)+* LUCENE-5929: Also extract terms to highlight from block join+ queries. (Julie Tibshirani via Mike McCandless)+API Changes* LUCENE-5900: Deprecated more constructors taking Version in *InfixSuggester anddiff --git a/lucene/benchmark/build.xml b/lucene/benchmark/build.xmlindex f6a2a9a460c..320c0d3baf2 100644--- a/lucene/benchmark/build.xml+++ b/lucene/benchmark/build.xml@@ -174,6 +174,7 @@<pathelement path="${spatial.jar}"/><pathelement path="${queries.jar}"/><pathelement path="${codecs.jar}"/>+ <pathelement path="${join.jar}"/><path refid="base.classpath"/><fileset dir="lib"/></path>@@ -276,7 +277,7 @@<echo>Benchmark output in JIRA table format is in file: ${shingle.jira.output.file}</echo></target>- <target name="init" depends="module-build.init,jar-memory,jar-highlighter,jar-analyzers-common,jar-queryparser,jar-facet,jar-spatial,jar-codecs"/>+ <target name="init" depends="module-build.init,jar-memory,jar-highlighter,jar-analyzers-common,jar-queryparser,jar-facet,jar-spatial,jar-codecs,jar-join"/><target name="compile-test" depends="copy-alg-files-for-testing,module-build.compile-test"/><target name="copy-alg-files-for-testing" description="copy .alg files as resources for testing">diff --git a/lucene/highlighter/build.xml b/lucene/highlighter/build.xmlindex e89db8883e9..b1e4b819b64 100644--- a/lucene/highlighter/build.xml+++ b/lucene/highlighter/build.xml@@ -31,10 +31,13 @@<path id="classpath"><pathelement path="${memory.jar}"/><pathelement path="${queries.jar}"/>+ <pathelement path="${join.jar}"/><path refid="base.classpath"/></path>- <target name="compile-core" depends="jar-memory, common.compile-core" />+ <target name="init" depends="module-build.init,jar-memory,jar-queries,jar-join"/>++ <target name="compile-core" depends="jar-memory, common.compile-core, jar-join" /><target name="javadocs" depends="javadocs-memory,compile-core,check-javadocs-uptodate"unless="javadocs-uptodate-${name}"><invoke-module-javadoc>diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java b/lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.javaindex bfd44659023..bb0c2dcfd02 100644--- a/lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java+++ b/lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java@@ -44,6 +44,8 @@ import org.apache.lucene.index.Terms;import org.apache.lucene.index.memory.MemoryIndex;import org.apache.lucene.queries.CommonTermsQuery;import org.apache.lucene.search.*;+import org.apache.lucene.search.join.ToChildBlockJoinQuery;+import org.apache.lucene.search.join.ToParentBlockJoinQuery;import org.apache.lucene.search.spans.FieldMaskingSpanQuery;import org.apache.lucene.search.spans.SpanFirstQuery;import org.apache.lucene.search.spans.SpanNearQuery;@@ -154,6 +156,10 @@ public class WeightedSpanTermExtractor {for (Iterator<Query> iterator = ((DisjunctionMaxQuery) query).iterator(); iterator.hasNext();) {extract(iterator.next(), terms);}+ } else if (query instanceof ToParentBlockJoinQuery) {+ extract(((ToParentBlockJoinQuery) query).getChildQuery(), terms);+ } else if (query instanceof ToChildBlockJoinQuery) {+ extract(((ToChildBlockJoinQuery) query).getParentQuery(), terms);} else if (query instanceof MultiPhraseQuery) {final MultiPhraseQuery mpq = (MultiPhraseQuery) query;final List<Term[]> termArrays = mpq.getTermArrays();diff --git a/lucene/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java b/lucene/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.javaindex daa609d4bc8..fc836a1ba76 100644--- a/lucene/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java+++ b/lucene/highlighter/src/test/org/apache/lucene/search/highlight/HighlighterTest.java@@ -21,12 +21,12 @@ import java.io.ByteArrayInputStream;import java.io.IOException;import java.nio.charset.StandardCharsets;import java.util.ArrayList;+import java.util.Arrays;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.StringTokenizer;-import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;@@ -42,14 +42,19 @@ import org.apache.lucene.document.TextField;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;-import org.apache.lucene.index.StoredDocument;import org.apache.lucene.index.IndexWriterConfig.OpenMode;import org.apache.lucene.index.IndexWriterConfig;+import org.apache.lucene.index.StoredDocument;import org.apache.lucene.index.Term;import org.apache.lucene.queries.CommonTermsQuery;import org.apache.lucene.search.*;import org.apache.lucene.search.BooleanClause.Occur;import org.apache.lucene.search.highlight.SynonymTokenizer.TestHighlightRunner;+import org.apache.lucene.search.join.BitDocIdSetCachingWrapperFilter;+import org.apache.lucene.search.join.BitDocIdSetFilter;+import org.apache.lucene.search.join.ScoreMode;+import org.apache.lucene.search.join.ToChildBlockJoinQuery;+import org.apache.lucene.search.join.ToParentBlockJoinQuery;import org.apache.lucene.search.spans.*;import org.apache.lucene.store.Directory;import org.apache.lucene.util.BytesRef;@@ -515,6 +520,62 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formatte}++ public void testToParentBlockJoinQuery() throws Exception {+ BitDocIdSetFilter parentFilter = new BitDocIdSetCachingWrapperFilter(+ new QueryWrapperFilter(+ new TermQuery(new Term(FIELD_NAME, "parent"))));++ query = new ToParentBlockJoinQuery(new TermQuery(new Term(FIELD_NAME, "child")),+ parentFilter, ScoreMode.None);+ searcher = newSearcher(reader);+ hits = searcher.search(query, 100);+ int maxNumFragmentsRequired = 2;++ QueryScorer scorer = new QueryScorer(query, FIELD_NAME);+ Highlighter highlighter = new Highlighter(this, scorer);++ for (int i = 0; i < hits.totalHits; i++) {+ String text = "child document";+ TokenStream tokenStream = analyzer.tokenStream(FIELD_NAME, text);++ highlighter.setTextFragmenter(new SimpleFragmenter(40));+ highlighter.getBestFragments(tokenStream, text, maxNumFragmentsRequired, "...");+ }++ assertTrue("Failed to find correct number of highlights " + numHighlights + " found",+ numHighlights == 1);+ }++ public void testToChildBlockJoinQuery() throws Exception {+ BitDocIdSetFilter parentFilter = new BitDocIdSetCachingWrapperFilter(+ new QueryWrapperFilter(+ new TermQuery(new Term(FIELD_NAME, "parent"))));++ BooleanQuery booleanQuery = new BooleanQuery();+ booleanQuery.add(new ToChildBlockJoinQuery(new TermQuery(+ new Term(FIELD_NAME, "parent")), parentFilter, false), Occur.MUST);+ booleanQuery.add(new TermQuery(new Term(FIELD_NAME, "child")), Occur.MUST);+ query = booleanQuery;++ searcher = newSearcher(reader);+ hits = searcher.search(query, 100);+ int maxNumFragmentsRequired = 2;++ QueryScorer scorer = new QueryScorer(query, FIELD_NAME);+ Highlighter highlighter = new Highlighter(this, scorer);++ for (int i = 0; i < hits.totalHits; i++) {+ String text = "parent document";+ TokenStream tokenStream = analyzer.tokenStream(FIELD_NAME, text);++ highlighter.setTextFragmenter(new SimpleFragmenter(40));+ highlighter.getBestFragments(tokenStream, text, maxNumFragmentsRequired, "...");+ }++ assertTrue("Failed to find correct number of highlights " + numHighlights + " found",+ numHighlights == 1);+ }public void testSimpleQueryScorerPhraseHighlighting2() throws Exception {PhraseQuery phraseQuery = new PhraseQuery();@@ -1901,6 +1962,10 @@ public class HighlighterTest extends BaseTokenStreamTestCase implements Formattedoc.add(new StoredField(NUMERIC_FIELD_NAME, 7));writer.addDocument(doc, analyzer);+ Document childDoc = doc(FIELD_NAME, "child document");+ Document parentDoc = doc(FIELD_NAME, "parent document");+ writer.addDocuments(Arrays.asList(childDoc, parentDoc));+writer.forceMerge(1);writer.close();reader = DirectoryReader.open(ramDir);diff --git a/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.javaindex afe13b9ba64..65f4bcfcd39 100644--- a/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java+++ b/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java@@ -90,6 +90,11 @@ public class ToChildBlockJoinQuery extends Query {return new ToChildBlockJoinWeight(this, parentQuery.createWeight(searcher), parentsFilter, doScores);}+ /** Return our parent query. */+ public Query getParentQuery() {+ return parentQuery;+ }+private static class ToChildBlockJoinWeight extends Weight {private final Query joinQuery;private final Weight parentWeight;diff --git a/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.javaindex 01b62ca245a..cba85b13eaa 100644--- a/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java+++ b/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java@@ -122,6 +122,11 @@ public class ToParentBlockJoinQuery extends Query {public Weight createWeight(IndexSearcher searcher) throws IOException {return new BlockJoinWeight(this, childQuery.createWeight(searcher), parentsFilter, scoreMode);}++ /** Return our child query. */+ public Query getChildQuery() {+ return childQuery;+ }private static class BlockJoinWeight extends Weight {private final Query joinQuery;