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

Lucene example source code file (TermAllGroupsCollector.java)

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

abstractallgroupscollector, arraylist, collection, default_initial_size, default_initial_size, io, ioexception, ioexception, list, sentinelintset, string, string, termallgroupscollector, termallgroupscollector, util

The Lucene TermAllGroupsCollector.java source code

package org.apache.lucene.search.grouping;

/*
 * 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 org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.FieldCache;

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

/**
 * A collector that collects all groups that match the
 * query. Only the group value is collected, and the order
 * is undefined.  This collector does not determine
 * the most relevant document of a group.
 *
 * <p/>
 * Implementation detail: an int hash set (SentinelIntSet)
 * is used to detect if a group is already added to the
 * total count.  For each segment the int set is cleared and filled
 * with previous counted groups that occur in the new
 * segment.
 *
 * @lucene.experimental
 */
public class TermAllGroupsCollector extends AbstractAllGroupsCollector<String> {

  private static final int DEFAULT_INITIAL_SIZE = 128;

  private final String groupField;
  private final SentinelIntSet ordSet;
  private final List<String> groups;

  private FieldCache.StringIndex index;

  /**
   * Expert: Constructs a {@link AbstractAllGroupsCollector}
   *
   * @param groupField  The field to group by
   * @param initialSize The initial allocation size of the
   *                    internal int set and group list
   *                    which should roughly match the total
   *                    number of expected unique groups. Be aware that the
   *                    heap usage is 4 bytes * initialSize.
   */
  public TermAllGroupsCollector(String groupField, int initialSize) {
    ordSet = new SentinelIntSet(initialSize, -1);
    groups = new ArrayList<String>(initialSize);
    this.groupField = groupField;
  }

  /**
   * Constructs a {@link AbstractAllGroupsCollector}. This sets the
   * initial allocation size for the internal int set and group
   * list to 128.
   *
   * @param groupField The field to group by
   */
  public TermAllGroupsCollector(String groupField) {
    this(groupField, DEFAULT_INITIAL_SIZE);
  }

  public void collect(int doc) throws IOException {
    int key = index.order[doc];
    if (!ordSet.exists(key)) {
      ordSet.put(key);
      String term = key == 0 ? null : index.lookup[key];
      groups.add(term);
    }
  }

  /**
   * {@inheritDoc}
   */
  public Collection<String> getGroups() {
    return groups;
  }

  public void setNextReader(IndexReader reader, int docBase) throws IOException {
    index = FieldCache.DEFAULT.getStringIndex(reader, groupField);

    // Clear ordSet and fill it with previous encountered groups that can occur in the current segment.
    ordSet.clear();
    for (String countedGroup : groups) {
      int ord = index.binarySearchLookup(countedGroup);
      if (ord >= 0) {
        ordSet.put(ord);
      }
    }
  }

}

Other Lucene examples (source code examples)

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