diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index afc0c658521..4606e8c5fea 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -322,6 +322,8 @@ Bug Fixes * SOLR-7462: AIOOBE in RecordingJSONParser (Scott Dawson, noble) +* SOLR-8418: Adapt to changes in LUCENE-6590 for use of boosts with MLTHandler and + Simple/CloudMLTQParser (Jens Wille, Ramkumar Aiyengar) Other Changes ---------------------- diff --git a/solr/core/src/java/org/apache/solr/handler/MoreLikeThisHandler.java b/solr/core/src/java/org/apache/solr/handler/MoreLikeThisHandler.java index 59da510d17a..148c76a05ef 100644 --- a/solr/core/src/java/org/apache/solr/handler/MoreLikeThisHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/MoreLikeThisHandler.java @@ -37,11 +37,7 @@ import org.apache.solr.common.params.SolrParams; import org.apache.solr.common.util.ContentStream; import org.apache.solr.common.util.NamedList; import org.apache.solr.common.util.SimpleOrderedMap; -import org.apache.solr.core.SolrCore; import org.apache.solr.handler.component.FacetComponent; -import org.apache.solr.handler.component.SpatialHeatmapFacets; -import org.apache.solr.handler.component.DateFacetProcessor; -import org.apache.solr.handler.component.RangeFacetProcessor; import org.apache.solr.request.SimpleFacets; import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.response.SolrQueryResponse; @@ -148,7 +144,7 @@ public class MoreLikeThisHandler extends RequestHandlerBase // Hold on to the interesting terms if relevant TermStyle termStyle = TermStyle.get(params.get(MoreLikeThisParams.INTERESTING_TERMS)); List interesting = (termStyle == TermStyle.NONE) - ? null : new ArrayList(mlt.mlt.getMaxQueryTerms()); + ? null : new ArrayList<>(mlt.mlt.getMaxQueryTerms()); DocListAndSet mltDocs = null; @@ -246,10 +242,10 @@ public class MoreLikeThisHandler extends RequestHandlerBase if (dbg == false) {//if it's true, we are doing everything anyway. String[] dbgParams = req.getParams().getParams(CommonParams.DEBUG); if (dbgParams != null) { - for (int i = 0; i < dbgParams.length; i++) { - if (dbgParams[i].equals(CommonParams.QUERY)) { + for (String dbgParam : dbgParams) { + if (dbgParam.equals(CommonParams.QUERY)) { dbgQuery = true; - } else if (dbgParams[i].equals(CommonParams.RESULTS)) { + } else if (dbgParam.equals(CommonParams.RESULTS)) { dbgResults = true; } } @@ -381,10 +377,14 @@ public class MoreLikeThisHandler extends RequestHandlerBase newQ.setMinimumNumberShouldMatch(boostedQuery.getMinimumNumberShouldMatch()); for (BooleanClause clause : boostedQuery) { Query q = clause.getQuery(); - Float b = this.boostFields.get(((TermQuery) q).getTerm().field()); - if (b != null) { - q = new BoostQuery(q, b); + float originalBoost = 1f; + if (q instanceof BoostQuery) { + BoostQuery bq = (BoostQuery) q; + q = bq.getQuery(); + originalBoost = bq.getBoost(); } + Float fieldBoost = boostFields.get(((TermQuery) q).getTerm().field()); + q = ((fieldBoost != null) ? new BoostQuery(q, fieldBoost * originalBoost) : clause.getQuery()); newQ.add(q, clause.getOccur()); } boostedQuery = newQ.build(); @@ -485,7 +485,7 @@ public class MoreLikeThisHandler extends RequestHandlerBase for( BooleanClause o : clauses ) { Query q = o.getQuery(); float boost = 1f; - if (query instanceof BoostQuery) { + if (q instanceof BoostQuery) { BoostQuery bq = (BoostQuery) q; q = bq.getQuery(); boost = bq.getBoost(); diff --git a/solr/core/src/java/org/apache/solr/search/mlt/CloudMLTQParser.java b/solr/core/src/java/org/apache/solr/search/mlt/CloudMLTQParser.java index 28e26341192..db75517bf89 100644 --- a/solr/core/src/java/org/apache/solr/search/mlt/CloudMLTQParser.java +++ b/solr/core/src/java/org/apache/solr/search/mlt/CloudMLTQParser.java @@ -98,8 +98,8 @@ public class CloudMLTQParser extends QParser { mlt.setAnalyzer(req.getSchema().getIndexAnalyzer()); - Map> filteredDocument = new HashMap(); - ArrayList fieldNames = new ArrayList(); + Map> filteredDocument = new HashMap<>(); + ArrayList fieldNames = new ArrayList<>(); if (qf != null) { for (String fieldName : qf) { @@ -146,12 +146,14 @@ public class CloudMLTQParser extends QParser { for (BooleanClause clause : boostedMLTQuery) { Query q = clause.getQuery(); - Float b = boostFields.get(((TermQuery) q).getTerm().field()); - - if (b != null) { - q = new BoostQuery(q, b); + float originalBoost = 1f; + if (q instanceof BoostQuery) { + BoostQuery bq = (BoostQuery) q; + q = bq.getQuery(); + originalBoost = bq.getBoost(); } - + Float fieldBoost = boostFields.get(((TermQuery) q).getTerm().field()); + q = ((fieldBoost != null) ? new BoostQuery(q, fieldBoost * originalBoost) : clause.getQuery()); newQ.add(q, clause.getOccur()); } diff --git a/solr/core/src/java/org/apache/solr/search/mlt/SimpleMLTQParser.java b/solr/core/src/java/org/apache/solr/search/mlt/SimpleMLTQParser.java index ae3676a3973..19e78a2db64 100644 --- a/solr/core/src/java/org/apache/solr/search/mlt/SimpleMLTQParser.java +++ b/solr/core/src/java/org/apache/solr/search/mlt/SimpleMLTQParser.java @@ -97,7 +97,7 @@ public class SimpleMLTQParser extends QParser { boostFields = SolrPluginUtils.parseFieldBoosts(qf); } - ArrayList fields = new ArrayList(); + ArrayList fields = new ArrayList<>(); if (qf != null) { for (String fieldName : qf) { @@ -136,12 +136,14 @@ public class SimpleMLTQParser extends QParser { for (BooleanClause clause : boostedMLTQuery) { Query q = clause.getQuery(); - Float b = boostFields.get(((TermQuery) q).getTerm().field()); - - if (b != null) { - q = new BoostQuery(q, b); + float originalBoost = 1f; + if (q instanceof BoostQuery) { + BoostQuery bq = (BoostQuery) q; + q = bq.getQuery(); + originalBoost = bq.getBoost(); } - + Float fieldBoost = boostFields.get(((TermQuery) q).getTerm().field()); + q = ((fieldBoost != null) ? new BoostQuery(q, fieldBoost * originalBoost) : clause.getQuery()); newQ.add(q, clause.getOccur()); } diff --git a/solr/core/src/test/org/apache/solr/handler/MoreLikeThisHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/MoreLikeThisHandlerTest.java index 2790cb6b51e..794d82c2cc2 100644 --- a/solr/core/src/test/org/apache/solr/handler/MoreLikeThisHandlerTest.java +++ b/solr/core/src/test/org/apache/solr/handler/MoreLikeThisHandlerTest.java @@ -89,6 +89,12 @@ public class MoreLikeThisHandlerTest extends SolrTestCaseJ4 { assertQ("morelikethis - tom cruise",mltreq ,"//result/doc[1]/int[@name='id'][.='46']" ,"//result/doc[2]/int[@name='id'][.='43']"); + + params.set(MoreLikeThisParams.BOOST, "true"); + mltreq.close(); mltreq = new LocalSolrQueryRequest( core, params); + assertQ("morelikethis - tom cruise",mltreq + ,"//result/doc[1]/int[@name='id'][.='46']" + ,"//result/doc[2]/int[@name='id'][.='43']"); params.set(CommonParams.Q, "id:44"); mltreq.close(); mltreq = new LocalSolrQueryRequest(h.getCore(), params); diff --git a/solr/core/src/test/org/apache/solr/search/mlt/CloudMLTQParserTest.java b/solr/core/src/test/org/apache/solr/search/mlt/CloudMLTQParserTest.java index bbdfcc45c37..c19aaf95810 100644 --- a/solr/core/src/test/org/apache/solr/search/mlt/CloudMLTQParserTest.java +++ b/solr/core/src/test/org/apache/solr/search/mlt/CloudMLTQParserTest.java @@ -111,6 +111,18 @@ public class CloudMLTQParserTest extends AbstractFullDistribZkTestBase { actualIds[i++] = Integer.valueOf(String.valueOf(solrDocument.getFieldValue("id"))); } assertArrayEquals(expectedIds, actualIds); + + params = new ModifiableSolrParams(); + params.set(CommonParams.Q, "{!mlt qf=lowerfilt boost=true}17"); + queryResponse = queryServer(params); + solrDocuments = queryResponse.getResults(); + expectedIds = new int[]{7, 13, 14, 15, 16, 20, 22, 24, 32, 9}; + actualIds = new int[solrDocuments.size()]; + i = 0; + for (SolrDocument solrDocument : solrDocuments) { + actualIds[i++] = Integer.valueOf(String.valueOf(solrDocument.getFieldValue("id"))); + } + assertArrayEquals(expectedIds, actualIds); params = new ModifiableSolrParams(); params.set(CommonParams.Q, "{!mlt qf=lowerfilt mindf=0 mintf=1}3"); diff --git a/solr/core/src/test/org/apache/solr/search/mlt/SimpleMLTQParserTest.java b/solr/core/src/test/org/apache/solr/search/mlt/SimpleMLTQParserTest.java index fb33ef5e122..fbd63e4ef27 100644 --- a/solr/core/src/test/org/apache/solr/search/mlt/SimpleMLTQParserTest.java +++ b/solr/core/src/test/org/apache/solr/search/mlt/SimpleMLTQParserTest.java @@ -97,6 +97,21 @@ public class SimpleMLTQParserTest extends SolrTestCaseJ4 { "//result/doc[10]/int[@name='id'][.='23']" ); + params = new ModifiableSolrParams(); + params.set(CommonParams.Q, "{!mlt qf=lowerfilt boost=true}17"); + assertQ(req(params), + "//result/doc[1]/int[@name='id'][.='13']", + "//result/doc[2]/int[@name='id'][.='14']", + "//result/doc[3]/int[@name='id'][.='15']", + "//result/doc[4]/int[@name='id'][.='16']", + "//result/doc[5]/int[@name='id'][.='18']", + "//result/doc[6]/int[@name='id'][.='19']", + "//result/doc[7]/int[@name='id'][.='20']", + "//result/doc[8]/int[@name='id'][.='21']", + "//result/doc[9]/int[@name='id'][.='22']", + "//result/doc[10]/int[@name='id'][.='23']" + ); + params = new ModifiableSolrParams(); params.set(CommonParams.Q, "{!mlt qf=lowerfilt mindf=0 mintf=1}26"); params.set(CommonParams.DEBUG, "true");