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
This commit is contained in:
Yonik Seeley 2010-09-08 01:40:26 +00:00
parent 50663b8570
commit 63f331d880
5 changed files with 85 additions and 43 deletions

View File

@ -458,6 +458,8 @@ Bug Fixes
to be removed before it was finished being copied. This did not affect to be removed before it was finished being copied. This did not affect
normal master/slave replication. (Peter Sturge via yonik) normal master/slave replication. (Peter Sturge via yonik)
* SOLR-2107: MoreLikeThisHandler doesn't work with alternate qparsers. (yonik)
Other Changes Other Changes
---------------------- ----------------------

View File

@ -34,10 +34,8 @@ import java.util.regex.Pattern;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause; import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.*;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.similar.MoreLikeThis; import org.apache.lucene.search.similar.MoreLikeThis;
import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CommonParams; 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.response.SolrQueryResponse;
import org.apache.solr.schema.IndexSchema; import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.SchemaField; import org.apache.solr.schema.SchemaField;
import org.apache.solr.search.DocIterator; import org.apache.solr.search.*;
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.util.SolrPluginUtils; import org.apache.solr.util.SolrPluginUtils;
@ -83,11 +77,44 @@ public class MoreLikeThisHandler extends RequestHandlerBase
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception
{ {
SolrParams params = req.getParams(); 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<Query> 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<Query>();
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(); SolrIndexSearcher searcher = req.getSearcher();
MoreLikeThisHelper mlt = new MoreLikeThisHelper( params, searcher ); MoreLikeThisHelper mlt = new MoreLikeThisHelper( params, searcher );
List<Query> filters = SolrPluginUtils.parseFilterQueries(req);
// Hold on to the interesting terms if relevant // Hold on to the interesting terms if relevant
TermStyle termStyle = TermStyle.get( params.get( MoreLikeThisParams.INTERESTING_TERMS ) ); TermStyle termStyle = TermStyle.get( params.get( MoreLikeThisParams.INTERESTING_TERMS ) );
@ -95,7 +122,6 @@ public class MoreLikeThisHandler extends RequestHandlerBase
? null : new ArrayList<InterestingTerm>( mlt.mlt.getMaxQueryTerms() ); ? null : new ArrayList<InterestingTerm>( mlt.mlt.getMaxQueryTerms() );
DocListAndSet mltDocs = null; DocListAndSet mltDocs = null;
String q = params.get( CommonParams.Q );
// Parse Required Params // Parse Required Params
// This will either have a single Reader or valid query // This will either have a single Reader or valid query
@ -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 start = params.getInt(CommonParams.START, 0);
int rows = params.getInt(CommonParams.ROWS, 10); int rows = params.getInt(CommonParams.ROWS, 10);
@ -136,8 +155,6 @@ public class MoreLikeThisHandler extends RequestHandlerBase
true); true);
int matchOffset = params.getInt(MoreLikeThisParams.MATCH_OFFSET, 0); int matchOffset = params.getInt(MoreLikeThisParams.MATCH_OFFSET, 0);
// Find the base match // 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, DocList match = searcher.getDocList(query, null, null, matchOffset, 1,
flags); // only get the first one... flags); // only get the first one...
if (includeMatch) { if (includeMatch) {

View File

@ -111,6 +111,9 @@ public class MinimalSchemaTest extends SolrTestCaseJ4 {
if (handler.startsWith("/update")) { if (handler.startsWith("/update")) {
continue; continue;
} }
if (handler.startsWith("/mlt")) {
continue;
}
assertQ("failure w/handler: '" + handler + "'", assertQ("failure w/handler: '" + handler + "'",
req("qt", handler, req("qt", handler,

View File

@ -21,10 +21,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.apache.solr.common.params.CommonParams; import org.apache.solr.common.params.*;
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.util.ContentStream; import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ContentStreamBase; import org.apache.solr.common.util.ContentStreamBase;
import org.apache.solr.core.SolrCore; import org.apache.solr.core.SolrCore;
@ -53,9 +50,8 @@ public class MoreLikeThisHandlerTest extends AbstractSolrTestCase {
SolrCore core = h.getCore(); SolrCore core = h.getCore();
MoreLikeThisHandler mlt = new MoreLikeThisHandler(); MoreLikeThisHandler mlt = new MoreLikeThisHandler();
Map<String,String[]> params = new HashMap<String,String[]>(); ModifiableSolrParams params = new ModifiableSolrParams();
MultiMapSolrParams mmparams = new MultiMapSolrParams( params ); SolrQueryRequestBase req = new SolrQueryRequestBase( core, params) {};
SolrQueryRequestBase req = new SolrQueryRequestBase( core, (SolrParams)mmparams ) {};
// requires 'q' or single content stream // requires 'q' or single content stream
try { 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(adoc("id","46","name","Nicole Kidman","subword","Batman","subword","Days of Thunder","subword","Eyes Wide Shut","subword","Far and Away"));
assertU(commit()); assertU(commit());
params.put(CommonParams.Q, new String[]{"id:42"}); params.set(CommonParams.Q, "id:42");
params.put(MoreLikeThisParams.MLT, new String[]{"true"}); params.set(MoreLikeThisParams.MLT, "true");
params.put(MoreLikeThisParams.SIMILARITY_FIELDS, new String[]{"name,subword,foo_ti"}); params.set(MoreLikeThisParams.SIMILARITY_FIELDS, "name,subword,foo_ti");
params.put(MoreLikeThisParams.INTERESTING_TERMS,new String[]{"details"}); params.set(MoreLikeThisParams.INTERESTING_TERMS, "details");
params.put(MoreLikeThisParams.MIN_TERM_FREQ,new String[]{"1"}); params.set(MoreLikeThisParams.MIN_TERM_FREQ,"1");
params.put(MoreLikeThisParams.MIN_DOC_FREQ,new String[]{"1"}); params.set(MoreLikeThisParams.MIN_DOC_FREQ,"1");
params.put("indent",new String[]{"true"}); params.set("indent","true");
SolrQueryRequest mltreq = new LocalSolrQueryRequest( core, (SolrParams)mmparams); SolrQueryRequest mltreq = new LocalSolrQueryRequest( core, params);
assertQ("morelikethis - tom cruise",mltreq assertQ("morelikethis - tom cruise",mltreq
,"//result/doc[1]/int[@name='id'][.='46']" ,"//result/doc[1]/int[@name='id'][.='46']"
,"//result/doc[2]/int[@name='id'][.='43']"); ,"//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 assertQ("morelike this - harrison ford",mltreq
,"//result/doc[1]/int[@name='id'][.='45']"); ,"//result/doc[1]/int[@name='id'][.='45']");
params.put(CommonParams.Q, new String[]{"id:42"}); // test that qparser plugins work
params.put(MoreLikeThisParams.QF,new String[]{"name^5.0 subword^0.1"}); 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 assertQ("morelikethis with weights",mltreq
,"//result/doc[1]/int[@name='id'][.='43']" ,"//result/doc[1]/int[@name='id'][.='43']"
,"//result/doc[2]/int[@name='id'][.='46']"); ,"//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"}); // params.put(MoreLikeThisParams.QF,new String[]{"foo_ti"});
// String response = h.query(mltreq); // String response = h.query(mltreq);
// System.out.println(response); // System.out.println(response);

View File

@ -423,6 +423,10 @@
<str>tvComponent</str> <str>tvComponent</str>
</arr> </arr>
</requestHandler> </requestHandler>
<requestHandler name="/mlt" class="solr.MoreLikeThisHandler">
</requestHandler>
<searchComponent class="solr.HighlightComponent" name="highlight"> <searchComponent class="solr.HighlightComponent" name="highlight">
<highlighting> <highlighting>
<!-- Configure the standard fragmenter --> <!-- Configure the standard fragmenter -->