1 package org.apache.lucene.search.grouping;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.search.FieldCache;
24 import org.apache.lucene.search.Sort;
27 * Concrete implementation of {@link AbstractFirstPassGroupingCollector} that groups based on
28 * field values and more specifically uses {@link org.apache.lucene.search.FieldCache.StringIndex}
31 * @lucene.experimental
33 public class TermFirstPassGroupingCollector extends AbstractFirstPassGroupingCollector<String> {
35 private FieldCache.StringIndex index;
37 private String groupField;
40 * Create the first pass collector.
42 * @param groupField The field used to group
43 * documents. This field must be single-valued and
44 * indexed (FieldCache is used to access its value
46 * @param groupSort The {@link Sort} used to sort the
47 * groups. The top sorted document within each group
48 * according to groupSort, determines how that group
49 * sorts against other groups. This must be non-null,
50 * ie, if you want to groupSort by relevance use
52 * @param topNGroups How many top groups to keep.
53 * @throws IOException When I/O related errors occur
55 public TermFirstPassGroupingCollector(String groupField, Sort groupSort, int topNGroups) throws IOException {
56 super(groupSort, topNGroups);
57 this.groupField = groupField;
61 protected String getDocGroupValue(int doc) {
62 final int ord = index.order[doc];
63 return ord == 0 ? null : index.lookup[ord];
67 protected String copyDocGroupValue(String groupValue, String reuse) {
72 public void setNextReader(IndexReader reader, int docBase) throws IOException {
73 super.setNextReader(reader, docBase);
74 index = FieldCache.DEFAULT.getStringIndex(reader, groupField);