~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/facet/src/examples/org/apache/lucene/facet/example/association/AssociationIndexer.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.facet.example.association;
2
 
 
3
 
import org.apache.lucene.document.Document;
4
 
import org.apache.lucene.document.Field;
5
 
import org.apache.lucene.document.Field.Index;
6
 
import org.apache.lucene.document.Field.Store;
7
 
import org.apache.lucene.index.IndexWriter;
8
 
import org.apache.lucene.index.IndexWriterConfig;
9
 
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
10
 
import org.apache.lucene.store.Directory;
11
 
 
12
 
import org.apache.lucene.facet.enhancements.EnhancementsDocumentBuilder;
13
 
import org.apache.lucene.facet.enhancements.association.AssociationProperty;
14
 
import org.apache.lucene.facet.example.ExampleUtils;
15
 
import org.apache.lucene.facet.example.simple.SimpleUtils;
16
 
import org.apache.lucene.facet.index.CategoryContainer;
17
 
import org.apache.lucene.facet.index.CategoryDocumentBuilder;
18
 
import org.apache.lucene.facet.taxonomy.CategoryPath;
19
 
import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
20
 
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
21
 
 
22
 
/**
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
29
 
 *
30
 
 *     http://www.apache.org/licenses/LICENSE-2.0
31
 
 *
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.
37
 
 */
38
 
 
39
 
/**
40
 
 * Sample indexer creates an index, and adds to it sample documents with
41
 
 * categories, which can be simple or contain associations.
42
 
 * 
43
 
 * @lucene.experimental
44
 
 */
45
 
public class AssociationIndexer {
46
 
 
47
 
  /**
48
 
   * Create an index, and adds to it sample documents and categories.
49
 
   * 
50
 
   * @param indexDir
51
 
   *            Directory in which the index should be created.
52
 
   * @param taxoDir
53
 
   *            Directory in which the taxonomy index should be created.
54
 
   * @throws Exception
55
 
   *             on error (no detailed exception handling here for sample
56
 
   *             simplicity
57
 
   */
58
 
  public static void index(Directory indexDir, Directory taxoDir) throws Exception {
59
 
 
60
 
    // create and open an index writer
61
 
    IndexWriter iw = new IndexWriter(indexDir, new IndexWriterConfig(ExampleUtils.EXAMPLE_VER, SimpleUtils.analyzer));
62
 
 
63
 
    // create and open a taxonomy writer
64
 
    TaxonomyWriter taxo = new DirectoryTaxonomyWriter(taxoDir, OpenMode.CREATE);
65
 
 
66
 
    // loop over sample documents
67
 
    int nDocsAdded = 0;
68
 
    int nFacetsAdded = 0;
69
 
    for (int docNum = 0; docNum < SimpleUtils.docTexts.length; docNum++) {
70
 
      ExampleUtils.log(" ++++ DOC ID: " + docNum);
71
 
      // obtain the sample categories for current document
72
 
      CategoryContainer categoryContainer = new CategoryContainer();
73
 
      for (CategoryPath path : SimpleUtils.categories[docNum]) {
74
 
        categoryContainer.addCategory(path);
75
 
        ExampleUtils.log("\t ++++ PATH: " + path);
76
 
      }
77
 
      // and also those with associations
78
 
      CategoryPath[] associationsPaths = AssociationUtils.categories[docNum];
79
 
      AssociationProperty[] associationProps = AssociationUtils.associations[docNum];
80
 
      for (int i = 0; i < associationsPaths.length; i++) {
81
 
        categoryContainer.addCategory(associationsPaths[i], associationProps[i]);
82
 
        ExampleUtils.log("\t $$$$ Association: ("
83
 
            + associationsPaths[i] + "," + associationProps[i]
84
 
            + ")");
85
 
      }
86
 
 
87
 
      // we do not alter indexing parameters!
88
 
      // a category document builder will add the categories to a document
89
 
      // once build() is called
90
 
      CategoryDocumentBuilder categoryDocBuilder = new EnhancementsDocumentBuilder(
91
 
          taxo, AssociationUtils.assocIndexingParams);
92
 
      categoryDocBuilder.setCategories(categoryContainer);
93
 
 
94
 
      // create a plain Lucene document and add some regular Lucene fields
95
 
      // to it
96
 
      Document doc = new Document();
97
 
      doc.add(new Field(SimpleUtils.TITLE, SimpleUtils.docTitles[docNum],
98
 
          Store.YES, Index.ANALYZED));
99
 
      doc.add(new Field(SimpleUtils.TEXT, SimpleUtils.docTexts[docNum],
100
 
          Store.NO, Index.ANALYZED));
101
 
 
102
 
      // invoke the category document builder for adding categories to the
103
 
      // document and,
104
 
      // as required, to the taxonomy index
105
 
      categoryDocBuilder.build(doc);
106
 
 
107
 
      // finally add the document to the index
108
 
      iw.addDocument(doc);
109
 
 
110
 
      nDocsAdded++;
111
 
      nFacetsAdded += categoryContainer.size();
112
 
    }
113
 
 
114
 
    // commit changes.
115
 
    // we commit changes to the taxonomy index prior to committing them to
116
 
    // the search index.
117
 
    // this is important, so that all facets referred to by documents in the
118
 
    // search index
119
 
    // will indeed exist in the taxonomy index.
120
 
    taxo.commit();
121
 
    iw.commit();
122
 
 
123
 
    // close the taxonomy index and the index - all modifications are
124
 
    // now safely in the provided directories: indexDir and taxoDir.
125
 
    taxo.close();
126
 
    iw.close();
127
 
 
128
 
    ExampleUtils.log("Indexed " + nDocsAdded + " documents with overall "
129
 
        + nFacetsAdded + " facets.");
130
 
  }
131
 
  
132
 
}