Skip to content

Apache Lucene open-source search software

SOLR-2996: treat un-fielded * as *:*

Yonik Seeley73a0270ad835
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 8a9727b68a6..f9864509851 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -257,6 +257,11 @@ Other Changes
* SOLR-4458: Sort directions (asc, desc) are now case insensitive
(Shawn Heisey via hossman)
+* SOLR-2996: A bare * without a field specification is treated as *:*
+ by the lucene and edismax query paesers.
+ (hossman, Jan Høydahl, Alan Woodward, yonik)
+
+
================== 4.1.0 ==================
Versions of Major Components
diff --git a/solr/core/src/java/org/apache/solr/parser/SolrQueryParserBase.java b/solr/core/src/java/org/apache/solr/parser/SolrQueryParserBase.java
index 3a9c9f04908..6f68dd5085d 100644
--- a/solr/core/src/java/org/apache/solr/parser/SolrQueryParserBase.java
+++ b/solr/core/src/java/org/apache/solr/parser/SolrQueryParserBase.java
@@ -179,11 +179,20 @@ public abstract class SolrQueryParserBase {
return this.defaultField;
}
+ protected String explicitField;
/** Handles the default field if null is passed */
public String getField(String fieldName) {
+ explicitField = fieldName;
return fieldName != null ? fieldName : this.defaultField;
}
+ /** For a fielded query, returns the actual field specified (i.e. null if default is being used)
+ * myfield:A or myfield:(A B C) will both return "myfield"
+ */
+ public String getExplicitField() {
+ return explicitField;
+ }
+
/**
* @see #setAutoGeneratePhraseQueries(boolean)
*/
@@ -725,7 +734,6 @@ public abstract class SolrQueryParserBase {
Query handleBareTokenQuery(String qfield, Token term, Token fuzzySlop, boolean prefix, boolean wildcard, boolean fuzzy, boolean regexp) throws SyntaxError {
Query q;
- String termImage=discardEscapeChar(term.image);
if (wildcard) {
q = getWildcardQuery(qfield, term.image);
} else if (prefix) {
@@ -744,8 +752,10 @@ public abstract class SolrQueryParserBase {
} else if (fms >= 1.0f && fms != (int) fms) {
throw new SyntaxError("Fractional edit distances are not allowed!");
}
+ String termImage=discardEscapeChar(term.image);
q = getFuzzyQuery(qfield, termImage, fms);
} else {
+ String termImage=discardEscapeChar(term.image);
q = getFieldQuery(qfield, termImage, false);
}
return q;
@@ -970,9 +980,12 @@ public abstract class SolrQueryParserBase {
protected Query getWildcardQuery(String field, String termStr) throws SyntaxError {
checkNullField(field);
// *:* -> MatchAllDocsQuery
- if ("*".equals(field) && "*".equals(termStr)) {
- return newMatchAllDocsQuery();
+ if ("*".equals(termStr)) {
+ if ("*".equals(field) || getExplicitField() == null) {
+ return newMatchAllDocsQuery();
+ }
}
+
FieldType fieldType = schema.getFieldType(field);
termStr = analyzeIfMultitermTermText(field, termStr, fieldType);
// can we use reversed wildcards in this field?
diff --git a/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java b/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java
index 5cd6ea16daa..ab80540468b 100644
--- a/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java
+++ b/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java
@@ -1055,7 +1055,7 @@ public class ExtendedDismaxQParser extends QParser {
@Override
protected Query getWildcardQuery(String field, String val) throws SyntaxError {
if (val.equals("*")) {
- if (field.equals("*")) {
+ if (field.equals("*") || getExplicitField() == null) {
return new MatchAllDocsQuery();
} else{
return getPrefixQuery(field,"");
diff --git a/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java b/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java
index 200c24b2de8..8b93d1e1b02 100755
--- a/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java
+++ b/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java
@@ -80,6 +80,23 @@ public class TestExtendedDismaxParser extends SolrTestCaseJ4 {
assertU(commit());
}
+ @Test
+ public void testSyntax() throws Exception {
+ // a bare * should be treated as *:*
+ assertJQ(req("defType","edismax", "q","*", "df","doesnotexist_s")
+ ,"/response/docs/[0]==" // make sure we get something...
+ );
+ assertJQ(req("defType","edismax", "q","doesnotexist_s:*")
+ ,"/response/numFound==0" // nothing should be found
+ );
+ assertJQ(req("defType","edismax","q","doesnotexist_s:*")
+ ,"/response/numFound==0" // nothing should be found
+ );
+ assertJQ(req("defType","edismax","q","doesnotexist_s:( * * * )")
+ ,"/response/numFound==0" // nothing should be found
+ );
+ }
+
public void testTrailingOperators() throws Exception {
// really just test that exceptions aren't thrown by
diff --git a/solr/core/src/test/org/apache/solr/search/TestSolrQueryParser.java b/solr/core/src/test/org/apache/solr/search/TestSolrQueryParser.java
index 01440db393a..2b7c89659b4 100644
--- a/solr/core/src/test/org/apache/solr/search/TestSolrQueryParser.java
+++ b/solr/core/src/test/org/apache/solr/search/TestSolrQueryParser.java
@@ -92,4 +92,19 @@ public class TestSolrQueryParser extends SolrTestCaseJ4 {
,"/response/numFound==1"
);
}
+
+ @Test
+ public void testSyntax() throws Exception {
+ // a bare * should be treated as *:*
+ assertJQ(req("q","*", "df","doesnotexist_s")
+ ,"/response/docs/[0]==" // make sure we get something...
+ );
+ assertJQ(req("q","doesnotexist_s:*")
+ ,"/response/numFound==0" // nothing should be found
+ );
+ assertJQ(req("q","doesnotexist_s:( * * * )")
+ ,"/response/numFound==0" // nothing should be found
+ );
+ }
+
}