alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (DistanceFieldComparatorSource.java)

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

distancefieldcomparatorsource, distancefieldcomparatorsource, distancefilter, distancescoredoclookupcomparator, distancescoredoclookupcomparator, double, fieldcomparator, fieldcomparatorsource, io, ioexception, ioexception, override, override

The Lucene DistanceFieldComparatorSource.java source code

/**
 * 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.lucene.spatial.tier;

import java.io.IOException;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.FieldComparator;
import org.apache.lucene.search.FieldComparatorSource;

/**
 * <p>NOTE: This API is still in
 * flux and might change in incompatible ways in the next
 * release.</font>
 */
public class DistanceFieldComparatorSource extends FieldComparatorSource {

  private static final long serialVersionUID = 1L;

  private DistanceFilter distanceFilter;
  private DistanceScoreDocLookupComparator dsdlc;

  public DistanceFieldComparatorSource(Filter distanceFilter) {
    this.distanceFilter = (DistanceFilter) distanceFilter;
  }

  public void cleanUp() {
    distanceFilter = null;

    if (dsdlc != null) {
      dsdlc.cleanUp();
    }

    dsdlc = null;
  }

  @Override
  public FieldComparator newComparator(String fieldname, int numHits,
                                         int sortPos, boolean reversed) throws IOException {
    dsdlc = new DistanceScoreDocLookupComparator(numHits);
    return dsdlc;
  }

  private class DistanceScoreDocLookupComparator extends FieldComparator<Double> {

    private double[] values;
    private double bottom;
    private int offset =0;
		
    public DistanceScoreDocLookupComparator(int numHits) {
      values = new double[numHits];
      return;
    }

    @Override
    public int compare(int slot1, int slot2) {
      double a = values[slot1];
      double b = values[slot2];
      if (a > b)
        return 1;
      if (a < b)
        return -1;

      return 0;
    }

    public void cleanUp() {
      distanceFilter = null;
    }

    @Override
    public int compareBottom(int doc) {
      double v2 = distanceFilter.getDistance(doc+ offset);
			
      if (bottom > v2) {
        return 1;
      } else if (bottom < v2) {
        return -1;
      }
      return 0;
    }

    @Override
    public void copy(int slot, int doc) {
      values[slot] = distanceFilter.getDistance(doc + offset);
    }

    @Override
    public void setBottom(int slot) {
      this.bottom = values[slot];
    }

    @Override
    public void setNextReader(IndexReader reader, int docBase)
      throws IOException {
			
      // each reader in a segmented base
      // has an offset based on the maxDocs of previous readers
      offset = docBase;
    }

    @Override
    public Double value(int slot) {
      return values[slot];
    }
  }
}

Other Lucene examples (source code examples)

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

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

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.