build a match all filter internal implementation and use it where appropiate (instead of using query filter wrapper around a match all query)

This commit is contained in:
kimchy 2010-08-10 18:18:51 +03:00
parent f2018e2f86
commit 5f986ef422
6 changed files with 202 additions and 4 deletions

View File

@ -0,0 +1,80 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search 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.elasticsearch.common.lucene.docset;
import org.apache.lucene.search.DocIdSetIterator;
import java.io.IOException;
/**
* An always positive
*
* @author kimchy (shay.banon)
*/
public class AllDocSet extends DocSet {
private final int maxDoc;
public AllDocSet(int maxDoc) {
this.maxDoc = maxDoc;
}
@Override public boolean isCacheable() {
return true;
}
@Override public boolean get(int doc) throws IOException {
return doc < maxDoc;
}
@Override public DocIdSetIterator iterator() throws IOException {
return new AllDocIdSetIterator(maxDoc);
}
private final class AllDocIdSetIterator extends DocIdSetIterator {
private final int maxDoc;
private int doc = -1;
private AllDocIdSetIterator(int maxDoc) {
this.maxDoc = maxDoc;
}
@Override public int docID() {
return doc;
}
@Override public int nextDoc() throws IOException {
if (++doc < maxDoc) {
return doc;
}
return NO_MORE_DOCS;
}
@Override public int advance(int target) throws IOException {
doc = target;
if (doc < maxDoc) {
return doc;
}
return NO_MORE_DOCS;
}
}
}

View File

@ -0,0 +1,58 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search 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.elasticsearch.common.lucene.search;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.DocIdSet;
import org.apache.lucene.search.Filter;
import org.elasticsearch.common.lucene.docset.AllDocSet;
import java.io.IOException;
/**
* A filter that matches on all docs.
*
* @author kimchy (shay.banon)
*/
public class MatchAllDocsFilter extends Filter {
@Override public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
return new AllDocSet(reader.maxDoc());
}
@Override public int hashCode() {
return this.getClass().hashCode();
}
@Override public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null) {
return false;
}
if (obj.getClass() == this.getClass()) {
return true;
}
return false;
}
}

View File

@ -31,7 +31,10 @@ public class Queries {
public final static MatchAllDocsQuery MATCH_ALL_QUERY = new MatchAllDocsQuery();
public final static QueryWrapperFilter MATCH_ALL_FILTER = new QueryWrapperFilter(MATCH_ALL_QUERY);
/**
* A match all docs filter. Note, requires no caching!.
*/
public final static MatchAllDocsFilter MATCH_ALL_FILTER = new MatchAllDocsFilter();
private final static Field disjuncts;

View File

@ -61,7 +61,7 @@ public class FacetsPhase implements SearchPhase {
// run global facets ...
if (context.searcher().globalCollectors() != null) {
Query query = new DeletionAwareConstantScoreQuery(context.filterCache().cache(Queries.MATCH_ALL_FILTER));
Query query = new DeletionAwareConstantScoreQuery(Queries.MATCH_ALL_FILTER); // no need to cache a MATCH ALL FILTER
if (context.types().length > 0) {
if (context.types().length == 1) {
String type = context.types()[0];

View File

@ -0,0 +1,57 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search 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.elasticsearch.common.lucene.search;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.DeletionAwareConstantScoreQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.elasticsearch.common.lucene.Lucene;
import org.testng.annotations.Test;
import static org.elasticsearch.common.lucene.DocumentBuilder.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
/**
* @author kimchy (shay.banon)
*/
public class MatchAllDocsFilterTests {
@Test public void testMatchAllDocsFilter() throws Exception {
Directory dir = new RAMDirectory();
IndexWriter indexWriter = new IndexWriter(dir, Lucene.STANDARD_ANALYZER, true, IndexWriter.MaxFieldLength.UNLIMITED);
indexWriter.addDocument(doc().add(field("_id", "1")).add(field("text", "lucene")).build());
indexWriter.addDocument(doc().add(field("_id", "2")).add(field("text", "lucene release")).build());
IndexReader reader = indexWriter.getReader();
IndexSearcher searcher = new IndexSearcher(reader);
DeletionAwareConstantScoreQuery query = new DeletionAwareConstantScoreQuery(Queries.MATCH_ALL_FILTER);
long count = Lucene.count(searcher, query, -1);
assertThat(count, equalTo(2l));
reader.close();
indexWriter.close();
}
}

View File

@ -19,9 +19,9 @@
package org.elasticsearch.index.engine;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.lucene.search.Queries;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
@ -60,6 +60,6 @@ public final class EngineSearcherTotalHitsMatcher extends TypeSafeMatcher<Engine
}
public static Matcher<Engine.Searcher> engineSearcherTotalHits(int totalHits) {
return new EngineSearcherTotalHitsMatcher(new MatchAllDocsQuery(), totalHits);
return new EngineSearcherTotalHitsMatcher(Queries.MATCH_ALL_QUERY, totalHits);
}
}