Skip to content

Commit

Permalink
Merge pull request #3 from ppshobi/develop
Browse files Browse the repository at this point in the history
Added limit records functionality
  • Loading branch information
ppshobi authored Apr 9, 2019
2 parents ce4d50e + 74bc0a8 commit 5513c97
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Commands/Search/QueryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(string $collection, string $bucket, string $terms, $
'collection' => $collection,
'bucket' => $bucket,
'terms' => quote($terms),
'limit' => $limit,
'limit' => $limit ? "LIMIT($limit)": null,
];

parent::__construct($this->command, $this->parameters);
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Search/SuggestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(string $collection, string $bucket, string $terms, $
'collection' => $collection,
'bucket' => $bucket,
'terms' => quote($terms),
'limit' => $limit,
'limit' => $limit ? "LIMIT($limit)" : null,
];

parent::__construct($this->command, $this->parameters);
Expand Down
10 changes: 6 additions & 4 deletions src/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ public function connect()
* @param $collection
* @param $bucket
* @param $terms
* @param $limit
* @return array
* @throws CommandFailedException
*/
public function query($collection, $bucket, $terms): array
public function query($collection, $bucket, $terms, $limit = null): array
{
$response = $this->send(new QueryCommand($collection, $bucket, $terms));
$response = $this->send(new QueryCommand($collection, $bucket, $terms, $limit));

if(! $response->getStatus() == 'PENDING') {
throw new CommandFailedException;
Expand All @@ -66,12 +67,13 @@ public function query($collection, $bucket, $terms): array
* @param $collection
* @param $bucket
* @param $terms
* @param $limit
* @return array
* @throws CommandFailedException
*/
public function suggest($collection, $bucket, $terms): array
public function suggest($collection, $bucket, $terms, $limit=null): array
{
$response = $this->send(new SuggestCommand($collection, $bucket, $terms));
$response = $this->send(new SuggestCommand($collection, $bucket, $terms, $limit));

if(! $response->getStatus() == 'PENDING') {
throw new CommandFailedException;
Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/SearchChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,51 @@ public function it_can_query_sonic_protocol_and_return_matched_records()
$this->assertTrue(is_array($this->search->query($this->collection, $this->bucket, "are")));
}

/**
* @test
*
*/
public function it_can_apply_limit_on_returned_records_for_a_query()
{
$this->ingest->connect();
$this->search->connect();
$this->control->connect();

$this->ingest->flushc($this->collection);

$this->assertEquals("OK", $this->ingest->push($this->collection, $this->bucket, "1234", "hi Shobi how are you?")->getStatus());
$this->assertEquals("OK", $this->ingest->push($this->collection, $this->bucket, "1235", "hi are you fine ?")->getStatus());
$this->assertEquals("OK", $this->ingest->push($this->collection, $this->bucket, "3456", "Jomit? How are you?")->getStatus());

$this->control->consolidate();

$this->assertCount(2, $this->search->query($this->collection, $this->bucket, "are", 2));
$this->assertCount(1, $this->search->query($this->collection, $this->bucket, "are", 1));
}

/**
* @test
*
*/
public function it_can_apply_limit_on_returned_records_for_a_suggest_command()
{
$this->ingest->connect();
$this->search->connect();
$this->control->connect();

$this->ingest->flushc($this->collection);

$this->assertEquals("OK", $this->ingest->push($this->collection, $this->bucket, "1234", "coincident")->getStatus());
$this->assertEquals("OK", $this->ingest->push($this->collection, $this->bucket, "1235", "coincidental")->getStatus());
$this->assertEquals("OK", $this->ingest->push($this->collection, $this->bucket, "3456", "coinciding")->getStatus());

$this->control->consolidate();

$this->assertCount(3, $this->search->suggest($this->collection, $this->bucket, "coin"));
$this->assertCount(2, $this->search->suggest($this->collection, $this->bucket, "coin", 2));
$this->assertCount(1, $this->search->suggest($this->collection, $this->bucket, "coin", 1));
}

/**
* @test
*
Expand Down

0 comments on commit 5513c97

Please sign in to comment.