You are here

public function ApacheSolrDocument::addField in Apache Solr Search 7

Same name and namespace in other branches
  1. 8 Apache_Solr_Document.php \ApacheSolrDocument::addField()
  2. 6.3 Apache_Solr_Document.php \ApacheSolrDocument::addField()

Add a value to a multi-valued field

NOTE: the solr XML format allows you to specify boosts PER value even though the underlying Lucene implementation only allows a boost per field. To remedy this, the final field boost value will be the product of all specified boosts on field values - this is similar to SolrJ's functionality.

@code $doc = new ApacheSolrDocument(); $doc->addField('foo', 'bar', 2.0); $doc->addField('foo', 'baz', 3.0); // resultant field boost will be 6! echo $doc->getFieldBoost('foo');

Parameters

string $key:

mixed $value:

mixed $boost: Use false for default boost, else cast to float that should be > 0 or will be treated as false

1 call to ApacheSolrDocument::addField()
ApacheSolrDocument::setMultiValue in ./Apache_Solr_Document.php
Handle the array manipulation for a multi-valued field

File

./Apache_Solr_Document.php, line 156

Class

ApacheSolrDocument
Holds Key / Value pairs that represent a Solr Document along with any associated boost values. Field values can be accessed by direct dereferencing such as:

Code

public function addField($key, $value, $boost = FALSE) {
  if (!isset($this->_fields[$key])) {

    // create holding array if this is the first value
    $this->_fields[$key] = array();
  }
  else {
    if (!is_array($this->_fields[$key])) {

      // move existing value into array if it is not already an array
      $this->_fields[$key] = array(
        $this->_fields[$key],
      );
    }
  }
  if ($this
    ->getFieldBoost($key) === FALSE) {

    // boost not already set, set it now
    $this
      ->setFieldBoost($key, $boost);
  }
  else {
    if ((double) $boost > 0.0) {

      // multiply passed boost with current field boost - similar to SolrJ implementation
      $this->_fieldBoosts[$key] *= (double) $boost;
    }
  }

  // add value to array
  $this->_fields[$key][] = $value;
}