From 44302daeb252787c0d783bf7b80b90ab969eed4d Mon Sep 17 00:00:00 2001 From: Grant Ingersoll Date: Mon, 4 May 2009 11:07:15 +0000 Subject: [PATCH] SOLR-1138: gracefully handle empty and null queries git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@771268 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 1 + .../component/QueryElevationComponent.java | 11 ++++++----- .../QueryElevationComponentTest.java | 19 ++++++++++++++++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 0891d8a4e0d..5de9106321f 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -354,6 +354,7 @@ Bug Fixes 41. SOLR-1135: Java replication creates Snapshot in the directory where Solr was launched (Jianhan Guo via shalin) +42. SOLR-1138: Query Elevation Component now gracefully handles missing queries. (gsingers) Other Changes ---------------------- diff --git a/src/java/org/apache/solr/handler/component/QueryElevationComponent.java b/src/java/org/apache/solr/handler/component/QueryElevationComponent.java index e2df6f515ff..1058eb1c9ea 100644 --- a/src/java/org/apache/solr/handler/component/QueryElevationComponent.java +++ b/src/java/org/apache/solr/handler/component/QueryElevationComponent.java @@ -52,6 +52,7 @@ import org.apache.lucene.search.Sort; import org.apache.lucene.search.SortComparatorSource; import org.apache.lucene.search.SortField; import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.solr.common.SolrException; import org.apache.solr.common.params.SolrParams; import org.apache.solr.common.util.DOMUtil; @@ -324,12 +325,12 @@ public class QueryElevationComponent extends SearchComponent implements SolrCore boolean force = params.getBool( FORCE_ELEVATION, forceElevation ); Query query = rb.getQuery(); - if( query == null ) { - throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, - "The QueryElevationComponent needs to be registered 'after' the query component" ); + String qstr = rb.getQueryString(); + if( query == null || qstr == null) { + return; } - - String qstr = getAnalyzedQuery( rb.getQueryString() ); + + qstr = getAnalyzedQuery(qstr); IndexReader reader = req.getSearcher().getReader(); ElevationObj booster = null; try { diff --git a/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.java b/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.java index 1c3a99f56a9..50a02595bbc 100644 --- a/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.java +++ b/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.java @@ -85,7 +85,24 @@ public class QueryElevationComponentTest extends AbstractSolrTestCase { assertEquals( "xxxx", comp.getAnalyzedQuery( "XXXX" ) ); assertEquals( "xxxxyyyy", comp.getAnalyzedQuery( "XXXX YYYY" ) ); } - + + public void testEmptyQuery() throws Exception { + SolrCore core = h.getCore(); + + + + //String query = "title:ipod"; + + Map args = new HashMap(); + args.put( "q.alt", "*:*" ); + args.put( "defType", "dismax"); + //args.put( CommonParams.FL, "id,title,score" ); + SolrQueryRequest req = new LocalSolrQueryRequest( core, new MapSolrParams( args) ); + assertQ("Make sure QEC handles null queries", req, "//*[@numFound='0']"); + + } + + public void testSorting() throws IOException { SolrCore core = h.getCore();