/** * Convenience method to add a 'add documents' command. * * If you need more control, like choosing a key for the command you need to * create you own command instance and use the add method. * * @param array $documents * @param boolean $overwrite * @param int $commitWithin * * @return self Provides fluent interface */ public function addDocuments($documents, $overwrite = null, $commitWithin = null) { $add = new AddCommand(); if (null !== $overwrite) { $add->setOverwrite($overwrite); } if (null !== $commitWithin) { $add->setCommitWithin($commitWithin); } $add->addDocuments($documents); return $this->add(null, $add); }
public function testBuildAddXmlWithFieldModifierAndNullValue() { $doc = new Document(); $doc->setKey('employeeId', '05991'); $doc->addField('skills', null, null, Document::MODIFIER_SET); $command = new AddCommand(); $command->addDocument($doc); $this->assertEquals('<add>' . '<doc>' . '<field name="employeeId">05991</field>' . '<field name="skills" update="set" null="true"></field>' . '</doc>' . '</add>', $this->builder->buildAddXml($command)); }
/** * Build XML for an add command * * @param Query\Command\Add $command * @param UpdateQuery $query * @return string */ public function buildAddXml($command, $query = null) { $xml = '<add'; $xml .= $this->boolAttrib('overwrite', $command->getOverwrite()); $xml .= $this->attrib('commitWithin', $command->getCommitWithin()); $xml .= '>'; foreach ($command->getDocuments() as $doc) { $xml .= '<doc'; $xml .= $this->attrib('boost', $doc->getBoost()); $xml .= '>'; foreach ($doc->getFields() as $name => $value) { $boost = $doc->getFieldBoost($name); $modifier = $doc->getFieldModifier($name); if (is_array($value)) { foreach ($value as $multival) { $xml .= $this->buildFieldXml($name, $boost, $multival, $modifier, $query); } } else { $xml .= $this->buildFieldXml($name, $boost, $value, $modifier, $query); } } $version = $doc->getVersion(); if ($version !== null) { $xml .= $this->buildFieldXml('_version_', null, $version); } $xml .= '</doc>'; } $xml .= '</add>'; return $xml; }
public function testBuildAddXmlWithDateTime() { $command = new AddCommand(); $command->addDocument(new Document(array('id' => 1, 'datetime' => new \DateTime('2013-01-15 14:41:58')))); $this->assertEquals('<add><doc><field name="id">1</field><field name="datetime">2013-01-15T14:41:58Z</field></doc></add>', $this->builder->buildAddXml($command, $this->query)); }