mirror of https://github.com/apache/lucene.git
SOLR-1930: remove unused prefix and wildcard filters, which have been moved to lucene
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@950784 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d9f5941e76
commit
2cf56fcdca
|
@ -1,81 +0,0 @@
|
|||
/**
|
||||
* 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.solr.search;
|
||||
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.apache.lucene.search.ConstantScoreQuery;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @version $Id$
|
||||
*/
|
||||
public class ConstantScorePrefixQuery extends Query {
|
||||
private final Term prefix;
|
||||
|
||||
public ConstantScorePrefixQuery(Term prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
/** Returns the prefix for this query */
|
||||
public Term getPrefix() { return prefix; }
|
||||
|
||||
public Query rewrite(IndexReader reader) throws IOException {
|
||||
// TODO: if number of terms are low enough, rewrite to a BooleanQuery
|
||||
// for potentially faster execution.
|
||||
// TODO: cache the bitset somewhere instead of regenerating it
|
||||
Query q = new ConstantScoreQuery(new PrefixFilter(prefix));
|
||||
q.setBoost(getBoost());
|
||||
return q;
|
||||
}
|
||||
|
||||
/** Prints a user-readable version of this query. */
|
||||
public String toString(String field)
|
||||
{
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
if (!prefix.field().equals(field)) {
|
||||
buffer.append(prefix.field());
|
||||
buffer.append(":");
|
||||
}
|
||||
buffer.append(prefix.text());
|
||||
buffer.append('*');
|
||||
if (getBoost() != 1.0f) {
|
||||
buffer.append("^");
|
||||
buffer.append(Float.toString(getBoost()));
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/** Returns true if <code>o</code> is equal to this. */
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof ConstantScorePrefixQuery)) return false;
|
||||
ConstantScorePrefixQuery other = (ConstantScorePrefixQuery) o;
|
||||
return this.prefix.equals(other.prefix) && this.getBoost()==other.getBoost();
|
||||
}
|
||||
|
||||
/** Returns a hash code value for this object.*/
|
||||
public int hashCode() {
|
||||
int h = prefix.hashCode() ^ Float.floatToIntBits(getBoost());
|
||||
h ^= (h << 14) | (h >>> 19); // reversible (1 to 1) transformation unique to ConstantScorePrefixQuery
|
||||
return h;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,116 +0,0 @@
|
|||
/**
|
||||
* 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.solr.search;
|
||||
|
||||
import org.apache.lucene.search.Filter;
|
||||
import org.apache.lucene.search.DocIdSet;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.TermEnum;
|
||||
import org.apache.lucene.index.TermDocs;
|
||||
import org.apache.lucene.util.OpenBitSet;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @version $Id$
|
||||
*/
|
||||
public class PrefixFilter extends Filter {
|
||||
protected final Term prefix;
|
||||
|
||||
PrefixFilter(Term prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
Term getPrefix() { return prefix; }
|
||||
|
||||
@Override
|
||||
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
|
||||
final OpenBitSet bitSet = new OpenBitSet(reader.maxDoc());
|
||||
new PrefixGenerator(prefix) {
|
||||
public void handleDoc(int doc) {
|
||||
bitSet.set(doc);
|
||||
}
|
||||
}.generate(reader);
|
||||
return bitSet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof PrefixFilter && ((PrefixFilter)o).prefix.equals(this.prefix);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 0xcecf7fe2 + prefix.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString () {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("PrefixFilter(");
|
||||
sb.append(prefix.toString());
|
||||
sb.append(")");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
// keep this protected until I decide if it's a good way
|
||||
// to separate id generation from collection (or should
|
||||
// I just reuse hitcollector???)
|
||||
interface IdGenerator {
|
||||
public void generate(IndexReader reader) throws IOException;
|
||||
public void handleDoc(int doc);
|
||||
}
|
||||
|
||||
|
||||
abstract class PrefixGenerator implements IdGenerator {
|
||||
protected final Term prefix;
|
||||
|
||||
PrefixGenerator(Term prefix) {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
public void generate(IndexReader reader) throws IOException {
|
||||
TermEnum enumerator = reader.terms(prefix);
|
||||
TermDocs termDocs = reader.termDocs();
|
||||
|
||||
try {
|
||||
|
||||
String prefixText = prefix.text();
|
||||
String prefixField = prefix.field();
|
||||
do {
|
||||
Term term = enumerator.term();
|
||||
if (term != null &&
|
||||
term.text().startsWith(prefixText) &&
|
||||
term.field() == prefixField)
|
||||
{
|
||||
termDocs.seek(term);
|
||||
while (termDocs.next()) {
|
||||
handleDoc(termDocs.doc());
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} while (enumerator.next());
|
||||
} finally {
|
||||
termDocs.close();
|
||||
enumerator.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,101 +0,0 @@
|
|||
/**
|
||||
* 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.solr.search;
|
||||
|
||||
import org.apache.lucene.search.Filter;
|
||||
import org.apache.lucene.search.DocIdSet;
|
||||
import org.apache.lucene.search.WildcardTermEnum;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.TermEnum;
|
||||
import org.apache.lucene.index.TermDocs;
|
||||
import org.apache.lucene.util.OpenBitSet;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @version $Id$
|
||||
*/
|
||||
public class WildcardFilter extends Filter {
|
||||
protected final Term term;
|
||||
|
||||
public WildcardFilter(Term wildcardTerm) {
|
||||
this.term = wildcardTerm;
|
||||
}
|
||||
|
||||
public Term getTerm() { return term; }
|
||||
|
||||
@Override
|
||||
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
|
||||
final OpenBitSet bitSet = new OpenBitSet(reader.maxDoc());
|
||||
new WildcardGenerator(term) {
|
||||
public void handleDoc(int doc) {
|
||||
bitSet.set(doc);
|
||||
}
|
||||
}.generate(reader);
|
||||
return bitSet;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof WildcardFilter && ((WildcardFilter)o).term.equals(this.term);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return term.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString () {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("WildcardFilter(");
|
||||
sb.append(term.toString());
|
||||
sb.append(")");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
abstract class WildcardGenerator implements IdGenerator {
|
||||
protected final Term wildcard;
|
||||
|
||||
WildcardGenerator(Term wildcard) {
|
||||
this.wildcard = wildcard;
|
||||
}
|
||||
|
||||
public void generate(IndexReader reader) throws IOException {
|
||||
TermEnum enumerator = new WildcardTermEnum(reader, wildcard);
|
||||
TermDocs termDocs = reader.termDocs();
|
||||
try {
|
||||
do {
|
||||
Term term = enumerator.term();
|
||||
if (term==null) break;
|
||||
termDocs.seek(term);
|
||||
while (termDocs.next()) {
|
||||
handleDoc(termDocs.doc());
|
||||
}
|
||||
} while (enumerator.next());
|
||||
} finally {
|
||||
termDocs.close();
|
||||
enumerator.close();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue