Skip to content

Commit

Permalink
An empty array as value in combination with the set modifier should…
Browse files Browse the repository at this point in the history
… remove a field when performing Atomic Updates (#699)

fixes #698
  • Loading branch information
Markus Kalkbrenner authored Sep 9, 2019
1 parent 48420e8 commit bd957b9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [5.1.2]
### Fixed
- BufferedAdd does not support Symfony event dispatcher
- An empty array as value in combination with the `set` modifier should remove a field when performing Atomic Updates

## [5.1.1]
### Fixed
Expand Down
8 changes: 8 additions & 0 deletions src/QueryType/Update/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Solarium\QueryType\Update\Query\Command\Commit;
use Solarium\QueryType\Update\Query\Command\Delete;
use Solarium\QueryType\Update\Query\Command\Optimize;
use Solarium\QueryType\Update\Query\Document;
use Solarium\QueryType\Update\Query\Query as UpdateQuery;

/**
Expand Down Expand Up @@ -225,6 +226,13 @@ protected function buildFieldXml(string $name, ?float $boost, $value, ?string $m
private function buildFieldsXml(string $key, ?float $boost, $value, ?string $modifier): string
{
$xml = '';

// Remove the values if 'null' or empty list is specified as the new value
// @see https://lucene.apache.org/solr/guide/8_1/updating-parts-of-documents.html
if (Document::MODIFIER_SET === $modifier && is_array($value) && empty($value)) {
$value = null;
}

if (is_array($value)) {
foreach ($value as $multival) {
if (is_array($multival)) {
Expand Down
4 changes: 4 additions & 0 deletions tests/QueryType/Update/RequestBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ public function testBuildAddXmlWithFieldModifiers()
$doc->setKey('id', 1);
$doc->addField('category', 123, null, Document::MODIFIER_ADD);
$doc->addField('name', 'test', 2.5, Document::MODIFIER_SET);
$doc->setField('skills', null, null, Document::MODIFIER_SET);
$doc->setField('parts', [], null, Document::MODIFIER_SET);
$doc->setField('stock', 2, null, Document::MODIFIER_INC);

$command = new AddCommand();
Expand All @@ -204,6 +206,8 @@ public function testBuildAddXmlWithFieldModifiers()
'<field name="id">1</field>'.
'<field name="category" update="add">123</field>'.
'<field name="name" boost="2.5" update="set">test</field>'.
'<field name="skills" update="set" null="true"></field>'.
'<field name="parts" update="set" null="true"></field>'.
'<field name="stock" update="inc">2</field>'.
'</doc>'.
'</add>',
Expand Down

0 comments on commit bd957b9

Please sign in to comment.