* @license http://github.com/basdenooijer/solarium/raw/master/COPYING * @link http://www.solarium-project.org/ * * @package Solarium * @subpackage Document */ /** * Read/Write Solr document * * This document type is used for update queries. It has all of the features of * the readonly document and it also allows for updating or adding fields and * boosts. * * While it is possible to use this document type for a select, alter it and use * it in an update query (effectively the 'edit' that Solr doesn't have) this * is not recommended. Most Solr indexes have fields that are indexed and not * stored. You will loose that data because it is impossible to retrieve it from * Solr. Always update from the original data source. * * @package Solarium * @subpackage Document */ class Solarium_Document_ReadWrite extends Solarium_Document_ReadOnly { /** * Document boost value * * @var float */ protected $_boost = null; /** * Field boosts * * Using fieldname as the key and the boost as the value * * @var array */ protected $_fieldBoosts; /** * Constructor * * @param array $fields * @param array $boosts */ public function __construct($fields = array(), $boosts = array()) { $this->_fields = $fields; $this->_fieldBoosts = $boosts; } /** * Add a field value * * If a field already has a value it will be converted * to a multivalue field. * * @param string $key * @param mixed $value * @param float $boost * @return Solarium_Document_ReadWrite Provides fluent interface */ public function addField($key, $value, $boost = null) { if (!isset($this->_fields[$key])) { $this->setField($key, $value, $boost); } else { // convert single value to array if needed if (!is_array($this->_fields[$key])) { $this->_fields[$key] = array($this->_fields[$key]); } $this->_fields[$key][] = $value; $this->setFieldBoost($key, $boost); } return $this; } /** * Set a field value * * If a field already has a value it will be overwritten. You cannot use * this method for a multivalue field. * If you supply NULL as the value the field will be removed * * @param string $key * @param mixed $value * @param float $boost * @return Solarium_Document_ReadWrite Provides fluent interface */ public function setField($key, $value, $boost = null) { if ($value === null) { $this->removeField($key); } else { $this->_fields[$key] = $value; $this->setFieldBoost($key, $boost); } return $this; } /** * Remove a field * * @param string $key * @return Solarium_Document_ReadWrite Provides fluent interface */ public function removeField($key) { if (isset($this->_fields[$key])) { unset($this->_fields[$key]); } if (isset($this->_fieldBoosts[$key])) { unset($this->_fieldBoosts[$key]); } return $this; } /** * Get the boost value for a field * * @param string $key * @return float */ public function getFieldBoost($key) { if (isset($this->_fieldBoosts[$key])) { return $this->_fieldBoosts[$key]; } else { return null; } } /** * Set the boost value for a field * * @param string $key * @param float $boost * @return Solarium_Document_ReadWrite Provides fluent interface */ public function setFieldBoost($key, $boost) { $this->_fieldBoosts[$key] = $boost; return $this; } /** * Set the document boost value * * @param float $boost * @return Solarium_Document_ReadWrite Provides fluent interface */ public function setBoost($boost) { $this->_boost = $boost; return $this; } /** * Get the document boost value * * @return float */ public function getBoost() { return $this->_boost; } /** * Clear all fields * * @return Solarium_Document_ReadWrite Provides fluent interface **/ public function clear() { $this->_fields = array(); $this->_fieldBoosts = array(); return $this; } /** * Set field value * * Magic method for setting fields as properties of this document * object, by field name. * * If you supply NULL as the value the field will be removed * If you supply an array a multivalue field will be created. * In all cases any existing (multi)value will be overwritten. * * @param string $name * @param string|null $value * @return void */ public function __set($name, $value) { $this->setField($name, $value); } /** * Unset field value * * Magic method for removing fields by unsetting object properties * * @param string $name * @return void */ public function __unset($name) { $this->removeField($name); } }