You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2015/03/04 10:23:48 UTC
svn commit: r1663899 - in /lucene/dev/trunk/lucene: ./
core/src/java/org/apache/lucene/search/
core/src/test/org/apache/lucene/search/
queryparser/src/java/org/apache/lucene/queryparser/simple/
queryparser/src/test/org/apache/lucene/queryparser/simple/
Author: jpountz
Date: Wed Mar 4 09:23:48 2015
New Revision: 1663899
URL: http://svn.apache.org/r1663899
Log:
LUCENE-6304: Add MatchNoDocsQuery.
Added:
lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/MatchNoDocsQuery.java (with props)
lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestMatchNoDocsQuery.java (with props)
Modified:
lucene/dev/trunk/lucene/CHANGES.txt
lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/Query.java
lucene/dev/trunk/lucene/queryparser/src/java/org/apache/lucene/queryparser/simple/SimpleQueryParser.java
lucene/dev/trunk/lucene/queryparser/src/test/org/apache/lucene/queryparser/simple/TestSimpleQueryParser.java
Modified: lucene/dev/trunk/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/CHANGES.txt?rev=1663899&r1=1663898&r2=1663899&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/CHANGES.txt (original)
+++ lucene/dev/trunk/lucene/CHANGES.txt Wed Mar 4 09:23:48 2015
@@ -54,6 +54,9 @@ New Features
* LUCENE-6303: Added filter caching baked into IndexSearcher and enabled by
default. (Adrien Grand)
+* LUCENE-6304: Added a new MatchNoDocsQuery that matches no documents.
+ (Lee Hinman via Adrien Grand)
+
Bug Fixes
* LUCENE-6249: StandardQueryParser doesn't support pure negative clauses.
Added: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/MatchNoDocsQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/MatchNoDocsQuery.java?rev=1663899&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/MatchNoDocsQuery.java (added)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/MatchNoDocsQuery.java Wed Mar 4 09:23:48 2015
@@ -0,0 +1,47 @@
+package org.apache.lucene.search;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.IOException;
+import java.util.Set;
+
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.ToStringUtils;
+
+/**
+ * A query that matches no documents.
+ */
+public class MatchNoDocsQuery extends Query {
+
+ @Override
+ public Query rewrite(IndexReader reader) throws IOException {
+ // Rewrite to an empty BooleanQuery so no Scorer or Weight is required
+ return new BooleanQuery();
+ }
+
+ @Override
+ public String toString(String field) {
+ StringBuilder buffer = new StringBuilder();
+ buffer.append("_none_");
+ buffer.append(ToStringUtils.boost(getBoost()));
+ return buffer.toString();
+ }
+}
Modified: lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/Query.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/Query.java?rev=1663899&r1=1663898&r2=1663899&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/Query.java (original)
+++ lucene/dev/trunk/lucene/core/src/java/org/apache/lucene/search/Query.java Wed Mar 4 09:23:48 2015
@@ -112,10 +112,7 @@ public abstract class Query implements C
@Override
public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + Float.floatToIntBits(boost);
- return result;
+ return Float.floatToIntBits(getBoost()) ^ getClass().hashCode();
}
@Override
Added: lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestMatchNoDocsQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestMatchNoDocsQuery.java?rev=1663899&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestMatchNoDocsQuery.java (added)
+++ lucene/dev/trunk/lucene/core/src/test/org/apache/lucene/search/TestMatchNoDocsQuery.java Wed Mar 4 09:23:48 2015
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search;
+
+import java.io.IOException;
+
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.MockAnalyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.store.Directory;
+
+import org.apache.lucene.util.LuceneTestCase;
+
+/**
+ * Tests MatchNoDocsQuery.
+ */
+public class TestMatchNoDocsQuery extends LuceneTestCase {
+ private Analyzer analyzer;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ analyzer = new MockAnalyzer(random());
+ }
+
+ public void testQuery() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(analyzer).setMaxBufferedDocs(2).setMergePolicy(newLogMergePolicy()));
+ addDoc("one", iw);
+ addDoc("two", iw);
+ addDoc("three four", iw);
+ IndexReader ir = DirectoryReader.open(iw, true);
+
+ IndexSearcher is = newSearcher(ir);
+ ScoreDoc[] hits;
+
+ hits = is.search(new MatchNoDocsQuery(), 1000).scoreDocs;
+ assertEquals(0, hits.length);
+
+ // A MatchNoDocsQuery rewrites to an empty BooleanQuery
+ MatchNoDocsQuery mndq = new MatchNoDocsQuery();
+ Query rewritten = mndq.rewrite(ir);
+ assertTrue(rewritten instanceof BooleanQuery);
+ assertEquals(0, ((BooleanQuery) rewritten).clauses().size());
+ hits = is.search(mndq, 1000).scoreDocs;
+ assertEquals(0, hits.length);
+
+ iw.close();
+ ir.close();
+ dir.close();
+ }
+
+ public void testEquals() {
+ Query q1 = new MatchNoDocsQuery();
+ Query q2 = new MatchNoDocsQuery();
+ assertTrue(q1.equals(q2));
+ QueryUtils.check(q1);
+ }
+
+ private void addDoc(String text, IndexWriter iw) throws IOException {
+ Document doc = new Document();
+ Field f = newTextField("key", text, Field.Store.YES);
+ doc.add(f);
+ iw.addDocument(doc);
+ }
+
+}
Modified: lucene/dev/trunk/lucene/queryparser/src/java/org/apache/lucene/queryparser/simple/SimpleQueryParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/queryparser/src/java/org/apache/lucene/queryparser/simple/SimpleQueryParser.java?rev=1663899&r1=1663898&r2=1663899&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/queryparser/src/java/org/apache/lucene/queryparser/simple/SimpleQueryParser.java (original)
+++ lucene/dev/trunk/lucene/queryparser/src/java/org/apache/lucene/queryparser/simple/SimpleQueryParser.java Wed Mar 4 09:23:48 2015
@@ -23,6 +23,7 @@ import org.apache.lucene.search.BooleanC
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.QueryBuilder;
@@ -149,7 +150,7 @@ public class SimpleQueryParser extends Q
State state = new State(data, buffer, 0, data.length);
parseSubQuery(state);
if (state.top == null) {
- return new BooleanQuery();
+ return new MatchNoDocsQuery();
} else {
return state.top;
}
Modified: lucene/dev/trunk/lucene/queryparser/src/test/org/apache/lucene/queryparser/simple/TestSimpleQueryParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/queryparser/src/test/org/apache/lucene/queryparser/simple/TestSimpleQueryParser.java?rev=1663899&r1=1663898&r2=1663899&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/queryparser/src/test/org/apache/lucene/queryparser/simple/TestSimpleQueryParser.java (original)
+++ lucene/dev/trunk/lucene/queryparser/src/test/org/apache/lucene/queryparser/simple/TestSimpleQueryParser.java Wed Mar 4 09:23:48 2015
@@ -29,6 +29,7 @@ import org.apache.lucene.search.BooleanC
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.PhraseQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
@@ -273,7 +274,7 @@ public class TestSimpleQueryParser exten
}
public void testGarbageEmpty() throws Exception {
- BooleanQuery expected = new BooleanQuery();
+ MatchNoDocsQuery expected = new MatchNoDocsQuery();
assertEquals(expected, parse(""));
assertEquals(expected, parse(" "));
@@ -645,4 +646,4 @@ public class TestSimpleQueryParser exten
parseKeyword(sb.toString(), TestUtil.nextInt(random(), 0, 1024)); // no exception
}
}
-}
\ No newline at end of file
+}