Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add foreach processor #1813

Merged
merged 10 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/Processor/ForeachProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Elastica\Processor;

/**
* Elastica Foreach Processor.
*
* @author Federico Panini <fpanini@gmail.com>
* @author Thibaut Simon-Fine <tsimonfine@gmail.com>
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/foreach-processor.html
*/
class ForeachProcessor extends AbstractProcessor
{
public const DEFAULT_IGNORE_MISSING_VALUE = false;

/**
* @param AbstractProcessor|array $processor
*/
public function __construct(string $field, $processor)
{
$this->setField($field);

if ($processor instanceof AbstractProcessor) {
$this->setProcessor($processor);
} elseif (\is_array($processor)) {
$this->setRawProcessor($processor);
} else {
throw new \TypeError(\sprintf('Argument 2 passed to %s::__construct() must be of type %s|array, %s given.', self::class, AbstractProcessor::class, \is_object($processor) ? \get_class($processor) : \gettype($processor)));
}
}

/**
* Set field.
*
* @return $this
*/
public function setField(string $field): self
{
return $this->setParam('field', $field);
}

/**
* Set processor.
*
* @return $this
*/
public function setProcessor(AbstractProcessor $processor): self
{
return $this->setParam('processor', $processor);
}

/**
* Set raw processor.
* Example : ['remove' => ['field' => 'user_agent']].
*
* @return $this
*/
public function setRawProcessor(array $processor): self
{
return $this->setParam('processor', $processor);
}

/**
* Set ignore_missing. Default value false.
*
* If true and field does not exist or is null, the processor quietly exits without modifying the document
*
* @return $this
*/
public function setIgnoreMissing(bool $ignoreMissing): self
{
return $this->setParam('ignore_missing', $ignoreMissing);
}
}
125 changes: 125 additions & 0 deletions tests/Processor/ForeachProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace Elastica\Test\Processor;

use Elastica\Bulk;
use Elastica\Document;
use Elastica\Processor\ForeachProcessor;
use Elastica\Processor\Uppercase;
use Elastica\ResultSet;
use Elastica\Test\BasePipeline as BasePipelineTest;

/**
* @internal
*/
class ForeachProcessorTest extends BasePipelineTest
{
/**
* @group unit
*/
public function testForeachProcessorDefault(): void
{
$subprocessor = new Uppercase('field2');
$processor = new ForeachProcessor('field1', $subprocessor);

$expected = [
'foreach' => [
'field' => 'field1',
'processor' => [
'uppercase' => [
'field' => 'field2',
],
],
],
];

$this->assertEquals($expected, $processor->toArray());
}

/**
* @group unit
*/
public function testForeachRawProcessorDefault(): void
{
$subprocessor = [
'uppercase' => [
'field' => 'field2',
],
];
$processor = new ForeachProcessor('field1', $subprocessor);

$expected = [
'foreach' => [
'field' => 'field1',
'processor' => [
'uppercase' => [
'field' => 'field2',
],
],
],
];

$this->assertEquals($expected, $processor->toArray());
}

/**
* @group unit
*/
public function testForeachProcessorIgnoreMissing(): void
{
$subprocessor = new Uppercase('field2');
$processor = new ForeachProcessor('field1', $subprocessor);
$processor->setIgnoreMissing(true);

$expected = [
'foreach' => [
'field' => 'field1',
'processor' => [
'uppercase' => [
'field' => 'field2',
],
],
'ignore_missing' => true,
],
];

$this->assertEquals($expected, $processor->toArray());
}

/**
* @group functional
*/
public function testForeachProcessor(): void
{
$subprocessor = new Uppercase('_ingest._value');
$foreach = new ForeachProcessor('values', $subprocessor);

$pipeline = $this->_createPipeline('my_custom_pipeline', 'pipeline for Foreach');
$pipeline->addProcessor($foreach)->create();

$index = $this->_createIndex();
$bulk = new Bulk($index->getClient());
$bulk->setIndex($index);

$bulk->addDocuments([
new Document(null, ['name' => 'ruflin', 'type' => 'elastica', 'values' => ['foo', 'bar', 'baz']]),
]);
$bulk->setRequestParam('pipeline', 'my_custom_pipeline');

$bulk->send();
$index->refresh();

/** @var ResultSet $result */
$result = $index->search('*');

$this->assertCount(1, $result->getResults());

foreach ($result->getResults() as $rx) {
$value = $rx->getData();
$this->assertCount(3, $value['values']);
$this->assertEquals('FOO', $value['values'][0]);
$this->assertEquals('BAR', $value['values'][1]);
$this->assertEquals('BAZ', $value['values'][2]);
}
}
}