Skip to content

Commit

Permalink
Merge pull request #452 from MyHammer/nested_documents
Browse files Browse the repository at this point in the history
add basic support for nested documents
  • Loading branch information
basdenooijer committed Oct 18, 2016
2 parents 7511302 + b6c66a8 commit 87641c8
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 49 deletions.
59 changes: 43 additions & 16 deletions library/Solarium/QueryType/Update/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function getRawData($query)
* Build XML for an add command.
*
* @param \Solarium\QueryType\Update\Query\Command\Add $command
* @param UpdateQuery $query
* @param UpdateQuery $query
*
* @return string
*/
Expand All @@ -131,13 +131,7 @@ public function buildAddXml($command, $query = null)
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);
}
$xml .= $this->buildFieldsXml($name, $boost, $value, $modifier, $query);
}

$version = $doc->getVersion();
Expand All @@ -164,10 +158,10 @@ public function buildDeleteXml($command)
{
$xml = '<delete>';
foreach ($command->getIds() as $id) {
$xml .= '<id>'.htmlspecialchars($id, ENT_NOQUOTES, 'UTF-8').'</id>';
$xml .= '<id>' . htmlspecialchars($id, ENT_NOQUOTES, 'UTF-8') . '</id>';
}
foreach ($command->getQueries() as $query) {
$xml .= '<query>'.htmlspecialchars($query, ENT_NOQUOTES, 'UTF-8').'</query>';
$xml .= '<query>' . htmlspecialchars($query, ENT_NOQUOTES, 'UTF-8') . '</query>';
}
$xml .= '</delete>';

Expand Down Expand Up @@ -225,10 +219,10 @@ public function buildRollbackXml()
*
* Used in the add command
*
* @param string $name
* @param float $boost
* @param mixed $value
* @param string $modifier
* @param string $name
* @param float $boost
* @param mixed $value
* @param string $modifier
* @param UpdateQuery $query
*
* @return string
Expand All @@ -239,7 +233,7 @@ protected function buildFieldXml($name, $boost, $value, $modifier = null, $query
$value = $query->getHelper()->formatDate($value);
}

$xml = '<field name="'.$name.'"';
$xml = '<field name="' . $name . '"';
$xml .= $this->attrib('boost', $boost);
$xml .= $this->attrib('update', $modifier);
if ($value === null) {
Expand All @@ -250,9 +244,42 @@ protected function buildFieldXml($name, $boost, $value, $modifier = null, $query
$value = 'true';
}

$xml .= '>'.htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8');
$xml .= '>' . htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8');
$xml .= '</field>';

return $xml;
}

/**
* @param string $key
*
* @param float $boost
* @param mixed $value
* @param string $modifier
* @param UpdateQuery $query
* @return string
*/
private function buildFieldsXml($key, $boost, $value, $modifier, $query)
{
$xml = '';
if (is_array($value)) {
foreach ($value as $multival) {
if (is_array($multival)) {
$xml .= '<doc>';
foreach ($multival as $k => $v) {
$xml .= $this->buildFieldsXml($k, $boost, $v, $modifier, $query);
}
$xml .= '</doc>';

} else {
$xml .= $this->buildFieldXml($key, $boost, $multival, $modifier, $query);
}
}

} else {
$xml .= $this->buildFieldXml($key, $boost, $value, $modifier, $query);
}

return $xml;
}
}
107 changes: 74 additions & 33 deletions tests/Solarium/Tests/QueryType/Update/RequestBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@

namespace Solarium\Tests\QueryType\Update;

use Solarium\QueryType\Update\Query\Query;
use Solarium\QueryType\Update\RequestBuilder;
use Solarium\Core\Client\Request;
use Solarium\QueryType\Update\Query\Command\Add as AddCommand;
use Solarium\QueryType\Update\Query\Command\Commit as CommitCommand;
use Solarium\QueryType\Update\Query\Command\Delete as DeleteCommand;
use Solarium\QueryType\Update\Query\Command\Optimize as OptimizeCommand;
use Solarium\QueryType\Update\Query\Command\Commit as CommitCommand;
use Solarium\QueryType\Update\Query\Command\Rollback as RollbackCommand;
use Solarium\QueryType\Update\Query\Document\Document;
use Solarium\QueryType\Update\Query\Query;
use Solarium\QueryType\Update\RequestBuilder;

class RequestBuilderTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -127,13 +127,52 @@ public function testBuildAddXmlMultivalueField()
$command->addDocument(new Document(array('id' => array(1, 2, 3), 'text' => 'test < 123 > test')));

$this->assertEquals(
'<add>'.
'<doc>'.
'<field name="id">1</field>'.
'<field name="id">2</field>'.
'<field name="id">3</field>'.
'<field name="text">test &lt; 123 &gt; test</field>'.
'</doc>'.
'<add>' .
'<doc>' .
'<field name="id">1</field>' .
'<field name="id">2</field>' .
'<field name="id">3</field>' .
'<field name="text">test &lt; 123 &gt; test</field>' .
'</doc>' .
'</add>',
$this->builder->buildAddXml($command)
);
}

