pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / grouping / src / test / org / apache / lucene / search / grouping / TermAllGroupsCollectorTest.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 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;
29
30 public class TermAllGroupsCollectorTest extends LuceneTestCase {
31
32   public void testTotalGroupCount() throws Exception {
33
34     final String groupField = "author";
35
36     Directory dir = newDirectory();
37     RandomIndexWriter w = new RandomIndexWriter(
38                                random,
39                                dir,
40                                newIndexWriterConfig(TEST_VERSION_CURRENT,
41                                                     new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
42     // 0
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));
47     w.addDocument(doc);
48
49     // 1
50     doc = new Document();
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));
54     w.addDocument(doc);
55
56     // 2
57     doc = new Document();
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));
61     w.addDocument(doc);
62     w.commit(); // To ensure a second segment
63
64     // 3
65     doc = new Document();
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));
69     w.addDocument(doc);
70
71     // 4
72     doc = new Document();
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));
76     w.addDocument(doc);
77
78     // 5
79     doc = new Document();
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));
83     w.addDocument(doc);
84
85     // 6 -- no author field
86     doc = new Document();
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));
89     w.addDocument(doc);
90
91     IndexSearcher indexSearcher = new IndexSearcher(w.getReader());
92     w.close();
93
94     TermAllGroupsCollector c1 = new TermAllGroupsCollector(groupField);
95     indexSearcher.search(new TermQuery(new Term("content", "random")), c1);
96     assertEquals(4, c1.getGroupCount());
97
98     TermAllGroupsCollector c2 = new TermAllGroupsCollector(groupField);
99     indexSearcher.search(new TermQuery(new Term("content", "some")), c2);
100     assertEquals(3, c2.getGroupCount());
101
102     TermAllGroupsCollector c3 = new TermAllGroupsCollector(groupField);
103     indexSearcher.search(new TermQuery(new Term("content", "blob")), c3);
104     assertEquals(2, c3.getGroupCount());
105
106     indexSearcher.getIndexReader().close();
107     dir.close();
108   }
109 }