pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / search / DrillDownTest.java
1 package org.apache.lucene.facet.search;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5
6 import org.apache.lucene.analysis.MockAnalyzer;
7 import org.apache.lucene.analysis.MockTokenizer;
8 import org.apache.lucene.document.Document;
9 import org.apache.lucene.document.Field;
10 import org.apache.lucene.document.Field.Index;
11 import org.apache.lucene.document.Field.Store;
12 import org.apache.lucene.index.CorruptIndexException;
13 import org.apache.lucene.index.IndexReader;
14 import org.apache.lucene.index.RandomIndexWriter;
15 import org.apache.lucene.index.Term;
16 import org.apache.lucene.search.IndexSearcher;
17 import org.apache.lucene.search.Query;
18 import org.apache.lucene.search.TermQuery;
19 import org.apache.lucene.search.TopDocs;
20 import org.apache.lucene.store.Directory;
21 import org.apache.lucene.store.LockObtainFailedException;
22 import org.junit.AfterClass;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25
26 import org.apache.lucene.util.LuceneTestCase;
27 import org.apache.lucene.facet.index.CategoryDocumentBuilder;
28 import org.apache.lucene.facet.index.params.CategoryListParams;
29 import org.apache.lucene.facet.index.params.PerDimensionIndexingParams;
30 import org.apache.lucene.facet.search.DrillDown;
31 import org.apache.lucene.facet.search.params.FacetSearchParams;
32 import org.apache.lucene.facet.taxonomy.CategoryPath;
33 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
34 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
35 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
36
37 /**
38  * Licensed to the Apache Software Foundation (ASF) under one or more
39  * contributor license agreements.  See the NOTICE file distributed with
40  * this work for additional information regarding copyright ownership.
41  * The ASF licenses this file to You under the Apache License, Version 2.0
42  * (the "License"); you may not use this file except in compliance with
43  * the License.  You may obtain a copy of the License at
44  *
45  *     http://www.apache.org/licenses/LICENSE-2.0
46  *
47  * Unless required by applicable law or agreed to in writing, software
48  * distributed under the License is distributed on an "AS IS" BASIS,
49  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
50  * See the License for the specific language governing permissions and
51  * limitations under the License.
52  */
53
54 public class DrillDownTest extends LuceneTestCase {
55   
56   private FacetSearchParams defaultParams = new FacetSearchParams();
57   private FacetSearchParams nonDefaultParams;
58   private static IndexReader reader;
59   private static DirectoryTaxonomyReader taxo;
60   private static Directory dir;
61   private static Directory taxoDir;
62   
63   public DrillDownTest() throws IOException {
64     PerDimensionIndexingParams iParams = new PerDimensionIndexingParams();
65     CategoryListParams aClParams = new CategoryListParams(new Term("testing_facets_a", "a"));
66     CategoryListParams bClParams = new CategoryListParams(new Term("testing_facets_b", "b"));
67     
68     iParams.addCategoryListParams(new CategoryPath("a"), aClParams);
69     iParams.addCategoryListParams(new CategoryPath("b"), bClParams);
70     
71     nonDefaultParams = new FacetSearchParams(iParams);
72   }
73   @BeforeClass
74   public static void createIndexes() throws CorruptIndexException, LockObtainFailedException, IOException {
75     dir = newDirectory();
76     RandomIndexWriter writer = new RandomIndexWriter(random, dir, 
77         newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.KEYWORD, false)));
78     
79     taxoDir = newDirectory();
80     TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
81     
82     for (int i = 0; i < 100; i++) {
83       ArrayList<CategoryPath> paths = new ArrayList<CategoryPath>();
84       Document doc = new Document();
85       if (i % 2 == 0) { // 50
86         doc.add(new Field("content", "foo", Store.NO, Index.ANALYZED));
87       }
88       if (i % 3 == 0) { // 33
89         doc.add(new Field("content", "bar", Store.NO, Index.ANALYZED));
90       }
91       if (i % 4 == 0) { // 25
92         paths.add(new CategoryPath("a"));
93       }
94       if (i % 5 == 0) { // 20
95         paths.add(new CategoryPath("b"));
96       }
97       CategoryDocumentBuilder builder = new CategoryDocumentBuilder(taxoWriter);
98       builder.setCategoryPaths(paths).build(doc);
99       writer.addDocument(doc);
100     }
101     
102     taxoWriter.close();
103     reader = writer.getReader();
104     writer.close();
105     
106     taxo = new DirectoryTaxonomyReader(taxoDir);
107   }
108   
109   @Test
110   public void testTermNonDefault() {
111     Term termA = DrillDown.term(nonDefaultParams, new CategoryPath("a"));
112     assertEquals(new Term("testing_facets_a", "a"), termA);
113     
114     Term termB = DrillDown.term(nonDefaultParams, new CategoryPath("b"));
115     assertEquals(new Term("testing_facets_b", "b"), termB);
116   }
117   
118   @Test
119   public void testTermDefault() {
120     String defaultField = CategoryListParams.DEFAULT_TERM.field();
121     
122     Term termA = DrillDown.term(defaultParams, new CategoryPath("a"));
123     assertEquals(new Term(defaultField, "a"), termA);
124     
125     Term termB = DrillDown.term(defaultParams, new CategoryPath("b"));
126     assertEquals(new Term(defaultField, "b"), termB);
127   }
128   
129   @Test
130   public void testQuery() throws IOException {
131     IndexSearcher searcher = newSearcher(reader);
132
133     // Making sure the query yields 25 documents with the facet "a"
134     Query q = DrillDown.query(defaultParams, new CategoryPath("a"));
135     TopDocs docs = searcher.search(q, 100);
136     assertEquals(25, docs.totalHits);
137     
138     // Making sure the query yields 5 documents with the facet "b" and the
139     // previous (facet "a") query as a base query
140     Query q2 = DrillDown.query(defaultParams, q, new CategoryPath("b"));
141     docs = searcher.search(q2, 100);
142     assertEquals(5, docs.totalHits);
143
144     // Making sure that a query of both facet "a" and facet "b" yields 5 results
145     Query q3 = DrillDown.query(defaultParams, new CategoryPath("a"), new CategoryPath("b"));
146     docs = searcher.search(q3, 100);
147     assertEquals(5, docs.totalHits);
148     
149     // Check that content:foo (which yields 50% results) and facet/b (which yields 20%)
150     // would gather together 10 results (10%..) 
151     Query fooQuery = new TermQuery(new Term("content", "foo"));
152     Query q4 = DrillDown.query(defaultParams, fooQuery, new CategoryPath("b"));
153     docs = searcher.search(q4, 100);
154     assertEquals(10, docs.totalHits);
155     
156     searcher.close();
157   }
158   
159   @Test
160   public void testQueryImplicitDefaultParams() throws IOException {
161     IndexSearcher searcher = newSearcher(reader);
162
163     // Create the base query to start with
164     Query q = DrillDown.query(defaultParams, new CategoryPath("a"));
165     
166     // Making sure the query yields 5 documents with the facet "b" and the
167     // previous (facet "a") query as a base query
168     Query q2 = DrillDown.query(q, new CategoryPath("b"));
169     TopDocs docs = searcher.search(q2, 100);
170     assertEquals(5, docs.totalHits);
171
172     // Check that content:foo (which yields 50% results) and facet/b (which yields 20%)
173     // would gather together 10 results (10%..) 
174     Query fooQuery = new TermQuery(new Term("content", "foo"));
175     Query q4 = DrillDown.query(fooQuery, new CategoryPath("b"));
176     docs = searcher.search(q4, 100);
177     assertEquals(10, docs.totalHits);
178     
179     searcher.close();
180   }
181   
182   @AfterClass
183   public static void closeIndexes() throws IOException {
184     if (reader != null) {
185       reader.close();
186       reader = null;
187     }
188     
189     if (taxo != null) {
190       taxo.close();
191       taxo = null;
192     }
193     
194     dir.close();
195     taxoDir.close();
196   }
197     
198 }