1
package org.apache.solr.schema;
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
10
* http://www.apache.org/licenses/LICENSE-2.0
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.
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;
25
import java.util.HashMap;
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.
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
46
public FieldType getSubType() {
51
protected void init(IndexSchema schema, Map<String, String> args) {
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);
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);
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.");
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.
77
* @param schema the IndexSchema
78
* @param type The {@link FieldType} of the prototype.
79
* @return The {@link SchemaField}
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,
92
schema.registerDynamicField(proto);
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();
105
* Throws UnsupportedOperationException()
108
public Query getFieldQuery(QParser parser, SchemaField field, String externalVal) {
109
throw new UnsupportedOperationException();
112
protected void createSuffixCache(int size) {
113
suffixes = new String[size];
114
for (int i=0; i<size; i++) {
115
suffixes[i] = "_" + i + suffix;
119
protected SchemaField subField(SchemaField base, int i) {
120
return schema.getField(base.getName() + suffixes[i]);