Skip to content

Commit 9e958b8

Browse files
committed
2.1.0 - add new raw query method
1 parent 1047e93 commit 9e958b8

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

src/MeilisearchQuery.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,32 @@ public function search(string $term = ''): \Laravel\Scout\Builder
9797
return $this->model::search($term, $this->callback());
9898
}
9999

100+
/**
101+
* Add a raw "AND" Meilisearch query to the builder.
102+
*/
103+
public function whereRaw(string $query): self
104+
{
105+
return $this->raw($query);
106+
}
107+
108+
/**
109+
* Add a raw "OR" Meilisearch query to the builder.
110+
*/
111+
public function orWhereRaw(string $query): self
112+
{
113+
return $this->raw($query, 'OR');
114+
}
115+
116+
/**
117+
* Add raw expression to the builder.
118+
*/
119+
protected function raw(string $query, string $boolean = 'AND'): self
120+
{
121+
$this->segments[] = new RawExpression($query, $boolean, empty($this->segments));
122+
123+
return $this;
124+
}
125+
100126
/**
101127
* Ensure that the for() method has been called before proceeding.
102128
*

src/RawExpression.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Chr15k\MeilisearchAdvancedQuery;
4+
5+
use Chr15k\MeilisearchAdvancedQuery\Contracts\QuerySegment;
6+
7+
class RawExpression implements QuerySegment
8+
{
9+
public function __construct(
10+
public string $rawQuery,
11+
public string $boolean = 'AND',
12+
public bool $init = false
13+
) {}
14+
15+
public function compile(): string
16+
{
17+
return trim(sprintf('%s %s', $this->init ? '' : $this->boolean, $this->rawQuery));
18+
}
19+
}

tests/MeilisearchQueryTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,54 @@ public function testMultipleSort()
223223

224224
$this->assertSame(['name:desc', 'email:asc'], $instance['sort']);
225225
}
226+
227+
public function testBasicRawQuery()
228+
{
229+
$compiled = MeilisearchQuery::for(User::class)->whereRaw("name = 'Chris'")->compile();
230+
231+
$this->assertSame("name = 'Chris'", $compiled);
232+
}
233+
234+
public function testBasicMultipleRawOrQuery()
235+
{
236+
$compiled = MeilisearchQuery::for(User::class)
237+
->whereRaw("name = 'Chris'")
238+
->orWhereRaw("name = 'Bob'")
239+
->compile();
240+
241+
$this->assertSame("name = 'Chris' OR name = 'Bob'", $compiled);
242+
}
243+
244+
public function testBasicRawAndQuery()
245+
{
246+
$compiled = MeilisearchQuery::for(User::class)
247+
->whereRaw("name = 'Chris'")
248+
->whereRaw("name = 'Bob'")
249+
->compile();
250+
251+
$this->assertSame("name = 'Chris' AND name = 'Bob'", $compiled);
252+
}
253+
254+
public function testSingleBasicRawQuery()
255+
{
256+
$compiled = MeilisearchQuery::for(User::class)
257+
->whereRaw("name = 'Chris' OR name = 'Bob'")
258+
->compile();
259+
260+
$this->assertSame("name = 'Chris' OR name = 'Bob'", $compiled);
261+
}
262+
263+
public function testMixedRawNestedQuery()
264+
{
265+
$compiled = MeilisearchQuery::for(User::class)
266+
->where('email', '[email protected]')
267+
->where(fn ($query) => $query
268+
->whereRaw("name = 'Chris'")
269+
->orWhereRaw("name = 'Bob'")
270+
)
271+
->where('verified', true)
272+
->compile();
273+
274+
$this->assertSame("email = '[email protected]' AND (name = 'Chris' OR name = 'Bob') AND verified = 'true'", $compiled);
275+
}
226276
}

0 commit comments

Comments
 (0)