public function testBuildAddXmlWithNestedDocuments()
{
$command = new AddCommand;
$command->addDocument(
new Document(
array(
'id' => array(
array(
'nested_id' => 42,
'customer_ids' => array(
15,
16
)
),
2,
'foo'
),
'text' => 'test < 123 > test'
)
)
);

$this->assertEquals(
'<add>' .
'<doc>' .
'<doc>' .
'<field name="nested_id">42</field>' .
'<field name="customer_ids">15</field>' .
'<field name="customer_ids">16</field>' .
'</doc>' .
'<field name="id">2</field>' .
'<field name="id">foo</field>' .
'<field name="text">test &lt; 123 &gt; test</field>' .
'</doc>' .
'</add>',
$this->builder->buildAddXml($command)
);
Expand Down Expand Up @@ -189,13 +228,13 @@ public function testBuildAddXmlWithFieldModifiers()
$command->addDocument($doc);

$this->assertEquals(
'<add>'.
'<doc>'.
'<field name="id">1</field>'.
'<field name="category" update="add">123</field>'.
'<field name="name" boost="2.5" update="set">test</field>'.
'<field name="stock" update="inc">2</field>'.
'</doc>'.
'<add>' .
'<doc>' .
'<field name="id">1</field>' .
'<field name="category" update="add">123</field>' .
'<field name="name" boost="2.5" update="set">test</field>' .
'<field name="stock" update="inc">2</field>' .
'</doc>' .
'</add>',
$this->builder->buildAddXml($command)
);
Expand All @@ -214,14 +253,14 @@ public function testBuildAddXmlWithFieldModifiersAndMultivalueFields()
$command->addDocument($doc);

$this->assertEquals(
'<add>'.
'<doc>'.
'<field name="id">1</field>'.
'<field name="category" update="add">123</field>'.
'<field name="category" update="add">234</field>'.
'<field name="name" boost="2.3" update="set">test</field>'.
'<field name="stock" update="inc">2</field>'.
'</doc>'.
'<add>' .
'<doc>' .
'<field name="id">1</field>' .
'<field name="category" update="add">123</field>' .
'<field name="category" update="add">234</field>' .
'<field name="name" boost="2.3" update="set">test</field>' .
'<field name="stock" update="inc">2</field>' .
'</doc>' .
'</add>',
$this->builder->buildAddXml($command)
);
Expand All @@ -244,7 +283,9 @@ public function testBuildAddXmlWithVersionedDocument()
public function testBuildAddXmlWithDateTime()
{
$command = new AddCommand;
$command->addDocument(new Document(array('id' => 1, 'datetime' => new \DateTime('2013-01-15 14:41:58'))));
$command->addDocument(
new Document(array('id' => 1, 'datetime' => new \DateTime('2013-01-15 14:41:58', new \DateTimeZone('Europe/London'))))
);

$this->assertEquals(
'<add><doc><field name="id">1</field><field name="datetime">2013-01-15T14:41:58Z</field></doc></add>',
Expand All @@ -262,11 +303,11 @@ public function testBuildAddXmlWithFieldModifierAndNullValue()
$command->addDocument($doc);

$this->assertEquals(
'<add>'.
'<doc>'.
'<field name="employeeId">05991</field>'.
'<field name="skills" update="set" null="true"></field>'.
'</doc>'.
'<add>' .
'<doc>' .
'<field name="employeeId">05991</field>' .
'<field name="skills" update="set" null="true"></field>' .
'</doc>' .
'</add>',
$this->builder->buildAddXml($command)
);
Expand Down Expand Up @@ -366,7 +407,7 @@ public function testBuildOptimizeXml()

public function testBuildOptimizeXmlWithParams()
{
$command = new OptimizeCommand(array('softcommit'=>true, 'waitsearcher'=>false, 'maxsegments'=>10));
$command = new OptimizeCommand(array('softcommit' => true, 'waitsearcher' => false, 'maxsegments' => 10));

$this->assertEquals(
'<optimize softCommit="true" waitSearcher="false" maxSegments="10"/>',
Expand All @@ -386,7 +427,7 @@ public function testBuildCommitXml()

public function testBuildCommitXmlWithParams()
{
$command = new CommitCommand(array('softcommit'=>true, 'waitsearcher'=>false, 'expungedeletes'=>true));
$command = new CommitCommand(array('softcommit' => true, 'waitsearcher' => false, 'expungedeletes' => true));

$this->assertEquals(
'<commit softCommit="true" waitSearcher="false" expungeDeletes="true"/>',
Expand Down

0 comments on commit 87641c8

Please sign in to comment.