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