All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.solr.common.SolrInputDocument Maven / Gradle / Ivy

There is a newer version: 9.5.0
Show newest version
/*
 * 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.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;

/**
 * Represent the field and boost 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 float _documentBoost = 1.0f;
  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]);
    }
  }
  
  public SolrInputDocument(Map fields) {
    _fields = fields;
  }
  
  /**
   * Remove all fields and boosts from the document
   */
  @Override
  public void clear()
  {
    if( _fields != null ) {
      _fields.clear();      
    }
    _childDocuments = null;
  }

  ///////////////////////////////////////////////////////////////////
  // Add / Set fields
  ///////////////////////////////////////////////////////////////////

  /** 
   * Add a field with implied null value for boost.
   * 
   * 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. 
   * @see #addField(String, Object, float)
   */
  public void addField(String name, Object value) 
  {
    addField(name, value, 1.0f );
  }
  
  /** 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 getFieldValues(String name) 
  {
    SolrInputField field = getField(name);
    if (field!=null) {
      return field.getValues();
    }
    return null;
  } 
  
  /** Get all field names.
   * 
   * @return Set of all field names.
   */
  @Override
  public Collection getFieldNames() 
  {
    return _fields.keySet();
  }
  
  /** Set a field with implied null value for boost.
   * 
   * @see #setField(String, Object, float)
   * @param name name of the field to set
   * @param value value of the field
   */
  public void setField(String name, Object value) 
  {
    setField(name, value, 1.0f );
  }
  
  public void setField(String name, Object value, float boost ) 
  {
    SolrInputField field = new SolrInputField( name );
    _fields.put( name, field );
    field.setValue( value, boost );
  }

  /**
   * Adds a field with the given name, value and boost.  If a field with the
   * name already exists, then the given value is appended to the value of that
   * field, with the new boost. If the value is a collection, then each of its
   * values will be added to the field.
   *
   * 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. 
   * @param boost Boost value for the field
   */
  public void addField(String name, Object value, float boost ) 
  {
    SolrInputField field = _fields.get( name );
    if( field == null || field.value == null ) {
      setField(name, value, boost);
    }
    else {
      field.addValue( value, boost );
    }
  }

  /**
   * Remove a field from the document
   * 
   * @param name The field name whose field is to be removed from the document
   * @return the previous field with name, or
   *         null if there was no field for key.
   */
  public SolrInputField removeField(String name) {
    return _fields.remove( name );
  }

  ///////////////////////////////////////////////////////////////////
  // Get the field values
  ///////////////////////////////////////////////////////////////////

  public SolrInputField getField( String field )
  {
    return _fields.get( field );
  }

  @Override
  public Iterator iterator() {
    return _fields.values().iterator();
  }
  
  public float getDocumentBoost() {
    return _documentBoost;
  }

  public void setDocumentBoost(float documentBoost) {
    _documentBoost = documentBoost;
  }
  
  @Override
  public String toString()
  {
    return "SolrInputDocument(fields: " + _fields.values()
        + ( _childDocuments == null ? "" : (", children: " + _childDocuments) )
        + ")";
  }
  
  public SolrInputDocument deepCopy() {
    SolrInputDocument clone = new SolrInputDocument();
    Set> entries = _fields.entrySet();
    for (Map.Entry fieldEntry : entries) {
      clone._fields.put(fieldEntry.getKey(), fieldEntry.getValue().deepCopy());
    }
    clone._documentBoost = _documentBoost;

    if (_childDocuments != null) {
      clone._childDocuments = new ArrayList<>(_childDocuments.size());
      for (SolrInputDocument child : _childDocuments) {
        clone._childDocuments.add(child.deepCopy());
      }
    }
    
    return clone;
  }

  //---------------------------------------------------
  // MAP interface
  //---------------------------------------------------

  @Override
  public boolean containsKey(Object key) {
    return _fields.containsKey(key);
  }

  @Override
  public boolean containsValue(Object value) {
    return _fields.containsValue(value);
  }

  @Override
  public Set> entrySet() {
    return _fields.entrySet();
  }

  @Override
  public SolrInputField get(Object key) {
    return _fields.get(key);
  }

  @Override
  public boolean isEmpty() {
    return _fields.isEmpty();
  }

  @Override
  public Set keySet() {
    return _fields.keySet();
  }

  @Override
  public SolrInputField put(String key, SolrInputField value) {
    return _fields.put(key, value);
  }

  @Override
  public void putAll(Map t) {
    _fields.putAll( t );
  }

  @Override
  public SolrInputField remove(Object key) {
    return _fields.remove(key);
  }

  @Override
  public int size() {
    return _fields.size();
  }

  @Override
  public Collection values() {
    return _fields.values();
  }

  @Override
  public void addChildDocument(SolrInputDocument child) {
   if (_childDocuments == null) {
     _childDocuments = new ArrayList<>();
   }
    _childDocuments.add(child);
  }
  
  public void addChildDocuments(Collection children) {
    for (SolrInputDocument child : children) {
      addChildDocument(child);
    }
  }

  /** Returns the list of child documents, or null if none. */
  public List getChildDocuments() {
    return _childDocuments;
  }
  
  public boolean hasChildDocuments() {
    boolean isEmpty = (_childDocuments == null || _childDocuments.isEmpty());
    return !isEmpty;
  }

  @Override
  public int getChildDocumentCount() {
    return hasChildDocuments() ? _childDocuments.size(): 0;
  }
}