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

Lucene example source code file (FilteringTokenFilter.java)

This example Lucene source code file (FilteringTokenFilter.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

filteringtokenfilter, filteringtokenfilter, io, ioexception, ioexception, override, positionincrementattribute, positionincrementattribute, tokenfilter, tokenstream, tokenstream

The Lucene FilteringTokenFilter.java source code

package org.apache.lucene.analysis;

 * 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,
 * See the License for the specific language governing permissions and
 * limitations under the License.

import java.io.IOException;

import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.queryParser.QueryParser; // for javadoc

 * Abstract base class for TokenFilters that may remove tokens.
 * You have to implement {@link #accept} and return a boolean if the current
 * token should be preserved. {@link #incrementToken} uses this method
 * to decide if a token should be passed to the caller.
public abstract class FilteringTokenFilter extends TokenFilter {

  private final PositionIncrementAttribute posIncrAtt = addAttribute(PositionIncrementAttribute.class);
  private boolean enablePositionIncrements; // no init needed, as ctor enforces setting value!

  public FilteringTokenFilter(boolean enablePositionIncrements, TokenStream input){
    this.enablePositionIncrements = enablePositionIncrements;

  /** Override this method and return if the current input token should be returned by {@link #incrementToken}. */
  protected abstract boolean accept() throws IOException;

  public final boolean incrementToken() throws IOException {
    if (enablePositionIncrements) {
      int skippedPositions = 0;
      while (input.incrementToken()) {
        if (accept()) {
          if (skippedPositions != 0) {
            posIncrAtt.setPositionIncrement(posIncrAtt.getPositionIncrement() + skippedPositions);
          return true;
        skippedPositions += posIncrAtt.getPositionIncrement();
    } else {
      while (input.incrementToken()) {
        if (accept()) {
          return true;
    // reached EOS -- return false
    return false;

   * @see #setEnablePositionIncrements(boolean)
  public boolean getEnablePositionIncrements() {
    return enablePositionIncrements;

   * If <code>true, this TokenFilter will preserve
   * positions of the incoming tokens (ie, accumulate and
   * set position increments of the removed tokens).
   * Generally, <code>true is best as it does not
   * lose information (positions of the original tokens)
   * during indexing.
   * <p> When set, when a token is stopped
   * (omitted), the position increment of the following
   * token is incremented.
   * <p> NOTE: be sure to also
   * set {@link QueryParser#setEnablePositionIncrements} if
   * you use QueryParser to create queries.
  public void setEnablePositionIncrements(boolean enable) {
    this.enablePositionIncrements = enable;

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene FilteringTokenFilter.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.