From 63f331d880653926c08d1506f871f961891c368f Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Wed, 8 Sep 2010 01:40:26 +0000 Subject: [PATCH] SOLR-2107: MoreLikeThisHandler doesn't work with alternate qparsers git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@993576 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 2 + .../solr/handler/MoreLikeThisHandler.java | 65 ++++++++++++------- .../org/apache/solr/MinimalSchemaTest.java | 3 + .../solr/handler/MoreLikeThisHandlerTest.java | 54 +++++++++------ .../test/test-files/solr/conf/solrconfig.xml | 4 ++ 5 files changed, 85 insertions(+), 43 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index a327745cba7..f62398e898b 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -458,6 +458,8 @@ Bug Fixes to be removed before it was finished being copied. This did not affect normal master/slave replication. (Peter Sturge via yonik) +* SOLR-2107: MoreLikeThisHandler doesn't work with alternate qparsers. (yonik) + Other Changes ---------------------- diff --git a/solr/src/java/org/apache/solr/handler/MoreLikeThisHandler.java b/solr/src/java/org/apache/solr/handler/MoreLikeThisHandler.java index d5acf3c275c..091011b0a8e 100644 --- a/solr/src/java/org/apache/solr/handler/MoreLikeThisHandler.java +++ b/solr/src/java/org/apache/solr/handler/MoreLikeThisHandler.java @@ -34,10 +34,8 @@ import java.util.regex.Pattern; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; -import org.apache.lucene.search.BooleanClause; -import org.apache.lucene.search.BooleanQuery; -import org.apache.lucene.search.Query; -import org.apache.lucene.search.TermQuery; +import org.apache.lucene.queryParser.ParseException; +import org.apache.lucene.search.*; import org.apache.lucene.search.similar.MoreLikeThis; import org.apache.solr.common.SolrException; import org.apache.solr.common.params.CommonParams; @@ -54,11 +52,7 @@ import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.response.SolrQueryResponse; import org.apache.solr.schema.IndexSchema; import org.apache.solr.schema.SchemaField; -import org.apache.solr.search.DocIterator; -import org.apache.solr.search.DocList; -import org.apache.solr.search.DocListAndSet; -import org.apache.solr.search.QueryParsing; -import org.apache.solr.search.SolrIndexSearcher; +import org.apache.solr.search.*; import org.apache.solr.util.SolrPluginUtils; @@ -83,20 +77,52 @@ public class MoreLikeThisHandler extends RequestHandlerBase public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception { SolrParams params = req.getParams(); + + // Set field flags + String fl = params.get(CommonParams.FL); + int flags = 0; + if (fl != null) { + flags |= SolrPluginUtils.setReturnFields(fl, rsp); + } + + String defType = params.get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE); + String q = params.get( CommonParams.Q ); + Query query = null; + SortSpec sortSpec = null; + List filters = null; + + try { + if (q != null) { + QParser parser = QParser.getParser(q, defType, req); + query = parser.getQuery(); + sortSpec = parser.getSort(true); + } + + String[] fqs = req.getParams().getParams(CommonParams.FQ); + if (fqs!=null && fqs.length!=0) { + filters = new ArrayList(); + for (String fq : fqs) { + if (fq != null && fq.trim().length()!=0) { + QParser fqp = QParser.getParser(fq, null, req); + filters.add(fqp.getQuery()); + } + } + } + } catch (ParseException e) { + throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e); + } + SolrIndexSearcher searcher = req.getSearcher(); - - + MoreLikeThisHelper mlt = new MoreLikeThisHelper( params, searcher ); - List filters = SolrPluginUtils.parseFilterQueries(req); - + // 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() ); DocListAndSet mltDocs = null; - String q = params.get( CommonParams.Q ); - + // Parse Required Params // This will either have a single Reader or valid query Reader reader = null; @@ -115,13 +141,6 @@ public class MoreLikeThisHandler extends RequestHandlerBase } } - // What fields do we need to return - String fl = params.get(CommonParams.FL); - int flags = 0; - if (fl != null) { - flags |= SolrPluginUtils.setReturnFields(fl, rsp); - } - int start = params.getInt(CommonParams.START, 0); int rows = params.getInt(CommonParams.ROWS, 10); @@ -136,8 +155,6 @@ public class MoreLikeThisHandler extends RequestHandlerBase true); int matchOffset = params.getInt(MoreLikeThisParams.MATCH_OFFSET, 0); // Find the base match - Query query = QueryParsing.parseQuery(q, params.get(CommonParams.DF), - params, req.getSchema()); DocList match = searcher.getDocList(query, null, null, matchOffset, 1, flags); // only get the first one... if (includeMatch) { diff --git a/solr/src/test/org/apache/solr/MinimalSchemaTest.java b/solr/src/test/org/apache/solr/MinimalSchemaTest.java index b20c9417413..04c0c27e3be 100644 --- a/solr/src/test/org/apache/solr/MinimalSchemaTest.java +++ b/solr/src/test/org/apache/solr/MinimalSchemaTest.java @@ -111,6 +111,9 @@ public class MinimalSchemaTest extends SolrTestCaseJ4 { if (handler.startsWith("/update")) { continue; } + if (handler.startsWith("/mlt")) { + continue; + } assertQ("failure w/handler: '" + handler + "'", req("qt", handler, diff --git a/solr/src/test/org/apache/solr/handler/MoreLikeThisHandlerTest.java b/solr/src/test/org/apache/solr/handler/MoreLikeThisHandlerTest.java index 1942bf7e36d..124e2482331 100644 --- a/solr/src/test/org/apache/solr/handler/MoreLikeThisHandlerTest.java +++ b/solr/src/test/org/apache/solr/handler/MoreLikeThisHandlerTest.java @@ -21,10 +21,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.Map; -import org.apache.solr.common.params.CommonParams; -import org.apache.solr.common.params.MoreLikeThisParams; -import org.apache.solr.common.params.MultiMapSolrParams; -import org.apache.solr.common.params.SolrParams; +import org.apache.solr.common.params.*; import org.apache.solr.common.util.ContentStream; import org.apache.solr.common.util.ContentStreamBase; import org.apache.solr.core.SolrCore; @@ -53,9 +50,8 @@ public class MoreLikeThisHandlerTest extends AbstractSolrTestCase { SolrCore core = h.getCore(); MoreLikeThisHandler mlt = new MoreLikeThisHandler(); - Map params = new HashMap(); - MultiMapSolrParams mmparams = new MultiMapSolrParams( params ); - SolrQueryRequestBase req = new SolrQueryRequestBase( core, (SolrParams)mmparams ) {}; + ModifiableSolrParams params = new ModifiableSolrParams(); + SolrQueryRequestBase req = new SolrQueryRequestBase( core, params) {}; // requires 'q' or single content stream try { @@ -80,29 +76,49 @@ public class MoreLikeThisHandlerTest extends AbstractSolrTestCase { assertU(adoc("id","46","name","Nicole Kidman","subword","Batman","subword","Days of Thunder","subword","Eyes Wide Shut","subword","Far and Away")); assertU(commit()); - params.put(CommonParams.Q, new String[]{"id:42"}); - params.put(MoreLikeThisParams.MLT, new String[]{"true"}); - params.put(MoreLikeThisParams.SIMILARITY_FIELDS, new String[]{"name,subword,foo_ti"}); - params.put(MoreLikeThisParams.INTERESTING_TERMS,new String[]{"details"}); - params.put(MoreLikeThisParams.MIN_TERM_FREQ,new String[]{"1"}); - params.put(MoreLikeThisParams.MIN_DOC_FREQ,new String[]{"1"}); - params.put("indent",new String[]{"true"}); + params.set(CommonParams.Q, "id:42"); + params.set(MoreLikeThisParams.MLT, "true"); + params.set(MoreLikeThisParams.SIMILARITY_FIELDS, "name,subword,foo_ti"); + params.set(MoreLikeThisParams.INTERESTING_TERMS, "details"); + params.set(MoreLikeThisParams.MIN_TERM_FREQ,"1"); + params.set(MoreLikeThisParams.MIN_DOC_FREQ,"1"); + params.set("indent","true"); - SolrQueryRequest mltreq = new LocalSolrQueryRequest( core, (SolrParams)mmparams); + SolrQueryRequest 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.put(CommonParams.Q, new String[]{"id:44"}); + params.set(CommonParams.Q, "id:44"); assertQ("morelike this - harrison ford",mltreq ,"//result/doc[1]/int[@name='id'][.='45']"); - - params.put(CommonParams.Q, new String[]{"id:42"}); - params.put(MoreLikeThisParams.QF,new String[]{"name^5.0 subword^0.1"}); + + // test that qparser plugins work + params.set(CommonParams.Q, "{!field f=id}44"); + assertQ(mltreq + ,"//result/doc[1]/int[@name='id'][.='45']"); + + params.set(CommonParams.Q, "id:42"); + params.set(MoreLikeThisParams.QF,"name^5.0 subword^0.1"); assertQ("morelikethis with weights",mltreq ,"//result/doc[1]/int[@name='id'][.='43']" ,"//result/doc[2]/int[@name='id'][.='46']"); + + // test that qparser plugins work w/ the MoreLikeThisHandler + params.set(CommonParams.QT, "/mlt"); + params.set(CommonParams.Q, "{!field f=id}44"); + assertQ(mltreq + ,"//result/doc[1]/int[@name='id'][.='45']"); + + // test that debugging works + params.set(CommonParams.QT, "/mlt"); + params.set("debugQuery", "true"); + assertQ(mltreq + ,"//result/doc[1]/int[@name='id'][.='45']" + ,"//lst[@name='debug']/lst[@name='explain']" + ); + // params.put(MoreLikeThisParams.QF,new String[]{"foo_ti"}); // String response = h.query(mltreq); // System.out.println(response); diff --git a/solr/src/test/test-files/solr/conf/solrconfig.xml b/solr/src/test/test-files/solr/conf/solrconfig.xml index 6115c5a9357..1d3622ba54f 100644 --- a/solr/src/test/test-files/solr/conf/solrconfig.xml +++ b/solr/src/test/test-files/solr/conf/solrconfig.xml @@ -423,6 +423,10 @@ tvComponent + + + +