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

Commit

Permalink
- Fix/Handle building filters (where) and extending it to support pas…
Browse files Browse the repository at this point in the history
…sing 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.
  • Loading branch information
AbdullahFaqeir committed Oct 2, 2021
1 parent cfa34d6 commit 17064cb
Show file tree
Hide file tree
Showing 7 changed files with 465 additions and 49 deletions.
7 changes: 7 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2020 Devloops LLC

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.
129 changes: 115 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[![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)
[![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)

[![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)


# Laravel Scout Typesense Engine

Typesense engine for laravel/scout https://github.com/typesense/typesense .

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


## Installation

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

After you have installed scout and the Typesense driver, you need to add the
`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`:
`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`:

```php
<?php
Expand Down Expand Up @@ -148,26 +146,129 @@ Then, sync the data with the search service like:

After that you can search your models with:

`Post::search('Bugs Bunny')->get();`
```php
$search = Post::search('Bugs Bunny');
```

### Or

```php
$search = Post::search('Bugs Bunny',function (\Laravel\Scout\Builder $builder,\Typesense\Documents $documents, string $query, array $params){
return $documents->search($params);
});
```

Then you can apply your where(s) to the builder as follows :

```php

//This way the default operator := will be used
$search->where('created_at', now()->unix());

//Or specially for typesense engine you can add typesense operator to the where statement
$search->where('created_at', [
'>=',
now()->unix()
]);

```

*Note : For geolocation search, make sure to send an empty operator as follows

```php
$search->where('location', [
'',
[
48.86093481609114,
2.33698396872901
]
]);

```

## Extended/Added methods to Scout Builder

#### Check [Typesense Search](https://typesense.org/docs/0.21.0/api/documents.html#search) for reference.

- Group by

```php
$search->groupBy(['name', 'created_at'])
//or
$search->groupBy('name', 'created_at')
```

- Order

```php
$search->orderBy('name','desc')
```

- Location Order

```php
$search->orderByLocation('location',48.853, 2.344, '1km')
//or
$search->orderByLocation('location',48.853, 2.344, '1mi')

//to allow ordering with other columns
$search->orderByLocation('location',48.853, 2.344, '1mi','asc',true)
```

- Group by limit

```php
$search->groupByLimit(200)
```

- Highlight start tag

```php
$search->setHighlightStartTag('<strong>')
```

- Highlight end tag

```php
$search->setHighlightEndTag('<end>')
```

- Hits limit

```php
$search->limitHits(200)
```

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

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

// You may also add record via collection...
`$post->searchable();`
```php
$post = Post::find(1);
```

// OR
### You may also add record via collection...

`$posts = Post::where('year', '>', '2018')->get();`
```php
$post->searchable();
```

// You may also add records via collections...
`$posts->searchable();`
#### ---- OR

```php
$posts = Post::where('year', '>', '2018')->get();
```

You may also add records via collections...

```php
$posts->searchable();
```

## Author

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

## License

Expand Down
44 changes: 44 additions & 0 deletions src/Classes/TypesenseDocumentIndexResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Devloops\LaravelTypesense\Classes;

/**
* Class TypesenseDocumentIndexResponse
*
* @package Devloops\LaravelTypesense\Classes
* @date 02/10/2021
* @author Abdullah Al-Faqeir <abdullah@devloops.net>
*/
class TypesenseDocumentIndexResponse
{

public function __construct(private bool $success, private ?string $error = null, private ?array $document = null)
{
}

/**
* @return bool
*/
public function isSuccess(): bool
{
return $this->success;
}

/**
* @return string|null
*/
public function getError(): ?string
{
return $this->error;
}

/**
* @return array|null
*/
public function getDocument(): ?array
{
return $this->document;
}


}
Loading

0 comments on commit 17064cb

Please sign in to comment.