forked from biblioverse/biblioteca
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
309 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Biblioteca\TypesenseBundle\Mapper; | ||
|
||
class MetadataMapping implements MetadataMappingInterface, \ArrayAccess, \IteratorAggregate, \Countable | ||
{ | ||
public function __construct(private array $data) | ||
{ | ||
} | ||
|
||
public function getIterator(): \Traversable | ||
{ | ||
return new \ArrayIterator($this->data); | ||
} | ||
|
||
public function offsetExists(mixed $offset): bool | ||
{ | ||
return isset($this->data[$offset]); | ||
} | ||
|
||
public function offsetGet(mixed $offset): mixed | ||
{ | ||
return $this->data[$offset] ?? null; | ||
} | ||
|
||
public function offsetSet(mixed $offset, mixed $value): void | ||
{ | ||
if (is_null($offset)) { | ||
$this->data[] = $value; | ||
} else { | ||
$this->data[$offset] = $value; | ||
} | ||
} | ||
|
||
public function offsetUnset(mixed $offset): void | ||
{ | ||
unset($this->data[$offset]); | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return count($this->data); | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return $this->data; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
BibliotecaTypesenseBundle/src/Mapper/MetadataMappingInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Biblioteca\TypesenseBundle\Mapper; | ||
|
||
interface MetadataMappingInterface | ||
{ | ||
/** | ||
* Field options and value | ||
* @return array<string,mixed> | ||
*/ | ||
public function toArray(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Biblioteca\TypesenseBundle\Populate; | ||
|
||
class BatchGenerator | ||
{ | ||
private iterable $iterable; | ||
private int $batchSize; | ||
|
||
/** | ||
* Constructor to initialize the iterable and batch size. | ||
* | ||
* @param iterable $iterable the data source to process | ||
* @param int $batchSize the number of elements in each batch | ||
* @throws \InvalidArgumentException if batch size is not greater than 0 | ||
*/ | ||
public function __construct(iterable $iterable, int $batchSize) | ||
{ | ||
if ($batchSize <= 0) { | ||
throw new \InvalidArgumentException('Batch size must be greater than 0.'); | ||
} | ||
|
||
$this->iterable = $iterable; | ||
$this->batchSize = $batchSize; | ||
} | ||
|
||
/** | ||
* Generate batches of elements from the iterable. | ||
* | ||
* @return \Generator yields an array of elements in each batch | ||
*/ | ||
public function generate(): \Generator | ||
{ | ||
$batch = []; | ||
foreach ($this->iterable as $item) { | ||
$batch[] = $item; | ||
|
||
if (count($batch) === $this->batchSize) { | ||
yield $batch; | ||
$batch = []; | ||
} | ||
} | ||
|
||
// Yield remaining elements if they exist | ||
if (!empty($batch)) { | ||
yield $batch; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Biblioteca\TypesenseBundle\Query; | ||
|
||
enum InfixEnum: string | ||
{ | ||
case OFF = 'off'; | ||
case ALWAYS = 'always'; | ||
case FALLBACK = 'fallback'; | ||
} |
116 changes: 116 additions & 0 deletions
116
BibliotecaTypesenseBundle/src/Query/SearchParameter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace Biblioteca\TypesenseBundle\Query; | ||
|
||
class SearchParameter implements SearchParameterInterface | ||
{ | ||
private readonly ?string $infix; | ||
/** | ||
* @var bool[]|null | ||
*/ | ||
private readonly ?array $prefix; | ||
|
||
/** | ||
* @var string[]|null | ||
*/ | ||
private readonly ?array $stopwords; | ||
|
||
/** | ||
* @param string|InfixEnum[]|null $infix | ||
* @param bool|bool[]|null $prefix | ||
* @param string[]|null $stopwords | ||
*/ | ||
public function __construct( | ||
private readonly string $q, | ||
private readonly string $queryBy, | ||
private readonly ?string $filterBy = null, | ||
private readonly ?string $sortBy = null, | ||
string|array|null $infix = null, | ||
bool|array|null $prefix = null, | ||
private readonly bool $preSegmentedQuery = false, | ||
private readonly ?string $preset = null, | ||
private readonly ?VectorQuery $vectorQuery = null, | ||
?array $stopwords = null, | ||
private readonly ?string $facetBy = null, | ||
private readonly ?string $facetQuery = null, | ||
private readonly ?int $remoteEmbeddingTimeoutMs = null, | ||
private readonly ?int $remoteEmbeddingBatchSize = null, | ||
private readonly ?int $remoteEmbeddingNumTries = null, | ||
private readonly ?string $highlightFields = null, | ||
private readonly ?int $highlightAffixNumTokens = null, | ||
private readonly ?string $highlightStartTag = null, | ||
private readonly ?string $highlightEndTag = null, | ||
private readonly ?string $groupBy = null, | ||
private readonly ?int $groupLimit = null, | ||
private readonly ?int $numTypos = null, | ||
private readonly ?int $page = null, | ||
private readonly ?int $perPage = null, | ||
private readonly ?int $maxFacetValues = null, | ||
private readonly ?int $minLen1Typo = null, | ||
private readonly ?int $minLen2Typo = null, | ||
private readonly ?float $dropTokensThreshold = null, | ||
private readonly ?array $hiddenHits = null, | ||
private readonly ?string $excludeFields = null, | ||
private readonly ?VoiceQueryInterface $voiceQuery = null, | ||
) { | ||
$this->infix = $this->convertArray($infix, fn ($infix) => $infix instanceof InfixEnum ? $infix->value : $infix, InfixEnum::class); | ||
$this->prefix = $prefix === [] ? null : implode(',', array_map(fn (bool $value) => $value ? 'true' : 'false', $prefix)); | ||
$this->stopwords = $stopwords === [] ? null : implode(',', $stopwords); | ||
|
||
// Check incompatible combinations | ||
if ($this->vectorQuery !== null && $this->infix !== null) { | ||
throw new \InvalidArgumentException('Cannot set both infix and vectorQuery'); | ||
} | ||
} | ||
|
||
private function convertArray(mixed $values, callable $convert, ?string $className = null): ?string | ||
{ | ||
if (!is_array($values)) { | ||
return $values === null ? null : $convert($values); | ||
} | ||
foreach ($values as $value) { | ||
if ($className !== null && !$value instanceof $className) { | ||
throw new \InvalidArgumentException(sprintf('Expected type %s, got %s', $className, is_object($value) ? get_class($value) : gettype($value))); | ||
} | ||
} | ||
|
||
return $values === [] ? null : implode(',', array_map($convert, $values)); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return array_filter([ | ||
'q' => $this->q, | ||
'query_by' => $this->queryBy, | ||
'filter_by' => $this->filterBy, | ||
'sort_by' => $this->sortBy, | ||
'drop_tokens_threshold' => $this->dropTokensThreshold, | ||
'facet_by' => $this->facetBy, | ||
'facet_query' => $this->facetQuery, | ||
'group_by' => $this->groupBy, | ||
'group_limit' => $this->groupLimit, | ||
'hidden_hits' => $this->hiddenHits, | ||
'highlight_affix_num_tokens' => $this->highlightAffixNumTokens, | ||
'highlight_end_tag' => $this->highlightEndTag, | ||
'highlight_fields' => $this->highlightFields, | ||
'highlight_start_tag' => $this->highlightStartTag, | ||
'infix' => $this->infix, | ||
'max_facet_values' => $this->maxFacetValues, | ||
'min_len_1typo' => $this->minLen1Typo, | ||
'min_len_2typo' => $this->minLen2Typo, | ||
'num_typos' => $this->numTypos, | ||
'page' => $this->page, | ||
'per_page' => $this->perPage, | ||
'pre_segmented_query' => $this->preSegmentedQuery, | ||
'prefix' => $this->prefix, | ||
'preset' => $this->preset, | ||
'remote_embedding_batch_size' => $this->remoteEmbeddingBatchSize, | ||
'remote_embedding_num_tries' => $this->remoteEmbeddingNumTries, | ||
'remote_embedding_timeout_ms' => $this->remoteEmbeddingTimeoutMs, | ||
'stopwords' => $this->stopwords, | ||
'vector_query' => $this->vectorQuery, | ||
'exclude_fields' => $this->excludeFields, | ||
'voice_query' => $this->voiceQuery ? (string) $this->voiceQuery : null, | ||
], fn (mixed $value) => !is_null($value)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
BibliotecaTypesenseBundle/src/Query/SearchParameterInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Biblioteca\TypesenseBundle\Query; | ||
|
||
interface SearchParameterInterface | ||
{ | ||
/** | ||
* @return array<string,mixed> | ||
*/ | ||
public function toArray(): array; | ||
} |
Oops, something went wrong.