org.apache.solr.common.SolrInputDocument Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.common;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import org.apache.solr.common.params.CommonParams;
/**
* Represent the field-value information needed to construct and index
* a Lucene Document. Like the SolrDocument, the field values should
* match those specified in schema.xml
*
*
* @since solr 1.3
*/
public class SolrInputDocument extends SolrDocumentBase implements Iterable
{
private final Map _fields;
private List _childDocuments;
public SolrInputDocument(String... fields) {
_fields = new LinkedHashMap<>();
assert fields.length % 2 == 0;
for (int i = 0; i < fields.length; i += 2) {
addField(fields[i], fields[i + 1]);
}
}
@Override
public void writeMap(EntryWriter ew) throws IOException {
BiConsumer bc = ew.getBiConsumer();
BiConsumer wrapper = (k, o) -> {
if (o instanceof SolrInputField) {
o = ((SolrInputField) o).getValue();
}
bc.accept(k, o);
};
_fields.forEach(wrapper);
if (_childDocuments != null) {
ew.put(CommonParams.CHILDDOC, _childDocuments);
}
}
public SolrInputDocument(Map fields) {
_fields = fields;
}
/**
* Remove all fields from the document
*/
@Override
public void clear()
{
if( _fields != null ) {
_fields.clear();
}
_childDocuments = null;
}
///////////////////////////////////////////////////////////////////
// Add / Set fields
///////////////////////////////////////////////////////////////////
/**
* Add a field value to any existing values that may or may not exist.
*
* The class type of value and the name parameter should match schema.xml.
* schema.xml can be found in conf directory under the solr home by default.
*
* @param name Name of the field, should match one of the field names defined under "fields" tag in schema.xml.
* @param value Value of the field, should be of same class type as defined by "type" attribute of the corresponding field in schema.xml.
*/
public void addField(String name, Object value)
{
SolrInputField field = _fields.get( name );
if( field == null || field.value == null ) {
setField(name, value);
}
else {
field.addValue( value );
}
}
/** Get the first value for a field.
*
* @param name name of the field to fetch
* @return first value of the field or null if not present
*/
@Override
public Object getFieldValue(String name)
{
SolrInputField field = getField(name);
Object o = null;
if (field!=null) o = field.getFirstValue();
return o;
}
/** Get all the values for a field.
*
* @param name name of the field to fetch
* @return value of the field or null if not set
*/
@Override
public Collection
© 2015 - 2025 Weber Informatics LLC | Privacy Policy