1 package org.apache.lucene.facet.search.association;
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;
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;
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
43 * http://www.apache.org/licenses/LICENSE-2.0
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.
52 /** Test for associations */
53 public class AssociationsFacetRequestTest extends LuceneTestCase {
55 private static Directory dir;
56 private static IndexReader reader;
57 private static Directory taxoDir;
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");
65 public static void beforeClassAssociationsFacetRequestTest() throws Exception {
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)));
72 TaxonomyWriter taxoWriter = new LuceneTaxonomyWriter(taxoDir);
74 EnhancementsDocumentBuilder builder = new EnhancementsDocumentBuilder(
75 taxoWriter, new DefaultEnhancementsIndexingParams(
76 new AssociationEnhancement()));
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));
88 builder.setCategories(container).build(doc);
89 writer.addDocument(doc);
93 reader = writer.getReader();
98 public static void afterClassAssociationsFacetRequestTest() throws Exception {
108 public void testIntSumAssociation() throws Exception {
109 LuceneTaxonomyReader taxo = new LuceneTaxonomyReader(taxoDir);
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));
116 Query q = new MatchAllDocsQuery();
118 FacetsCollector fc = new FacetsCollector(fsp, reader, taxo);
120 IndexSearcher searcher = newSearcher(reader);
121 searcher.search(q, fc);
122 List<FacetResult> res = fc.getFacetResults();
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());
134 public void testFloatSumAssociation() throws Exception {
135 LuceneTaxonomyReader taxo = new LuceneTaxonomyReader(taxoDir);
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));
142 Query q = new MatchAllDocsQuery();
144 FacetsCollector fc = new FacetsCollector(fsp, reader, taxo);
146 IndexSearcher searcher = newSearcher(reader);
147 searcher.search(q, fc);
148 List<FacetResult> res = fc.getFacetResults();
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);
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);
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));
173 Query q = new MatchAllDocsQuery();
175 FacetsCollector fc = new FacetsCollector(fsp, reader, taxo);
177 IndexSearcher searcher = newSearcher(reader);
178 searcher.search(q, fc);
180 fc.getFacetResults();
181 fail("different aggregators for same category list should not be supported");
182 } catch (RuntimeException e) {