Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
services:
typesense:
image: typesense/typesense:27.0.rc21
image: typesense/typesense:28.0.rc36
ports:
- 8108:8108/tcp
volumes:
Expand Down
14 changes: 14 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class Client
*/
public Analytics $analytics;

/**
* @var Stemming
*/
public Stemming $stemming;

/**
* @var Conversations
*/
Expand Down Expand Up @@ -108,6 +113,7 @@ public function __construct(array $config)
$this->multiSearch = new MultiSearch($this->apiCall);
$this->presets = new Presets($this->apiCall);
$this->analytics = new Analytics($this->apiCall);
$this->stemming = new Stemming($this->apiCall);
$this->conversations = new Conversations($this->apiCall);
}

Expand Down Expand Up @@ -199,6 +205,14 @@ public function getAnalytics(): Analytics
return $this->analytics;
}

/**
* @return Stemming
*/
public function getStemming(): Stemming
{
return $this->stemming;
}

/**
* @return Conversations
*/
Expand Down
9 changes: 0 additions & 9 deletions src/Conversations.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ public function __construct(ApiCall $apiCall)
$this->typesenseModels = new ConversationModels($this->apiCall);
}

/**
* @return array
* @throws TypesenseClientError|HttpClientException
*/
public function retrieve(): array
{
return $this->apiCall->get(static::RESOURCE_PATH, []);
}

/**
* @return Models
*/
Expand Down
26 changes: 26 additions & 0 deletions src/Stemming.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Typesense;

class Stemming
{
const RESOURCE_PATH = '/stemming';

private ApiCall $apiCall;

private StemmingDictionaries $typesenseDictionaries;


public function __construct(ApiCall $apiCall)
{
$this->apiCall = $apiCall;
}

public function dictionaries()
{
if (!isset($this->typesenseDictionaries)) {
$this->typesenseDictionaries = new StemmingDictionaries($this->apiCall);
}
return $this->typesenseDictionaries;
}
}
99 changes: 99 additions & 0 deletions src/StemmingDictionaries.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Typesense;

class StemmingDictionaries implements \ArrayAccess
{
const RESOURCE_PATH = '/stemming/dictionaries';

private ApiCall $apiCall;
private $typesenseDictionaries = [];

public function __construct(ApiCall $apiCall)
{
$this->apiCall = $apiCall;
}

public function __get($id)
{
if (!isset($this->typesenseDictionaries[$id])) {
$this->typesenseDictionaries[$id] = new StemmingDictionary($id, $this->apiCall);
}
return $this->typesenseDictionaries[$id];
}

public function upsert($id, $wordRootCombinations)
{
$dictionaryInJSONLFormat = is_array($wordRootCombinations) ? implode(
"\n",
array_map(
static fn(array $wordRootCombo) => json_encode($wordRootCombo, JSON_THROW_ON_ERROR),
$wordRootCombinations
)
) : $wordRootCombinations;

$resultsInJSONLFormat = $this->apiCall->post($this->endpoint_path("import"), $dictionaryInJSONLFormat, false, ["id" => $id]);

return is_array($wordRootCombinations) ? array_map(
static function ($item) {
return json_decode($item, true, 512, JSON_THROW_ON_ERROR);
},
array_filter(
explode("\n", $resultsInJSONLFormat),
'strlen'
)
) : $resultsInJSONLFormat;
}

public function retrieve()
{
$response = $this->apiCall->get(StemmingDictionaries::RESOURCE_PATH, []);

// If response is null, return empty dictionaries structure
if ($response === null) {
return ['dictionaries' => []];
}
return $response;
}

private function endpoint_path($operation = null)
{
return $operation === null ? self::RESOURCE_PATH : self::RESOURCE_PATH . "/" . encodeURIComponent($operation);
}

/**
* @inheritDoc
*/
public function offsetExists($offset): bool
{
return isset($this->typesenseDictionaries[$offset]);
}

/**
* @inheritDoc
*/
public function offsetGet($offset): StemmingDictionary
{
if (!isset($this->typesenseDictionaries[$offset])) {
$this->typesenseDictionaries[$offset] = new StemmingDictionary($offset, $this->apiCall);
}

return $this->typesenseDictionaries[$offset];
}

/**
* @inheritDoc
*/
public function offsetSet($offset, $value): void
{
$this->typesenseDictionaries[$offset] = $value;
}

/**
* @inheritDoc
*/
public function offsetUnset($offset): void
{
unset($this->typesenseDictionaries[$offset]);
}
}
25 changes: 25 additions & 0 deletions src/StemmingDictionary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Typesense;

class StemmingDictionary
{
private $id;
private ApiCall $apiCall;

public function __construct(string $id, ApiCall $apiCall)
{
$this->id = $id;
$this->apiCall = $apiCall;
}

public function retrieve()
{
return $this->apiCall->get($this->endpointPath(), []);
}

private function endpointPath()
{
return StemmingDictionaries::RESOURCE_PATH . '/' . encodeURIComponent($this->id);
}
}
2 changes: 1 addition & 1 deletion tests/Feature/ConversationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public function testCanDeleteAConversation(): void

private function endPointPath(): string
{
return sprintf('%s/%s', ConversationsTest::RESOURCE_PATH, $this->id);
return sprintf('%s/%s', "/conversations", $this->id);
}
}
19 changes: 0 additions & 19 deletions tests/Feature/ConversationsTest.php

This file was deleted.

47 changes: 47 additions & 0 deletions tests/Feature/StemmingDictionariesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Feature;

use Tests\TestCase;

class StemmingDictionariesTest extends TestCase
{
private $dictionaryId = 'test_dictionary';

private $dictionary = [
["root" => "exampleRoot1", "word" => "exampleWord1"],
["root" => "exampleRoot2", "word" => "exampleWord2"]
];

private $dictionaryUpsertResponse = null;

protected function setUp(): void
{
parent::setUp();

$this->client()->stemming->dictionaries()->upsert(
$this->dictionaryId,
$this->dictionary
);
}

public function testCanUpsertADictionary(): void
{
$this->dictionaryUpsertResponse = $this->client()->stemming->dictionaries()->upsert($this->dictionaryId, $this->dictionary);
$this->assertEquals($this->dictionary, $this->dictionaryUpsertResponse);
}

public function testCanRetrieveADictionary(): void
{
$returnData = $this->client()->stemming->dictionaries()[$this->dictionaryId]->retrieve();
$this->assertEquals($returnData['id'], $this->dictionaryId);
}


public function testCanRetrieveAllDicitionaries(): void
{
$returnData = $this->client()->stemming->dictionaries()->retrieve();
$this->assertCount(1, $returnData['dictionaries']);
$this->assertEquals($returnData['dictionaries'][0], $this->dictionaryId);
}
}