1 package org.apache.lucene.facet.search;
3 import java.io.IOException;
4 import java.util.ArrayList;
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.IndexWriterConfig;
15 import org.apache.lucene.index.RandomIndexWriter;
16 import org.apache.lucene.index.Term;
17 import org.apache.lucene.search.IndexSearcher;
18 import org.apache.lucene.search.Query;
19 import org.apache.lucene.search.TermQuery;
20 import org.apache.lucene.search.TopDocs;
21 import org.apache.lucene.store.Directory;
22 import org.apache.lucene.store.LockObtainFailedException;
23 import org.junit.AfterClass;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
27 import org.apache.lucene.util.LuceneTestCase;
28 import org.apache.lucene.facet.index.CategoryDocumentBuilder;
29 import org.apache.lucene.facet.index.params.CategoryListParams;
30 import org.apache.lucene.facet.index.params.PerDimensionIndexingParams;
31 import org.apache.lucene.facet.search.DrillDown;
32 import org.apache.lucene.facet.search.params.FacetSearchParams;
33 import org.apache.lucene.facet.taxonomy.CategoryPath;
34 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
35 import org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyReader;
36 import org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter;
39 * Licensed to the Apache Software Foundation (ASF) under one or more
40 * contributor license agreements. See the NOTICE file distributed with
41 * this work for additional information regarding copyright ownership.
42 * The ASF licenses this file to You under the Apache License, Version 2.0
43 * (the "License"); you may not use this file except in compliance with
44 * the License. You may obtain a copy of the License at
46 * http://www.apache.org/licenses/LICENSE-2.0
48 * Unless required by applicable law or agreed to in writing, software
49 * distributed under the License is distributed on an "AS IS" BASIS,
50 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
51 * See the License for the specific language governing permissions and
52 * limitations under the License.
55 public class DrillDownTest extends LuceneTestCase {
57 private FacetSearchParams defaultParams = new FacetSearchParams();
58 private FacetSearchParams nonDefaultParams;
59 private static IndexReader reader;
60 private static LuceneTaxonomyReader taxo;
61 private static Directory dir;
62 private static Directory taxoDir;
64 public DrillDownTest() throws IOException {
65 PerDimensionIndexingParams iParams = new PerDimensionIndexingParams();
66 CategoryListParams aClParams = new CategoryListParams(new Term("testing_facets_a", "a"));
67 CategoryListParams bClParams = new CategoryListParams(new Term("testing_facets_b", "b"));
69 iParams.addCategoryListParams(new CategoryPath("a"), aClParams);
70 iParams.addCategoryListParams(new CategoryPath("b"), bClParams);
72 nonDefaultParams = new FacetSearchParams(iParams);
75 public static void createIndexes() throws CorruptIndexException, LockObtainFailedException, IOException {
77 RandomIndexWriter writer = new RandomIndexWriter(random, dir,
78 newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.KEYWORD, false)));
80 taxoDir = newDirectory();
81 TaxonomyWriter taxoWriter = new LuceneTaxonomyWriter(taxoDir);
83 for (int i = 0; i < 100; i++) {
84 ArrayList<CategoryPath> paths = new ArrayList<CategoryPath>();
85 Document doc = new Document();
86 if (i % 2 == 0) { // 50
87 doc.add(new Field("content", "foo", Store.NO, Index.ANALYZED));
89 if (i % 3 == 0) { // 33
90 doc.add(new Field("content", "bar", Store.NO, Index.ANALYZED));
92 if (i % 4 == 0) { // 25
93 paths.add(new CategoryPath("a"));
95 if (i % 5 == 0) { // 20
96 paths.add(new CategoryPath("b"));
98 CategoryDocumentBuilder builder = new CategoryDocumentBuilder(taxoWriter);
99 builder.setCategoryPaths(paths).build(doc);
100 writer.addDocument(doc);
104 reader = writer.getReader();
107 taxo = new LuceneTaxonomyReader(taxoDir);
111 public void testTermNonDefault() {
112 Term termA = DrillDown.term(nonDefaultParams, new CategoryPath("a"));
113 assertEquals(new Term("testing_facets_a", "a"), termA);
115 Term termB = DrillDown.term(nonDefaultParams, new CategoryPath("b"));
116 assertEquals(new Term("testing_facets_b", "b"), termB);
120 public void testTermDefault() {
121 String defaultField = CategoryListParams.DEFAULT_TERM.field();
123 Term termA = DrillDown.term(defaultParams, new CategoryPath("a"));
124 assertEquals(new Term(defaultField, "a"), termA);
126 Term termB = DrillDown.term(defaultParams, new CategoryPath("b"));
127 assertEquals(new Term(defaultField, "b"), termB);
131 public void testQuery() throws IOException {
132 IndexSearcher searcher = newSearcher(reader);
134 // Making sure the query yields 25 documents with the facet "a"
135 Query q = DrillDown.query(defaultParams, new CategoryPath("a"));
136 TopDocs docs = searcher.search(q, 100);
137 assertEquals(25, docs.totalHits);
139 // Making sure the query yields 5 documents with the facet "b" and the
140 // previous (facet "a") query as a base query
141 Query q2 = DrillDown.query(defaultParams, q, new CategoryPath("b"));
142 docs = searcher.search(q2, 100);
143 assertEquals(5, docs.totalHits);
145 // Making sure that a query of both facet "a" and facet "b" yields 5 results
146 Query q3 = DrillDown.query(defaultParams, new CategoryPath("a"), new CategoryPath("b"));
147 docs = searcher.search(q3, 100);
148 assertEquals(5, docs.totalHits);
150 // Check that content:foo (which yields 50% results) and facet/b (which yields 20%)
151 // would gather together 10 results (10%..)
152 Query fooQuery = new TermQuery(new Term("content", "foo"));
153 Query q4 = DrillDown.query(defaultParams, fooQuery, new CategoryPath("b"));
154 docs = searcher.search(q4, 100);
155 assertEquals(10, docs.totalHits);
159 public void testQueryImplicitDefaultParams() throws IOException {
160 IndexSearcher searcher = newSearcher(reader);
162 // Create the base query to start with
163 Query q = DrillDown.query(defaultParams, new CategoryPath("a"));
165 // Making sure the query yields 5 documents with the facet "b" and the
166 // previous (facet "a") query as a base query
167 Query q2 = DrillDown.query(q, new CategoryPath("b"));
168 TopDocs docs = searcher.search(q2, 100);
169 assertEquals(5, docs.totalHits);
171 // Check that content:foo (which yields 50% results) and facet/b (which yields 20%)
172 // would gather together 10 results (10%..)
173 Query fooQuery = new TermQuery(new Term("content", "foo"));
174 Query q4 = DrillDown.query(fooQuery, new CategoryPath("b"));
175 docs = searcher.search(q4, 100);
176 assertEquals(10, docs.totalHits);
180 public static void closeIndexes() throws IOException {
181 if (reader != null) {