add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / search / association / AssociationsFacetRequestTest.java
1 package org.apache.lucene.facet.search.association;
2
3 import java.util.List;
4
5 import org.apache.lucene.index.IndexReader;
6 import org.apache.lucene.index.RandomIndexWriter;
7 import org.apache.lucene.search.IndexSearcher;
8 import org.apache.lucene.search.MatchAllDocsQuery;
9 import org.apache.lucene.search.Query;
10 import org.apache.lucene.store.Directory;
11 import org.junit.AfterClass;
12 import org.junit.BeforeClass;
13 import org.junit.Test;
14
15 import org.apache.lucene.util.LuceneTestCase;
16 import org.apache.lucene.analysis.MockAnalyzer;
17 import org.apache.lucene.analysis.MockTokenizer;
18 import org.apache.lucene.document.Document;
19 import org.apache.lucene.facet.enhancements.EnhancementsDocumentBuilder;
20 import org.apache.lucene.facet.enhancements.association.AssociationEnhancement;
21 import org.apache.lucene.facet.enhancements.association.AssociationFloatProperty;
22 import org.apache.lucene.facet.enhancements.association.AssociationIntProperty;
23 import org.apache.lucene.facet.enhancements.params.DefaultEnhancementsIndexingParams;
24 import org.apache.lucene.facet.index.CategoryContainer;
25 import org.apache.lucene.facet.search.FacetsCollector;
26 import org.apache.lucene.facet.search.params.FacetSearchParams;
27 import org.apache.lucene.facet.search.params.association.AssociationFloatSumFacetRequest;
28 import org.apache.lucene.facet.search.params.association.AssociationIntSumFacetRequest;
29 import org.apache.lucene.facet.search.results.FacetResult;
30 import org.apache.lucene.facet.taxonomy.CategoryPath;
31 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
32 import org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyReader;
33 import org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter;
34
35 /**
36  * Licensed to the Apache Software Foundation (ASF) under one or more
37  * contributor license agreements.  See the NOTICE file distributed with
38  * this work for additional information regarding copyright ownership.
39  * The ASF licenses this file to You under the Apache License, Version 2.0
40  * (the "License"); you may not use this file except in compliance with
41  * the License.  You may obtain a copy of the License at
42  *
43  *     http://www.apache.org/licenses/LICENSE-2.0
44  *
45  * Unless required by applicable law or agreed to in writing, software
46  * distributed under the License is distributed on an "AS IS" BASIS,
47  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
48  * See the License for the specific language governing permissions and
49  * limitations under the License.
50  */
51
52 /** Test for associations */
53 public class AssociationsFacetRequestTest extends LuceneTestCase {
54
55   private static Directory dir;
56   private static IndexReader reader;
57   private static Directory taxoDir;
58   
59   private static final CategoryPath aint = new CategoryPath("int", "a");
60   private static final CategoryPath bint = new CategoryPath("int", "b");
61   private static final CategoryPath afloat = new CategoryPath("float", "a");
62   private static final CategoryPath bfloat = new CategoryPath("float", "b");
63   
64   @BeforeClass
65   public static void beforeClassAssociationsFacetRequestTest() throws Exception {
66     dir = newDirectory();
67     taxoDir = newDirectory();
68     // preparations - index, taxonomy, content
69     RandomIndexWriter writer = new RandomIndexWriter(random, dir, newIndexWriterConfig(TEST_VERSION_CURRENT, 
70         new MockAnalyzer(random, MockTokenizer.KEYWORD, false)));
71     
72     TaxonomyWriter taxoWriter = new LuceneTaxonomyWriter(taxoDir);
73     
74     EnhancementsDocumentBuilder builder = new EnhancementsDocumentBuilder(
75         taxoWriter, new DefaultEnhancementsIndexingParams(
76             new AssociationEnhancement()));
77     
78     // index documents, 50% have only 'b' and all have 'a'
79     for (int i = 0; i < 100; i++) {
80       Document doc = new Document();
81       CategoryContainer container = new CategoryContainer();
82       container.addCategory(aint, new AssociationIntProperty(2));
83       container.addCategory(afloat, new AssociationFloatProperty(0.5f));
84       if (i % 2 == 0) { // 50
85         container.addCategory(bint, new AssociationIntProperty(3));
86         container.addCategory(bfloat, new AssociationFloatProperty(0.2f));
87       }
88       builder.setCategories(container).build(doc);
89       writer.addDocument(doc);
90     }
91     
92     taxoWriter.close();
93     reader = writer.getReader();
94     writer.close();
95   }
96   
97   @AfterClass
98   public static void afterClassAssociationsFacetRequestTest() throws Exception {
99     reader.close();
100     reader = null;
101     dir.close();
102     dir = null;
103     taxoDir.close();
104     taxoDir = null;
105   }
106   
107   @Test
108   public void testIntSumAssociation() throws Exception {
109     LuceneTaxonomyReader taxo = new LuceneTaxonomyReader(taxoDir);
110
111     // facet requests for two facets
112     FacetSearchParams fsp = new FacetSearchParams();
113     fsp.addFacetRequest(new AssociationIntSumFacetRequest(aint, 10));
114     fsp.addFacetRequest(new AssociationIntSumFacetRequest(bint, 10));
115     
116     Query q = new MatchAllDocsQuery();
117
118     FacetsCollector fc = new FacetsCollector(fsp, reader, taxo);
119     
120     IndexSearcher searcher = newSearcher(reader);
121     searcher.search(q, fc);
122     List<FacetResult> res = fc.getFacetResults();
123     
124     assertNotNull("No results!",res);
125     assertEquals("Wrong number of results!",2, res.size());
126     assertEquals("Wrong count for category 'a'!",200, (int) res.get(0).getFacetResultNode().getValue());
127     assertEquals("Wrong count for category 'b'!",150, (int) res.get(1).getFacetResultNode().getValue());
128     
129     searcher.close();
130     taxo.close();
131   }
132   
133   @Test
134   public void testFloatSumAssociation() throws Exception {
135     LuceneTaxonomyReader taxo = new LuceneTaxonomyReader(taxoDir);
136
137     // facet requests for two facets
138     FacetSearchParams fsp = new FacetSearchParams();
139     fsp.addFacetRequest(new AssociationFloatSumFacetRequest(afloat, 10));
140     fsp.addFacetRequest(new AssociationFloatSumFacetRequest(bfloat, 10));
141     
142     Query q = new MatchAllDocsQuery();
143
144     FacetsCollector fc = new FacetsCollector(fsp, reader, taxo);
145     
146     IndexSearcher searcher = newSearcher(reader);
147     searcher.search(q, fc);
148     List<FacetResult> res = fc.getFacetResults();
149     
150     assertNotNull("No results!",res);
151     assertEquals("Wrong number of results!",2, res.size());
152     assertEquals("Wrong count for category 'a'!",50f, (float) res.get(0).getFacetResultNode().getValue(), 0.00001);
153     assertEquals("Wrong count for category 'b'!",10f, (float) res.get(1).getFacetResultNode().getValue(), 0.00001);
154     
155     searcher.close();
156     taxo.close();
157   }  
158     
159   @Test
160   public void testDifferentAggregatorsSameCategoryList() throws Exception {
161     // Same category list cannot be aggregated by two different aggregators. If
162     // you want to do that, you need to separate the categories into two
163     // category list (you'll still have one association list).
164     LuceneTaxonomyReader taxo = new LuceneTaxonomyReader(taxoDir);
165
166     // facet requests for two facets
167     FacetSearchParams fsp = new FacetSearchParams();
168     fsp.addFacetRequest(new AssociationIntSumFacetRequest(aint, 10));
169     fsp.addFacetRequest(new AssociationIntSumFacetRequest(bint, 10));
170     fsp.addFacetRequest(new AssociationFloatSumFacetRequest(afloat, 10));
171     fsp.addFacetRequest(new AssociationFloatSumFacetRequest(bfloat, 10));
172     
173     Query q = new MatchAllDocsQuery();
174
175     FacetsCollector fc = new FacetsCollector(fsp, reader, taxo);
176     
177     IndexSearcher searcher = newSearcher(reader);
178     searcher.search(q, fc);
179     try {
180       fc.getFacetResults();
181       fail("different aggregators for same category list should not be supported");
182     } catch (RuntimeException e) {
183       // ok - expected
184     }
185     searcher.close();
186     taxo.close();
187   }  
188
189 }