Skip to content

Commit

Permalink
feat: where filters
Browse files Browse the repository at this point in the history
  • Loading branch information
faustoq committed Jul 4, 2024
1 parent b5f96be commit 831c5a3
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 40 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ use OramaCloud\Query;

$client = new Client("<Your Orama Cloud Endpoint>", "<Your Orama Cloud API Key>");

$query = Query::fromArray([
'term' => 'red shoes'
]);

$result = $client->search($query);
$result = $client->search(
(new Query())
->term('red shoes')
->where('price', 'gt', 99.99)
->where('category', 'eq', 'sport')
);
```

## Advanced search
Expand Down
75 changes: 58 additions & 17 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,74 @@

namespace OramaCloud;

use OramaCloud\QueryParams\Where;

class Query {

private $term;
private $mode;
private $limit = 5;
private $offset = 0;
private $limit;
private $offset;
private $where = [];

public function __construct($term = '', $mode = 'fulltext') {
$this->term = $term;
$this->mode = $mode;
}

public function setTerm($term) {
public function term($term) {
$this->term = $term;
return $this;
}

public function setMode($mode) {
public function mode($mode) {
$this->mode = $mode;
return $this;
}

public function setLimit($limit) {
public function where($property, $operator, $value) {
$this->where[] = new Where($property, $operator, $value);
return $this;
}

public function limit($limit) {
$this->limit = $limit;
return $this;
}

public function setOffset($offset) {
public function offset($offset) {
$this->offset = $offset;
return $this;
}

public function toArray() {
return [
'term' => $this->term,
'mode' => $this->mode,
'limit' => $this->limit,
'offset' => $this->offset
];
$array = [];

if (!is_null($this->term)) {
$array['term'] = $this->term;
}

if (!is_null($this->mode)) {
$array['mode'] = $this->mode;
}

if (!is_null($this->limit)) {
$array['limit'] = $this->limit;
}

if (!is_null($this->offset)) {
$array['offset'] = $this->offset;
}

if (isset($this->where) && is_array($this->where) && count($this->where) > 0) {
foreach ($this->where as $where) {
foreach ($where->toArray() as $key => $value) {
$array['where'][$key] = $value;
}
}
}

return $array;
}

public function toJson() {
Expand All @@ -49,11 +78,23 @@ public function toJson() {

public static function fromArray($array) {
$query = new Query();
$query
->setTerm(isset($array['term']) ? $array['term'] : '')
->setMode(isset($array['mode']) ? $array['mode'] : 'fulltext')
->setLimit(isset($array['limit']) ? $array['limit'] : 5)
->setOffset(isset($array['offset']) ? $array['offset'] : 0);

$query->term(isset($array['term']) ? $array['term'] : '');
$query->mode(isset($array['mode']) ? $array['mode'] : 'fulltext');

if (isset($array['where']) && !is_null($array['where'])) {
foreach ($array['where'] as $property => $value) {
$query->where($property, key($value), $value[key($value)]);
}
}

if (isset($array['limit']) && !is_null($array['limit'])) {
$query->limit($array['limit']);
}

if (isset($array['offset']) && !is_null($array['offset'])) {
$query->offset($array['offset']);
}

return $query;
}
Expand Down
24 changes: 24 additions & 0 deletions src/QueryParams/Where.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace OramaCloud\QueryParams;

class Where {

private $property;
private $operator;
private $value;

public function __construct($property, $operator, $value) {
$this->property = $property;
$this->operator = $operator;
$this->value = $value;
}

public function toArray() {
return [
$this->property => [
$this->operator => $this->value
]
];
}
}
8 changes: 6 additions & 2 deletions tests/Feature/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@

test('basic fulltext search', function () {
$client = new Client(API_ENDPOINT, PUBLIC_API_KEY);
$query = new Query('install sdk');

$result = $client->search($query);
$result = $client->search(
(new Query())
->term('red shoes')
->mode('fulltext')
->limit(10)
);

$this->assertArrayHasKey('hits', $result);
$this->assertArrayHasKey('elapsed', $result);
Expand Down
44 changes: 28 additions & 16 deletions tests/Unit/QueryTest.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
<?php

use OramaCloud\Query;
use OramaCloud\QueryParams\Where;

test('query params', function () {
test('configure query params', function () {
$term = 'mock-term';
$mode = 'mock-mode';

$query = new Query();
$query->setTerm($term);
$query->setMode($mode);
$query
->term('red shoes')
->mode('fulltext')
->where('price', 'gte', 99.99)
->where('category', 'eq', 'shoes');

$result = $query->toArray();

$this->assertEquals($term, $result['term']);
$this->assertEquals($mode, $result['mode']);
$this->assertEquals($result['term'], 'red shoes');
$this->assertEquals($result['mode'], 'fulltext');
$this->assertEquals($result['where'], [
'category' => [
'eq' => 'shoes'
],
'price' => [
'gte' => 99.99
]
]);
});

test('default query params from array', function () {
Expand All @@ -22,23 +34,26 @@
$this->assertEquals($query->toArray(), [
'term' => '',
'mode' => 'fulltext',
'limit' => 5,
'offset' => 0
]);
});

test('query params from array', function () {
$params = [
'term' => 'mock-term',
'mode' => 'mock-mode'
'mode' => 'mock-mode',
'where' => [
'foo' => [
'eq' => 99
],
'bar' => [
'gt' => 10
]
]
];

$query = Query::fromArray($params);

$this->assertEquals(array_merge($params, [
'limit' => 5,
'offset' => 0
]), $query->toArray());
$this->assertEquals($params, $query->toArray());
});

test('query params as json', function () {
Expand All @@ -49,8 +64,5 @@

$query = Query::fromArray($params);

$this->assertEquals(array_merge($params, [
'limit' => 5,
'offset' => 0
]), json_decode($query->toJson(), true));
$this->assertEquals($params, json_decode($query->toJson(), true));
});

0 comments on commit 831c5a3

Please sign in to comment.