You are here

public function SearchApiSolrDocument::addField in Search API Solr 7

Adds 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.

$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: The name of the field.

$value: The value to add for the field.

float|false $boost: FALSE for default boost, or a positive number for setting a field boost.

File

includes/document.inc, line 177

Class

SearchApiSolrDocument
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;
}