From 2adc2a19719eeb5781a49e00fbc23f86128103bf Mon Sep 17 00:00:00 2001 From: chengpohi Date: Tue, 16 Aug 2016 09:00:16 +0800 Subject: [PATCH] Enable BoostingQuery with FVH highlighter (#19984) * Enable BoostingQuery with FVH highlighter * apply boost with negativeBoost * flatten boosting query with its own boost and update boost query to a single layer --- .../search/vectorhighlight/CustomFieldQuery.java | 15 ++++++++++++++- .../subphase/highlight/HighlighterSearchIT.java | 2 -- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/apache/lucene/search/vectorhighlight/CustomFieldQuery.java b/core/src/main/java/org/apache/lucene/search/vectorhighlight/CustomFieldQuery.java index e455340e3a0..88b045d2a5e 100644 --- a/core/src/main/java/org/apache/lucene/search/vectorhighlight/CustomFieldQuery.java +++ b/core/src/main/java/org/apache/lucene/search/vectorhighlight/CustomFieldQuery.java @@ -22,6 +22,8 @@ package org.apache.lucene.search.vectorhighlight; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.queries.BlendedTermQuery; +import org.apache.lucene.queries.BoostingQuery; +import org.apache.lucene.search.BoostQuery; import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.MultiPhraseQuery; import org.apache.lucene.search.PhraseQuery; @@ -56,7 +58,12 @@ public class CustomFieldQuery extends FieldQuery { @Override void flatten(Query sourceQuery, IndexReader reader, Collection flatQueries, float boost) throws IOException { - if (sourceQuery instanceof SpanTermQuery) { + if (sourceQuery instanceof BoostQuery) { + BoostQuery bq = (BoostQuery) sourceQuery; + sourceQuery = bq.getQuery(); + boost *= bq.getBoost(); + flatten(sourceQuery, reader, flatQueries, boost); + } else if (sourceQuery instanceof SpanTermQuery) { super.flatten(new TermQuery(((SpanTermQuery) sourceQuery).getTerm()), reader, flatQueries, boost); } else if (sourceQuery instanceof ConstantScoreQuery) { flatten(((ConstantScoreQuery) sourceQuery).getQuery(), reader, flatQueries, boost); @@ -75,6 +82,12 @@ public class CustomFieldQuery extends FieldQuery { } else if (sourceQuery instanceof ToParentBlockJoinQuery) { ToParentBlockJoinQuery blockJoinQuery = (ToParentBlockJoinQuery) sourceQuery; flatten(blockJoinQuery.getChildQuery(), reader, flatQueries, boost); + } else if (sourceQuery instanceof BoostingQuery) { + BoostingQuery boostingQuery = (BoostingQuery) sourceQuery; + //flatten positive query with query boost + flatten(boostingQuery.getMatch(), reader, flatQueries, boost); + //flatten negative query with negative boost + flatten(boostingQuery.getContext(), reader, flatQueries, boostingQuery.getBoost()); } else { super.flatten(sourceQuery, reader, flatQueries, boost); } diff --git a/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java b/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java index c7a5fc241ab..33e8cb3784f 100644 --- a/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java +++ b/core/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java @@ -1400,7 +1400,6 @@ public class HighlighterSearchIT extends ESIntegTestCase { assertHighlight(searchResponse, 0, "field2", 0, 1, equalTo("The quick brown fox jumps over the lazy dog")); } - @AwaitsFix(bugUrl="Broken now that BoostingQuery does not extend BooleanQuery anymore") public void testBoostingQueryTermVector() throws IOException { assertAcked(prepareCreate("test").addMapping("type1", type1TermVectorMapping())); ensureGreen(); @@ -2643,7 +2642,6 @@ public class HighlighterSearchIT extends ESIntegTestCase { } } - @AwaitsFix(bugUrl="Broken now that BoostingQuery does not extend BooleanQuery anymore") public void testFastVectorHighlighterPhraseBoost() throws Exception { assertAcked(prepareCreate("test").addMapping("type1", type1TermVectorMapping())); phraseBoostTestCase("fvh");