mirror of https://github.com/apache/lucene.git
LUCENE-6304: Add MatchNoDocsQuery.
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1663899 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c1c593a0ad
commit
4d6df6a480
|
@ -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.
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -112,10 +112,7 @@ public abstract class Query implements Cloneable {
|
|||
|
||||
@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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -23,6 +23,7 @@ import org.apache.lucene.search.BooleanClause;
|
|||
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 QueryBuilder {
|
|||
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;
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@ import org.apache.lucene.search.BooleanClause.Occur;
|
|||
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 extends LuceneTestCase {
|
|||
}
|
||||
|
||||
public void testGarbageEmpty() throws Exception {
|
||||
BooleanQuery expected = new BooleanQuery();
|
||||
MatchNoDocsQuery expected = new MatchNoDocsQuery();
|
||||
|
||||
assertEquals(expected, parse(""));
|
||||
assertEquals(expected, parse(" "));
|
||||
|
|
Loading…
Reference in New Issue