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

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/schema/AbstractSubTypeFieldType.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.solr.schema;
2
 
/**
3
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
4
 
 * contributor license agreements.  See the NOTICE file distributed with
5
 
 * this work for additional information regarding copyright ownership.
6
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
7
 
 * (the "License"); you may not use this file except in compliance with
8
 
 * 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, software
13
 
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 
 * See the License for the specific language governing permissions and
16
 
 * limitations under the License.
17
 
 */
18
 
 
19
 
import org.apache.solr.common.SolrException;
20
 
import org.apache.solr.common.params.MapSolrParams;
21
 
import org.apache.solr.common.params.SolrParams;
22
 
import org.apache.solr.search.QParser;
23
 
import org.apache.lucene.search.Query;
24
 
 
25
 
import java.util.HashMap;
26
 
import java.util.Map;
27
 
 
28
 
 
29
 
/**
30
 
 * An abstract base class for FieldTypes that delegate work to another {@link org.apache.solr.schema.FieldType}.
31
 
 * The sub type can be obtained by either specifying the subFieldType attribute or the subFieldSuffix.  In the former
32
 
 * case, a new dynamic field will be injected into the schema automatically with the name of {@link #POLY_FIELD_SEPARATOR}.
33
 
 * In the latter case, it will use an existing dynamic field definition to get the type.  See the example schema and the
34
 
 * use of the {@link org.apache.solr.schema.PointType} for more details.
35
 
 *
36
 
 **/
37
 
public abstract class AbstractSubTypeFieldType extends FieldType implements SchemaAware {
38
 
  protected FieldType subType;
39
 
  public static final String SUB_FIELD_SUFFIX = "subFieldSuffix";
40
 
  public static final String SUB_FIELD_TYPE = "subFieldType";
41
 
  protected String suffix;
42
 
  protected int dynFieldProps;
43
 
  protected String[] suffixes;
44
 
  protected IndexSchema schema;   // needed for retrieving SchemaFields
45
 
 
46
 
  public FieldType getSubType() {
47
 
    return subType;
48
 
  }
49
 
 
50
 
  @Override
51
 
  protected void init(IndexSchema schema, Map<String, String> args) {
52
 
    this.schema = schema;
53
 
    //it's not a first class citizen for the IndexSchema
54
 
    SolrParams p = new MapSolrParams(args);
55
 
    String subFT = p.get(SUB_FIELD_TYPE);
56
 
    String subSuffix = p.get(SUB_FIELD_SUFFIX);
57
 
    if (subFT != null) {
58
 
      args.remove(SUB_FIELD_TYPE);
59
 
      subType = schema.getFieldTypeByName(subFT.trim());
60
 
      suffix = POLY_FIELD_SEPARATOR + subType.typeName;
61
 
    } else if (subSuffix != null) {
62
 
      args.remove(SUB_FIELD_SUFFIX);
63
 
      suffix = subSuffix;
64
 
    } else {
65
 
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "The field type: " + typeName
66
 
              + " must specify the " +
67
 
              SUB_FIELD_TYPE + " attribute or the " + SUB_FIELD_SUFFIX + " attribute.");
68
 
    }
69
 
 
70
 
  }
71
 
 
72
 
  /**
73
 
   * Helper method for creating a dynamic field SchemaField prototype.  Returns a {@link SchemaField} with
74
 
   * the {@link FieldType} given and a name of "*" + {@link FieldType#POLY_FIELD_SEPARATOR} + {@link FieldType#typeName}
75
 
   * and props of indexed=true, stored=false.
76
 
   *
77
 
   * @param schema the IndexSchema
78
 
   * @param type   The {@link FieldType} of the prototype.
79
 
   * @return The {@link SchemaField}
80
 
   */
81
 
 
82
 
  static SchemaField registerPolyFieldDynamicPrototype(IndexSchema schema, FieldType type) {
83
 
    String name = "*" + FieldType.POLY_FIELD_SEPARATOR + type.typeName;
84
 
    Map<String, String> props = new HashMap<String, String>();
85
 
    //Just set these, delegate everything else to the field type
86
 
    props.put("indexed", "true");
87
 
    props.put("stored", "false");
88
 
    props.put("multiValued", "false");
89
 
    int p = SchemaField.calcProps(name, type, props);
90
 
    SchemaField proto = SchemaField.create(name,
91
 
            type, p, null);
92
 
    schema.registerDynamicField(proto);
93
 
    return proto;
94
 
  }
95
 
 
96
 
  public void inform(IndexSchema schema) {
97
 
    //Can't do this until here b/c the Dynamic Fields are not initialized until here.
98
 
    if (subType != null) {
99
 
      SchemaField proto = registerPolyFieldDynamicPrototype(schema, subType);
100
 
      dynFieldProps = proto.getProperties();
101
 
    }
102
 
  }
103
 
 
104
 
  /**
105
 
   * Throws UnsupportedOperationException()
106
 
   */
107
 
  @Override
108
 
  public Query getFieldQuery(QParser parser, SchemaField field, String externalVal) {
109
 
    throw new UnsupportedOperationException();
110
 
  }
111
 
 
112
 
  protected void createSuffixCache(int size) {
113
 
    suffixes = new String[size];
114
 
    for (int i=0; i<size; i++) {
115
 
      suffixes[i] = "_" + i + suffix;
116
 
    }
117
 
  }
118
 
 
119
 
  protected SchemaField subField(SchemaField base, int i) {
120
 
    return schema.getField(base.getName() + suffixes[i]);
121
 
  }
122
 
}