home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (SpanQueryFilter.java)

This example Lucene source code file (SpanQueryFilter.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

docidset, io, ioexception, ioexception, openbitset, override, override, spanfilter, spanfilterresult, spanfilterresult, spanquery, spanqueryfilter, spanqueryfilter, spans, string, util

The Lucene SpanQueryFilter.java source code

package org.apache.lucene.search;
 * Copyright 2007 The Apache Software Foundation
 * Licensed 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,
 * See the License for the specific language governing permissions and
 * limitations under the License.

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.search.spans.Spans;
import org.apache.lucene.util.OpenBitSet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

 * Constrains search results to only match those which also match a provided
 * query. Also provides position information about where each document matches
 * at the cost of extra space compared with the QueryWrapperFilter.
 * There is an added cost to this above what is stored in a {@link QueryWrapperFilter}.  Namely,
 * the position information for each matching document is stored.
 * <p/>
 * This filter does not cache.  See the {@link org.apache.lucene.search.CachingSpanFilter} for a wrapper that
 * caches.
public class SpanQueryFilter extends SpanFilter {
  protected SpanQuery query;

  protected SpanQueryFilter()

  /** Constructs a filter which only matches documents matching
   * <code>query.
   * @param query The {@link org.apache.lucene.search.spans.SpanQuery} to use as the basis for the Filter.
  public SpanQueryFilter(SpanQuery query) {
    this.query = query;

  public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
    SpanFilterResult result = bitSpans(reader);
    return result.getDocIdSet();

  public SpanFilterResult bitSpans(IndexReader reader) throws IOException {

    final OpenBitSet bits = new OpenBitSet(reader.maxDoc());
    Spans spans = query.getSpans(reader);
    List<SpanFilterResult.PositionInfo> tmp = new ArrayList(20);
    int currentDoc = -1;
    SpanFilterResult.PositionInfo currentInfo = null;
    while (spans.next())
      int doc = spans.doc();
      if (currentDoc != doc)
        currentInfo = new SpanFilterResult.PositionInfo(doc);
        currentDoc = doc;
      currentInfo.addPosition(spans.start(), spans.end());
    return new SpanFilterResult(bits, tmp);

  public SpanQuery getQuery() {
    return query;

  public String toString() {
    return "SpanQueryFilter(" + query + ")";

  public boolean equals(Object o) {
    return o instanceof SpanQueryFilter && this.query.equals(((SpanQueryFilter) o).query);

  public int hashCode() {
    return query.hashCode() ^ 0x923F64B9;

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene SpanQueryFilter.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 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.