Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Commit 17064cb

Browse files
- Fix/Handle building filters (where) and extending it to support passing operators.
- Fix/Handle building orders. - Fix import to report results which helps to find failed documents. - Added support `orderByLocation()` to scout builder using macros. - Added support `groupBy()` to scout builder using macros. - Added support `groupByLimit()` to scout builder using macros. - Added support `setHighlightStartTag()` to scout builder using macros. - Added support `setHighlightEndTag()` to scout builder using macros. - Added support `limitHits()` to scout builder using macros. - Added missing license file. - Updated readme with more examples.
1 parent cfa34d6 commit 17064cb

File tree

7 files changed

+465
-49
lines changed

7 files changed

+465
-49
lines changed

LICENSE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2020 Devloops LLC
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 115 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
[![Latest Version on Packagist](https://img.shields.io/packagist/v/devloopsnet/laravel-typesense.svg?style=for-the-badge)](https://packagist.org/packages/devloopsnet/laravel-typesense) ![Postcardware](https://img.shields.io/badge/Postcardware-%F0%9F%92%8C-197593?style=for-the-badge)
1+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/devloopsnet/laravel-typesense.svg?style=for-the-badge)](https://packagist.org/packages/devloopsnet/laravel-typesense) ![Postcardware](https://img.shields.io/badge/Postcardware-%F0%9F%92%8C-197593?style=for-the-badge)
22

33
[![PHP from Packagist](https://img.shields.io/packagist/php-v/devloopsnet/laravel-typesense?style=flat-square)](https://packagist.org/packages/devloopsnet/laravel-typesense) [![Total Downloads](https://img.shields.io/packagist/dt/devloopsnet/laravel-typesense.svg?style=flat-square)](https://packagist.org/packages/devloopsnet/laravel-typesense)
44

5-
65
# Laravel Scout Typesense Engine
6+
77
Typesense engine for laravel/scout https://github.com/typesense/typesense .
88

99
<p align="center">
@@ -19,7 +19,6 @@ This package makes it easy to add full text search support to your models with L
1919
- [Author](#author)
2020
- [License](#license)
2121

22-
2322
## Installation
2423

2524
You can install the package via composer:
@@ -86,8 +85,7 @@ In your `config/scout.php` add:
8685
## Usage
8786

8887
After you have installed scout and the Typesense driver, you need to add the
89-
`Searchable` trait to your models that you want to make searchable. Additionaly,
90-
define the fields you want to make searchable by defining the `toSearchableArray` method on the model and implement `TypesenseSearch`:
88+
`Searchable` trait to your models that you want to make searchable. Additionaly, define the fields you want to make searchable by defining the `toSearchableArray` method on the model and implement `TypesenseSearch`:
9189

9290
```php
9391
<?php
@@ -148,26 +146,129 @@ Then, sync the data with the search service like:
148146

149147
After that you can search your models with:
150148

151-
`Post::search('Bugs Bunny')->get();`
149+
```php
150+
$search = Post::search('Bugs Bunny');
151+
```
152+
153+
### Or
154+
155+
```php
156+
$search = Post::search('Bugs Bunny',function (\Laravel\Scout\Builder $builder,\Typesense\Documents $documents, string $query, array $params){
157+
return $documents->search($params);
158+
});
159+
```
160+
161+
Then you can apply your where(s) to the builder as follows :
162+
163+
```php
164+
165+
//This way the default operator := will be used
166+
$search->where('created_at', now()->unix());
167+
168+
//Or specially for typesense engine you can add typesense operator to the where statement
169+
$search->where('created_at', [
170+
'>=',
171+
now()->unix()
172+
]);
173+
174+
```
175+
176+
*Note : For geolocation search, make sure to send an empty operator as follows
177+
178+
```php
179+
$search->where('location', [
180+
'',
181+
[
182+
48.86093481609114,
183+
2.33698396872901
184+
]
185+
]);
186+
187+
```
188+
189+
## Extended/Added methods to Scout Builder
190+
191+
#### Check [Typesense Search](https://typesense.org/docs/0.21.0/api/documents.html#search) for reference.
192+
193+
- Group by
194+
195+
```php
196+
$search->groupBy(['name', 'created_at'])
197+
//or
198+
$search->groupBy('name', 'created_at')
199+
```
200+
201+
- Order
202+
203+
```php
204+
$search->orderBy('name','desc')
205+
```
206+
207+
- Location Order
208+
209+
```php
210+
$search->orderByLocation('location',48.853, 2.344, '1km')
211+
//or
212+
$search->orderByLocation('location',48.853, 2.344, '1mi')
213+
214+
//to allow ordering with other columns
215+
$search->orderByLocation('location',48.853, 2.344, '1mi','asc',true)
216+
```
217+
218+
- Group by limit
219+
220+
```php
221+
$search->groupByLimit(200)
222+
```
223+
224+
- Highlight start tag
225+
226+
```php
227+
$search->setHighlightStartTag('<strong>')
228+
```
229+
230+
- Highlight end tag
231+
232+
```php
233+
$search->setHighlightEndTag('<end>')
234+
```
235+
236+
- Hits limit
237+
238+
```php
239+
$search->limitHits(200)
240+
```
152241

153242
## Adding via Query
154-
The `searchable()` method will chunk the results of the query and add the records to your search index.
155243

156-
`$post = Post::find(1);`
244+
The `searchable()` method will chunk the results of the query and add the records to your search index.
157245

158-
// You may also add record via collection...
159-
`$post->searchable();`
246+
```php
247+
$post = Post::find(1);
248+
```
160249

161-
// OR
250+
### You may also add record via collection...
162251

163-
`$posts = Post::where('year', '>', '2018')->get();`
252+
```php
253+
$post->searchable();
254+
```
164255

165-
// You may also add records via collections...
166-
`$posts->searchable();`
256+
#### ---- OR
257+
258+
```php
259+
$posts = Post::where('year', '>', '2018')->get();
260+
```
261+
262+
You may also add records via collections...
263+
264+
```php
265+
$posts->searchable();
266+
```
167267

168268
## Author
169269

170270
- [Abdullah Al-Faqeir](https://github.com/abdullahfaqeir)
271+
- [Contributors](https://github.com/devloopsnet/laravel-scout-typesense-engine/graphs/contributors)
171272

172273
## License
173274

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Devloops\LaravelTypesense\Classes;
4+
5+
/**
6+
* Class TypesenseDocumentIndexResponse
7+
*
8+
* @package Devloops\LaravelTypesense\Classes
9+
* @date 02/10/2021
10+
* @author Abdullah Al-Faqeir <[email protected]>
11+
*/
12+
class TypesenseDocumentIndexResponse
13+
{
14+
15+
public function __construct(private bool $success, private ?string $error = null, private ?array $document = null)
16+
{
17+
}
18+
19+
/**
20+
* @return bool
21+
*/
22+
public function isSuccess(): bool
23+
{
24+
return $this->success;
25+
}
26+
27+
/**
28+
* @return string|null
29+
*/
30+
public function getError(): ?string
31+
{
32+
return $this->error;
33+
}
34+
35+
/**
36+
* @return array|null
37+
*/
38+
public function getDocument(): ?array
39+
{
40+
return $this->document;
41+
}
42+
43+
44+
}

0 commit comments

Comments
 (0)