001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.update;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.List;
021:
022: import org.apache.lucene.document.Document;
023: import org.apache.lucene.document.Field;
024: import org.apache.solr.core.SolrException;
025: import org.apache.solr.schema.IndexSchema;
026: import org.apache.solr.schema.SchemaField;
027:
028: /**
029: * @author yonik
030: * @version $Id: DocumentBuilder.java 542679 2007-05-29 22:28:21Z ryan $
031: */
032:
033: // Not thread safe - by design. Create a new builder for each thread.
034: public class DocumentBuilder {
035: private final IndexSchema schema;
036: private Document doc;
037: private HashMap<String, String> map = new HashMap<String, String>();
038:
039: public DocumentBuilder(IndexSchema schema) {
040: this .schema = schema;
041: }
042:
043: public void startDoc() {
044: doc = new Document();
045: map.clear();
046: }
047:
048: protected void addSingleField(SchemaField sfield, String val,
049: float boost) {
050: //System.out.println("###################ADDING FIELD "+sfield+"="+val);
051:
052: // we don't check for a null val ourselves because a solr.FieldType
053: // might actually want to map it to something. If createField()
054: // returns null, then we don't store the field.
055: Field field = sfield.createField(val, boost);
056: if (field != null) {
057: if (!sfield.multiValued()) {
058: String oldValue = map.put(sfield.getName(), val);
059: if (oldValue != null) {
060: throw new SolrException(
061: SolrException.ErrorCode.BAD_REQUEST,
062: "ERROR: multiple values encountered for non multiValued field "
063: + sfield.getName() + ": first='"
064: + oldValue + "' second='" + val
065: + "'");
066: }
067: }
068: // field.setBoost(boost);
069: doc.add(field);
070: }
071: }
072:
073: public void addField(SchemaField sfield, String val, float boost) {
074: addSingleField(sfield, val, boost);
075: }
076:
077: public void addField(String name, String val) {
078: addField(name, val, 1.0f);
079: }
080:
081: public void addField(String name, String val, float boost) {
082: SchemaField sfield = schema.getFieldOrNull(name);
083: if (sfield != null) {
084: addField(sfield, val, boost);
085: }
086:
087: // Check if we should copy this field to any other fields.
088: // This could happen whether it is explicit or not.
089: SchemaField[] destArr = schema.getCopyFields(name);
090: if (destArr != null) {
091: for (SchemaField destField : destArr) {
092: addSingleField(destField, val, boost);
093: }
094: }
095:
096: // error if this field name doesn't match anything
097: if (sfield == null && (destArr == null || destArr.length == 0)) {
098: throw new SolrException(
099: SolrException.ErrorCode.BAD_REQUEST,
100: "ERROR:unknown field '" + name + "'");
101: }
102: }
103:
104: public void setBoost(float boost) {
105: doc.setBoost(boost);
106: }
107:
108: public void endDoc() {
109: }
110:
111: // specific to this type of document builder
112: public Document getDoc() throws IllegalArgumentException {
113:
114: // Check for all required fields -- Note, all fields with a
115: // default value are defacto 'required' fields.
116: List<String> missingFields = new ArrayList<String>(schema
117: .getRequiredFields().size());
118: for (SchemaField field : schema.getRequiredFields()) {
119: if (doc.getField(field.getName()) == null) {
120: if (field.getDefaultValue() != null) {
121: doc.add(field.createField(field.getDefaultValue(),
122: 1.0f));
123: } else {
124: missingFields.add(field.getName());
125: }
126: }
127: }
128:
129: if (missingFields.size() > 0) {
130: StringBuilder builder = new StringBuilder();
131: // add the uniqueKey if possible
132: if (schema.getUniqueKeyField() != null) {
133: String n = schema.getUniqueKeyField().getName();
134: String v = doc.get(n);
135: builder.append("Document [" + n + "=" + v + "] ");
136: }
137: builder.append("missing required fields: ");
138: for (String field : missingFields) {
139: builder.append(field);
140: builder.append(" ");
141: }
142: throw new SolrException(
143: SolrException.ErrorCode.BAD_REQUEST, builder
144: .toString());
145: }
146:
147: Document ret = doc;
148: doc = null;
149: return ret;
150: }
151: }
|