|
Lucene example source code file (BooleanFilter.java)
The Lucene BooleanFilter.java source codepackage 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.ArrayList; import org.apache.lucene.index.IndexReader; import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.util.OpenBitSet; import org.apache.lucene.util.OpenBitSetDISI; /** * A container Filter that allows Boolean composition of Filters. * Filters are allocated into one of three logical constructs; * SHOULD, MUST NOT, MUST * The results Filter BitSet is constructed as follows: * SHOULD Filters are OR'd together * The resulting Filter is NOT'd with the NOT Filters * The resulting Filter is AND'd with the MUST Filters */ public class BooleanFilter extends Filter { ArrayList<Filter> shouldFilters = null; ArrayList<Filter> notFilters = null; ArrayList<Filter> mustFilters = null; private DocIdSetIterator getDISI(ArrayList<Filter> filters, int index, IndexReader reader) throws IOException { return filters.get(index).getDocIdSet(reader).iterator(); } /** * Returns the a DocIdSetIterator representing the Boolean composition * of the filters that have been added. */ @Override public DocIdSet getDocIdSet(IndexReader reader) throws IOException { OpenBitSetDISI res = null; if (shouldFilters != null) { for (int i = 0; i < shouldFilters.size(); i++) { if (res == null) { res = new OpenBitSetDISI(getDISI(shouldFilters, i, reader), reader.maxDoc()); } else { DocIdSet dis = shouldFilters.get(i).getDocIdSet(reader); if(dis instanceof OpenBitSet) { // optimized case for OpenBitSets res.or((OpenBitSet) dis); } else { res.inPlaceOr(getDISI(shouldFilters, i, reader)); } } } } if (notFilters!=null) { for (int i = 0; i < notFilters.size(); i++) { if (res == null) { res = new OpenBitSetDISI(getDISI(notFilters, i, reader), reader.maxDoc()); res.flip(0, reader.maxDoc()); // NOTE: may set bits on deleted docs } else { DocIdSet dis = notFilters.get(i).getDocIdSet(reader); if(dis instanceof OpenBitSet) { // optimized case for OpenBitSets res.andNot((OpenBitSet) dis); } else { res.inPlaceNot(getDISI(notFilters, i, reader)); } } } } if (mustFilters!=null) { for (int i = 0; i < mustFilters.size(); i++) { if (res == null) { res = new OpenBitSetDISI(getDISI(mustFilters, i, reader), reader.maxDoc()); } else { DocIdSet dis = mustFilters.get(i).getDocIdSet(reader); if(dis instanceof OpenBitSet) { // optimized case for OpenBitSets res.and((OpenBitSet) dis); } else { res.inPlaceAnd(getDISI(mustFilters, i, reader)); } } } } if (res !=null) return finalResult(res, reader.maxDoc()); return DocIdSet.EMPTY_DOCIDSET; } /** Provide a SortedVIntList when it is definitely smaller * than an OpenBitSet. * @deprecated Either use CachingWrapperFilter, or * switch to a different DocIdSet implementation yourself. * This method will be removed in Lucene 4.0 */ @Deprecated protected final DocIdSet finalResult(OpenBitSetDISI result, int maxDocs) { return result; } /** * Adds a new FilterClause to the Boolean Filter container * @param filterClause A FilterClause object containing a Filter and an Occur parameter */ public void add(FilterClause filterClause) { if (filterClause.getOccur().equals(Occur.MUST)) { if (mustFilters==null) { mustFilters=new ArrayList<Filter>(); } mustFilters.add(filterClause.getFilter()); } if (filterClause.getOccur().equals(Occur.SHOULD)) { if (shouldFilters==null) { shouldFilters=new ArrayList<Filter>(); } shouldFilters.add(filterClause.getFilter()); } if (filterClause.getOccur().equals(Occur.MUST_NOT)) { if (notFilters==null) { notFilters=new ArrayList<Filter>(); } notFilters.add(filterClause.getFilter()); } } private boolean equalFilters(ArrayList<Filter> filters1, ArrayList Other Lucene examples (source code examples)Here is a short list of links related to this Lucene BooleanFilter.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.