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 org.apache.lucene.analysis.MockAnalyzer;
21 import org.apache.lucene.document.Document;
22 import org.apache.lucene.document.Field;
23 import org.apache.lucene.index.RandomIndexWriter;
24 import org.apache.lucene.index.Term;
25 import org.apache.lucene.search.IndexSearcher;
26 import org.apache.lucene.search.TermQuery;
27 import org.apache.lucene.store.Directory;
28 import org.apache.lucene.util.LuceneTestCase;
30 public class TermAllGroupsCollectorTest extends LuceneTestCase {
32 public void testTotalGroupCount() throws Exception {
34 final String groupField = "author";
36 Directory dir = newDirectory();
37 RandomIndexWriter w = new RandomIndexWriter(
40 newIndexWriterConfig(TEST_VERSION_CURRENT,
41 new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
43 Document doc = new Document();
44 doc.add(new Field(groupField, "author1", Field.Store.YES, Field.Index.ANALYZED));
45 doc.add(new Field("content", "random text", Field.Store.YES, Field.Index.ANALYZED));
46 doc.add(new Field("id", "1", Field.Store.YES, Field.Index.NO));
51 doc.add(new Field(groupField, "author1", Field.Store.YES, Field.Index.ANALYZED));
52 doc.add(new Field("content", "some more random text blob", Field.Store.YES, Field.Index.ANALYZED));
53 doc.add(new Field("id", "2", Field.Store.YES, Field.Index.NO));
58 doc.add(new Field(groupField, "author1", Field.Store.YES, Field.Index.ANALYZED));
59 doc.add(new Field("content", "some more random textual data", Field.Store.YES, Field.Index.ANALYZED));
60 doc.add(new Field("id", "3", Field.Store.YES, Field.Index.NO));
62 w.commit(); // To ensure a second segment
66 doc.add(new Field(groupField, "author2", Field.Store.YES, Field.Index.ANALYZED));
67 doc.add(new Field("content", "some random text", Field.Store.YES, Field.Index.ANALYZED));
68 doc.add(new Field("id", "4", Field.Store.YES, Field.Index.NO));
73 doc.add(new Field(groupField, "author3", Field.Store.YES, Field.Index.ANALYZED));
74 doc.add(new Field("content", "some more random text", Field.Store.YES, Field.Index.ANALYZED));
75 doc.add(new Field("id", "5", Field.Store.YES, Field.Index.NO));
80 doc.add(new Field(groupField, "author3", Field.Store.YES, Field.Index.ANALYZED));
81 doc.add(new Field("content", "random blob", Field.Store.YES, Field.Index.ANALYZED));
82 doc.add(new Field("id", "6", Field.Store.YES, Field.Index.NO));
85 // 6 -- no author field
87 doc.add(new Field("content", "random word stuck in alot of other text", Field.Store.YES, Field.Index.ANALYZED));
88 doc.add(new Field("id", "6", Field.Store.YES, Field.Index.NO));
91 IndexSearcher indexSearcher = new IndexSearcher(w.getReader());
94 TermAllGroupsCollector c1 = new TermAllGroupsCollector(groupField);
95 indexSearcher.search(new TermQuery(new Term("content", "random")), c1);
96 assertEquals(4, c1.getGroupCount());
98 TermAllGroupsCollector c2 = new TermAllGroupsCollector(groupField);
99 indexSearcher.search(new TermQuery(new Term("content", "some")), c2);
100 assertEquals(3, c2.getGroupCount());
102 TermAllGroupsCollector c3 = new TermAllGroupsCollector(groupField);
103 indexSearcher.search(new TermQuery(new Term("content", "blob")), c3);
104 assertEquals(2, c3.getGroupCount());
106 indexSearcher.getIndexReader().close();