1 package org.apache.lucene.facet.index;
3 import java.io.IOException;
4 import java.util.ArrayList;
7 import org.apache.lucene.analysis.MockAnalyzer;
8 import org.apache.lucene.analysis.MockTokenizer;
9 import org.apache.lucene.document.Document;
10 import org.apache.lucene.index.IndexReader;
11 import org.apache.lucene.index.IndexWriterConfig;
12 import org.apache.lucene.index.RandomIndexWriter;
13 import org.apache.lucene.search.IndexSearcher;
14 import org.apache.lucene.search.MatchAllDocsQuery;
15 import org.apache.lucene.store.Directory;
16 import org.junit.Test;
18 import org.apache.lucene.util.LuceneTestCase;
19 import org.apache.lucene.facet.example.merge.TaxonomyMergeUtils;
20 import org.apache.lucene.facet.search.FacetsCollector;
21 import org.apache.lucene.facet.search.params.CountFacetRequest;
22 import org.apache.lucene.facet.search.params.FacetSearchParams;
23 import org.apache.lucene.facet.search.results.FacetResult;
24 import org.apache.lucene.facet.search.results.FacetResultNode;
25 import org.apache.lucene.facet.taxonomy.CategoryPath;
26 import org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyReader;
27 import org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter;
30 * Licensed to the Apache Software Foundation (ASF) under one or more
31 * contributor license agreements. See the NOTICE file distributed with
32 * this work for additional information regarding copyright ownership.
33 * The ASF licenses this file to You under the Apache License, Version 2.0
34 * (the "License"); you may not use this file except in compliance with
35 * the License. You may obtain a copy of the License at
37 * http://www.apache.org/licenses/LICENSE-2.0
39 * Unless required by applicable law or agreed to in writing, software
40 * distributed under the License is distributed on an "AS IS" BASIS,
41 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42 * See the License for the specific language governing permissions and
43 * limitations under the License.
46 public class FacetsPayloadProcessorProviderTest extends LuceneTestCase {
48 private static final int NUM_DOCS = 100;
51 public void testTaxonomyMergeUtils() throws Exception {
52 Directory dir = newDirectory();
53 Directory taxDir = newDirectory();
54 buildIndexWithFacets(dir, taxDir, true);
56 Directory dir1 = newDirectory();
57 Directory taxDir1 = newDirectory();
58 buildIndexWithFacets(dir1, taxDir1, false);
60 TaxonomyMergeUtils.merge(dir, taxDir, dir1, taxDir1);
62 verifyResults(dir1, taxDir1);
69 private void verifyResults(Directory dir, Directory taxDir) throws IOException {
70 IndexReader reader1 = IndexReader.open(dir);
71 LuceneTaxonomyReader taxReader = new LuceneTaxonomyReader(taxDir);
72 IndexSearcher searcher = newSearcher(reader1);
73 FacetSearchParams fsp = new FacetSearchParams();
74 fsp.addFacetRequest(new CountFacetRequest(new CategoryPath("tag"), NUM_DOCS));
75 FacetsCollector collector = new FacetsCollector(fsp, reader1, taxReader);
76 searcher.search(new MatchAllDocsQuery(), collector);
77 FacetResult result = collector.getFacetResults().get(0);
78 FacetResultNode node = result.getFacetResultNode();
79 for (FacetResultNode facet: node.getSubResults()) {
80 int weight = (int)facet.getValue();
81 int label = Integer.parseInt(facet.getLabel().getComponent(1));
82 //System.out.println(label + ": " + weight);
84 System.out.println(label + ": " + weight);
86 assertEquals(NUM_DOCS ,weight);
92 private void buildIndexWithFacets(Directory dir, Directory taxDir, boolean asc) throws IOException {
93 IndexWriterConfig config = newIndexWriterConfig(TEST_VERSION_CURRENT,
94 new MockAnalyzer(random, MockTokenizer.WHITESPACE, false));
95 RandomIndexWriter writer = new RandomIndexWriter(random, dir, config);
97 LuceneTaxonomyWriter taxonomyWriter = new LuceneTaxonomyWriter(taxDir);
98 for (int i = 1; i <= NUM_DOCS; i++) {
99 Document doc = new Document();
100 List<CategoryPath> categoryPaths = new ArrayList<CategoryPath>(i + 1);
101 for (int j = i; j <= NUM_DOCS; j++) {
102 int facetValue = asc? j: NUM_DOCS - j;
103 categoryPaths.add(new CategoryPath("tag", Integer.toString(facetValue)));
105 CategoryDocumentBuilder catBuilder = new CategoryDocumentBuilder(taxonomyWriter);
106 catBuilder.setCategoryPaths(categoryPaths);
107 catBuilder.build(doc);
108 writer.addDocument(doc);
110 taxonomyWriter.close();