View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.index;
20  
21  import org.apache.lucene.document.Field;
22  import org.apache.lucene.document.FieldType;
23  import org.apache.lucene.index.IndexOptions;
24  
25  /**
26   * Holds basic information about Indexer field, how it is stored. To keep this centralized, and not spread across code.
27   * Since Lucene 2.x, the field names are encoded, so please use real chatty names instead of cryptic chars!
28   *
29   * @author cstamas
30   */
31  public class IndexerField {
32      private final org.apache.maven.index.Field ontology;
33  
34      private final IndexerFieldVersion version;
35  
36      private final String key;
37  
38      private final FieldType fieldType;
39  
40      /** Indexed, not tokenized, not stored. */
41      public static final FieldType KEYWORD_NOT_STORED = new FieldType();
42  
43      /** Indexed, not tokenized, stored. */
44      public static final FieldType KEYWORD_STORED = new FieldType();
45  
46      /** Indexed, tokenized, not stored. */
47      public static final FieldType ANALYZED_NOT_STORED = new FieldType();
48  
49      /** Indexed, tokenized, stored. */
50      public static final FieldType ANALYZED_STORED = new FieldType();
51  
52      static {
53          KEYWORD_NOT_STORED.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
54          KEYWORD_NOT_STORED.setStored(false);
55          KEYWORD_NOT_STORED.setTokenized(false);
56          KEYWORD_NOT_STORED.freeze();
57  
58          KEYWORD_STORED.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
59          KEYWORD_STORED.setStored(true);
60          KEYWORD_STORED.setTokenized(false);
61          KEYWORD_STORED.freeze();
62  
63          ANALYZED_NOT_STORED.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
64          ANALYZED_NOT_STORED.setStored(false);
65          ANALYZED_NOT_STORED.setTokenized(true);
66          ANALYZED_NOT_STORED.freeze();
67  
68          ANALYZED_STORED.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
69          ANALYZED_STORED.setStored(true);
70          ANALYZED_STORED.setTokenized(true);
71          ANALYZED_STORED.freeze();
72      }
73  
74      public IndexerField(
75              final org.apache.maven.index.Field ontology,
76              final IndexerFieldVersion version,
77              final String key,
78              final String description,
79              final FieldType fieldType) {
80          this.ontology = ontology;
81  
82          this.version = version;
83  
84          this.key = key;
85  
86          this.fieldType = fieldType;
87  
88          ontology.addIndexerField(this);
89      }
90  
91      public org.apache.maven.index.Field getOntology() {
92          return ontology;
93      }
94  
95      public IndexerFieldVersion getVersion() {
96          return version;
97      }
98  
99      public String getKey() {
100         return key;
101     }
102 
103     public FieldType getFieldType() {
104         return fieldType;
105     }
106 
107     public boolean isIndexed() {
108         return fieldType.indexOptions() != IndexOptions.NONE;
109     }
110 
111     public boolean isKeyword() {
112         return isIndexed() && !fieldType.tokenized();
113     }
114 
115     public boolean isStored() {
116         return fieldType.stored();
117     }
118 
119     public Field toField(String value) {
120         return new Field(getKey(), value, getFieldType());
121     }
122 }