LUCENE-7484: FastVectorHighlighter failed to highlight SynonymQuery

This commit is contained in:
Mike McCandless 2016-10-10 09:12:31 -04:00
parent 9fea5129d3
commit 58b64c3675
3 changed files with 48 additions and 0 deletions

View File

@ -64,6 +64,9 @@ Bug Fixes
* LUCENE-7468: ASCIIFoldingFilter should not emit duplicated tokens when
preserve original is on. (David Causse via Adrien Grand)
* LUCENE-7484: FastVectorHighlighter failed to highlight SynonymQuery
(Jim Ferenczi via Mike McCandless)
Improvements
* LUCENE-7439: FuzzyQuery now matches all terms within the specified

View File

@ -36,6 +36,7 @@ import org.apache.lucene.search.DisjunctionMaxQuery;
import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SynonymQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.join.ToParentBlockJoinQuery;
import org.apache.lucene.search.vectorhighlight.FieldTermStack.TermInfo;
@ -120,6 +121,12 @@ public class FieldQuery {
if( !flatQueries.contains( sourceQuery ) )
flatQueries.add( sourceQuery );
}
else if ( sourceQuery instanceof SynonymQuery ){
SynonymQuery synQuery = (SynonymQuery) sourceQuery;
for( Term term : synQuery.getTerms()) {
flatten( new TermQuery(term), reader, flatQueries, boost);
}
}
else if( sourceQuery instanceof PhraseQuery ){
PhraseQuery pq = (PhraseQuery)sourceQuery;
if( pq.getTerms().length == 1 )

View File

@ -46,6 +46,7 @@ import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.SynonymQuery;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.DefaultEncoder;
@ -521,6 +522,43 @@ public class FastVectorHighlighterTest extends LuceneTestCase {
writer.close();
dir.close();
}
public void testWithSynonym() throws IOException {
Directory dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
FieldType type = new FieldType(TextField.TYPE_STORED);
type.setStoreTermVectorOffsets(true);
type.setStoreTermVectorPositions(true);
type.setStoreTermVectors(true);
type.freeze();
Document doc = new Document();
doc.add( new Field("field", "the quick brown fox", type ));
writer.addDocument(doc);
FastVectorHighlighter highlighter = new FastVectorHighlighter();
IndexReader reader = DirectoryReader.open(writer);
int docId = 0;
// query1: simple synonym query
SynonymQuery synQuery = new SynonymQuery(new Term("field", "quick"), new Term("field", "fast"));
FieldQuery fieldQuery = highlighter.getFieldQuery(synQuery, reader);
String[] bestFragments = highlighter.getBestFragments(fieldQuery, reader, docId, "field", 54, 1);
assertEquals("the <b>quick</b> brown fox", bestFragments[0]);
// query2: boolean query with synonym query
BooleanQuery.Builder bq =
new BooleanQuery.Builder()
.add(new BooleanClause(synQuery, Occur.MUST))
.add(new BooleanClause(new TermQuery(new Term("field", "fox")), Occur.MUST));
fieldQuery = highlighter.getFieldQuery(bq.build(), reader);
bestFragments = highlighter.getBestFragments(fieldQuery, reader, docId, "field", 54, 1);
assertEquals("the <b>quick</b> brown <b>fox</b>", bestFragments[0]);
reader.close();
writer.close();
dir.close();
}
public void testBooleanPhraseWithSynonym() throws IOException {
Directory dir = newDirectory();