Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.
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
12 changes: 11 additions & 1 deletion Model/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Index implements HttpTransportable
*
* @var string[]
*/
private $fields = [];
private $fields;

/**
* Metadata.
Expand Down Expand Up @@ -199,6 +199,16 @@ public function getFields(): array
return $this->fields;
}

/**
* @param array $fields
*
* @return void
*/
public function withFields(array $fields)
{
$this->fields = $fields;
}

/**
* Get Metadata.
*
Expand Down
33 changes: 28 additions & 5 deletions Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,7 @@ public static function create(
*/
public static function createMatchAll(): self
{
return static::create(
'',
self::DEFAULT_PAGE,
self::DEFAULT_SIZE
);
return static::create('');
}

/**
Expand Down Expand Up @@ -342,6 +338,16 @@ public function getFields(): array
return $this->fields;
}

/**
* @param int $size
*
* @return void
*/
public function forceSize(int $size)
{
$this->size = $size;
}

/**
* Filter universe by types.
*
Expand Down Expand Up @@ -867,6 +873,23 @@ public function getAggregation(string $aggregationName): ? Aggregation
return $this->aggregations[$aggregationName] ?? null;
}

/**
* @param string $aggregationField
*
* @return void
*/
public function deleteAggregationByField(string $aggregationField): void
{
$aggregationField = Item::getPathByField($aggregationField);
foreach ($this->aggregations as $key => $aggregation) {
if ($aggregation->getField() === $aggregationField) {
unset($this->aggregations[$key]);

return;
}
}
}

/**
* Return Querytext.
*
Expand Down
31 changes: 31 additions & 0 deletions Tests/Model/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,35 @@ public function testWithMetadataValue()
$index = Index::createFromArray($index->toArray());
$this->assertEquals('val1', $index->getMetadataValue('key1'));
}

public function testWithFields()
{
$index = Index::createFromArray([
'uuid' => [
'id' => 'testId',
],
'app_id' => [
'id' => 'testAppId',
],
]);

$this->assertEquals([], $index->getFields());

$index = Index::createFromArray([
'uuid' => [
'id' => 'testId',
],
'app_id' => [
'id' => 'testAppId',
],
'is_ok' => true,
'doc_count' => 10,
'size' => '1kb',
'fields' => ['f1', 'f2', 'f3'],
]);

$this->assertEquals(['f1', 'f2', 'f3'], $index->getFields());
$index->withFields(['f4', 'f5']);
$this->assertEquals(['f4', 'f5'], $index->getFields());
}
}
44 changes: 44 additions & 0 deletions Tests/Query/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,48 @@ public function testAggregationPromoted()
$this->assertEquals(['item1', 'item3'], $query->getAggregation('field2')->getPromoted());
$this->assertEquals(['item4'], $query->getAggregation('field3')->getPromoted());
}

public function testDeleteAggregationByField()
{
$query = Query::createMatchAll()
->aggregateBy('field1', 'field1', Filter::AT_LEAST_ONE, Aggregation::SORT_BY_COUNT_ASC)
->aggregateBy('field2', 'field2', Filter::AT_LEAST_ONE, Aggregation::SORT_BY_COUNT_ASC);

$this->assertCount(2, $query->getAggregations());
$this->assertNotNull($query->getAggregation('field1'));
$this->assertNotNull($query->getAggregation('field2'));
$this->assertNull($query->getAggregation('field3'));

$query->deleteAggregationByField('field1');
$this->assertCount(1, $query->getAggregations());
$this->assertNull($query->getAggregation('field1'));
$this->assertNotNull($query->getAggregation('field2'));
$this->assertNull($query->getAggregation('field3'));

$query->deleteAggregationByField('field1');
$this->assertCount(1, $query->getAggregations());
$this->assertNull($query->getAggregation('field1'));
$this->assertNotNull($query->getAggregation('field2'));
$this->assertNull($query->getAggregation('field3'));

$query->deleteAggregationByField('field2');
$this->assertCount(0, $query->getAggregations());
$this->assertNull($query->getAggregation('field1'));
$this->assertNull($query->getAggregation('field2'));
$this->assertNull($query->getAggregation('field3'));

$query->deleteAggregationByField('field3');
$this->assertCount(0, $query->getAggregations());
$this->assertNull($query->getAggregation('field1'));
$this->assertNull($query->getAggregation('field2'));
$this->assertNull($query->getAggregation('field3'));
}

public function testForceSize()
{
$query = Query::create('x', 1, 10);
$this->assertEquals(10, $query->getSize());
$query->forceSize(7);
$this->assertEquals(7, $query->getSize());
}
}