1 package org.apache.lucene.facet.enhancements.association;
3 import org.apache.lucene.analysis.MockAnalyzer;
4 import org.apache.lucene.analysis.MockTokenizer;
5 import org.apache.lucene.document.Document;
6 import org.apache.lucene.index.IndexReader;
7 import org.apache.lucene.index.RandomIndexWriter;
8 import org.apache.lucene.store.Directory;
11 import org.apache.lucene.util.LuceneTestCase;
12 import org.apache.lucene.facet.enhancements.EnhancementsDocumentBuilder;
13 import org.apache.lucene.facet.enhancements.params.DefaultEnhancementsIndexingParams;
14 import org.apache.lucene.facet.enhancements.params.EnhancementsIndexingParams;
15 import org.apache.lucene.facet.index.CategoryContainer;
16 import org.apache.lucene.facet.index.attributes.CategoryAttributeImpl;
17 import org.apache.lucene.facet.index.attributes.CategoryProperty;
18 import org.apache.lucene.facet.taxonomy.CategoryPath;
19 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
20 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
23 * Licensed to the Apache Software Foundation (ASF) under one or more
24 * contributor license agreements. See the NOTICE file distributed with
25 * this work for additional information regarding copyright ownership.
26 * The ASF licenses this file to You under the Apache License, Version 2.0
27 * (the "License"); you may not use this file except in compliance with
28 * the License. You may obtain a copy of the License at
30 * http://www.apache.org/licenses/LICENSE-2.0
32 * Unless required by applicable law or agreed to in writing, software
33 * distributed under the License is distributed on an "AS IS" BASIS,
34 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35 * See the License for the specific language governing permissions and
36 * limitations under the License.
39 public class CustomAssociationPropertyTest extends LuceneTestCase {
42 public void testCustomProperty() throws Exception {
43 class CustomProperty extends AssociationIntProperty {
44 public CustomProperty(int value) {
48 public void merge(CategoryProperty other) {
49 throw new UnsupportedOperationException();
53 final int NUM_CATEGORIES = 10;
54 EnhancementsIndexingParams iParams = new DefaultEnhancementsIndexingParams(
55 new AssociationEnhancement());
57 Directory iDir = newDirectory();
58 Directory tDir = newDirectory();
60 RandomIndexWriter w = new RandomIndexWriter(random, iDir,
61 newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.KEYWORD, false)));
62 DirectoryTaxonomyWriter taxoW = new DirectoryTaxonomyWriter(tDir);
64 CategoryContainer cc = new CategoryContainer();
65 EnhancementsDocumentBuilder builder = new EnhancementsDocumentBuilder(taxoW, iParams);
66 for (int i = 1; i <= NUM_CATEGORIES; i++) {
67 CategoryAttributeImpl ca = new CategoryAttributeImpl(new CategoryPath(Integer.toString(i)));
68 ca.addProperty(new CustomProperty(i));
72 builder.setCategories(cc);
73 w.addDocument(builder.build(new Document()));
75 IndexReader reader = w.getReader();
78 DirectoryTaxonomyReader taxo = new DirectoryTaxonomyReader(tDir);
79 String field = iParams.getCategoryListParams(new CategoryPath("0")).getTerm().field();
80 AssociationsPayloadIterator api = new AssociationsPayloadIterator(reader, field);
85 for (int i = 1; i <= NUM_CATEGORIES; i++) {
86 int ordinal = taxo.getOrdinal(new CategoryPath(Integer.toString(i)));
88 long association = api.getAssociation(ordinal);
89 assertTrue("Association expected for ordinal "+ordinal+" but none was found",
90 association <= Integer.MAX_VALUE);
92 assertEquals("Wrong association value for category '"+ i+"'", i, (int)association);
95 assertTrue("No categories found for doc #0", flag);