add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / grouping / src / java / org / apache / lucene / search / grouping / TermFirstPassGroupingCollector.java
1 package org.apache.lucene.search.grouping;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.IOException;
21
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.search.FieldCache;
24 import org.apache.lucene.search.Sort;
25
26 /**
27  * Concrete implementation of {@link AbstractFirstPassGroupingCollector} that groups based on
28  * field values and more specifically uses {@link org.apache.lucene.search.FieldCache.StringIndex}
29  * to collect groups.
30  *
31  * @lucene.experimental
32  */
33 public class TermFirstPassGroupingCollector extends AbstractFirstPassGroupingCollector<String> {
34
35   private FieldCache.StringIndex index;
36
37   private String groupField;
38
39   /**
40    * Create the first pass collector.
41    *
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
45    *    per-document).
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
51    *    Sort.RELEVANCE.
52    *  @param topNGroups How many top groups to keep.
53    *  @throws IOException When I/O related errors occur
54    */
55   public TermFirstPassGroupingCollector(String groupField, Sort groupSort, int topNGroups) throws IOException {
56     super(groupSort, topNGroups);
57     this.groupField = groupField;
58   }
59
60   @Override
61   protected String getDocGroupValue(int doc) {
62     final int ord = index.order[doc];
63     return ord == 0 ? null : index.lookup[ord];
64   }
65
66   @Override
67   protected String copyDocGroupValue(String groupValue, String reuse) {
68     return groupValue;
69   }
70
71   @Override
72   public void setNextReader(IndexReader reader, int docBase) throws IOException {
73     super.setNextReader(reader, docBase);
74     index = FieldCache.DEFAULT.getStringIndex(reader, groupField);
75   }
76 }