Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring search package into monorepo #2064

Open
wants to merge 10 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/split_packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
package: ["admin", "core", "opayo", "paypal", "table-rate-shipping", "stripe", "meilisearch"]
package: ["admin", "core", "opayo", "paypal", "table-rate-shipping", "stripe", "meilisearch", "search"]
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
php: [8.3, 8.2]
laravel: [11.*, 10.*]
dependency-version: [prefer-stable]
testsuite: [core, admin, shipping, stripe]
testsuite: [core, admin, shipping, stripe, search]
include:
- laravel: 11.*
testbench: 9.*
Expand Down
31 changes: 20 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,25 @@
"filament/filament": "^3.2.25",
"filament/spatie-laravel-media-library-plugin": "^3.2",
"guzzlehttp/guzzle": "^7.3",
"http-interop/http-factory-guzzle": "^1.2",
"kalnoy/nestedset": "^v6.x-dev",
"laravel/framework": "^10.0|^11.0",
"laravel/scout": "^10.0",
"leandrocfe/filament-apex-charts": "^3.1.3",
"livewire/livewire": "^3.0",
"lukascivil/treewalker": "0.9.1",
"marvinosswald/filament-input-select-affix": "^0.2.0",
"meilisearch/meilisearch-php": "^v1.6.0",
"meilisearch/meilisearch-php": "^1.10",
"php": "^8.2",
"spatie/laravel-activitylog": "^4.4",
"spatie/laravel-blink": "^1.7",
"spatie/laravel-data": "^4.9.0",
"spatie/laravel-medialibrary": "^11.0.0",
"spatie/laravel-permission": "^6.4",
"spatie/php-structure-discoverer": "^2.0",
"stripe/stripe-php": "^14.4",
"technikermathe/blade-lucide-icons": "^v3.0"
"technikermathe/blade-lucide-icons": "^v3.0",
"typesense/typesense-php": "^4.9"
},
"require-dev": {
"larastan/larastan": "^2.9",
Expand All @@ -50,7 +53,8 @@
"autoload": {
"files": [
"packages/admin/src/helpers.php",
"packages/core/src/helpers.php"
"packages/core/src/helpers.php",
"packages/search/src/helpers.php"
],
"psr-4": {
"Lunar\\": "packages/core/src",
Expand All @@ -63,6 +67,7 @@
"Lunar\\Meilisearch\\": "packages/meilisearch/src/",
"Lunar\\Opayo\\": "packages/opayo/src/",
"Lunar\\Paypal\\": "packages/paypal/src/",
"Lunar\\Search\\": "packages/search/src/",
"Lunar\\Shipping\\": "packages/table-rate-shipping/src",
"Lunar\\Shipping\\Database\\Factories\\": "packages/table-rate-shipping/database/factories",
"Lunar\\Stripe\\": "packages/stripe/src/"
Expand All @@ -76,28 +81,31 @@
"Lunar\\Tests\\Paypal\\": "tests/paypal",
"Lunar\\Tests\\Shipping\\": "tests/shipping",
"Lunar\\Shipping\\Tests\\": "packages/table-rate-shipping/tests",
"Lunar\\Tests\\Stripe\\": "tests/stripe"
"Lunar\\Tests\\Stripe\\": "tests/stripe",
"Lunar\\Tests\\Search\\": "tests/search"
}
},
"extra": {
"lunar": {
"name": [
"Meilisearch",
"Table Rate Shipping",
"Opayo Payments",
"Search",
"Meilisearch",
"Paypal Payments",
"Stripe Payments",
"Table Rate Shipping"
"Stripe Payments"
]
},
"laravel": {
"providers": [
"Lunar\\Shipping\\ShippingServiceProvider",
"Lunar\\Stripe\\StripePaymentsServiceProvider",
"Lunar\\Paypal\\PaypalServiceProvider",
"Lunar\\Opayo\\OpayoServiceProvider",
"Lunar\\Meilisearch\\MeilisearchServiceProvider",
"Lunar\\LunarServiceProvider",
"Lunar\\Admin\\LunarPanelProvider"
"Lunar\\Search\\SearchServiceProvider",
"Lunar\\Admin\\LunarPanelProvider",
"Lunar\\Opayo\\OpayoServiceProvider",
"Lunar\\Shipping\\ShippingServiceProvider",
"Lunar\\LunarServiceProvider"
]
}
},
Expand All @@ -107,6 +115,7 @@
"lunarphp/meilisearch": "self.version",
"lunarphp/opayo": "self.version",
"lunarphp/paypal": "self.version",
"lunarphp/search": "self.version",
"lunarphp/stripe": "self.version",
"lunarphp/table-rate-shipping": "self.version"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/meilisearch/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"meilisearch/meilisearch-php": "^v1.6.0"
"meilisearch/meilisearch-php": "^1.10"
},
"autoload": {
"psr-4": {
Expand Down
99 changes: 99 additions & 0 deletions packages/search/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

## Lunar Search

This packages brings E-Commerce search to Lunar.
---

## Requirements
- Lunar >= 1.x

## License

Lunar is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

## Installation

### Require the composer package

```sh
composer require lunarphp/search
```

## Usage

### Configuration

Most configuration is done via `config/lunar/search.php`. Here you can specify which facets should be used and how they are displayed.

```php
'facets' => [
\Lunar\Models\Product::class => [
'brand' => [
'label' => 'Brand',
],
'colour' => [
'label' => 'Colour',
],
'size' => [
'label' => 'Size',
],
'shoe-size' => [
'label' => 'Shoe Size',
]
]
],
```

### Basic Search

At a basic level, you can search models using the provided facade.

```php
use Lunar\Search\Facades\Search;

// Search on a specific model
$results = Search::on(\Lunar\Models\Collection::class)->query('Hoodies')->get();

// Search on Lunar\Models\Product by default.
$results = Search::query('Hoodies')->get();
```

Under the hood this will detect what Scout driver is mapped under `lunar.search.engine_map` and
then perform a search using that given driver. To increase performance the results will not be
hydrated from the database, but instead will be the raw results from the search provider.


### Handling the response

Searching returns a `Lunar\Data\SearchResult` DTO which you can use in your templates:

```php
use Lunar\Search\Facades\Search;
$results = Search::query('Hoodies')->get();
```

```bladehtml
<!-- Loop through results-->
@foreach($results->hits as $hit)
{{ $hit->document['name'] }}
@endforeach

<!-- Loop through facets -->
@foreach($results->facets as $facet)
<span>
<strong>{{ $facet->label }}</strong>
@foreach($facet->values as $facetValue)
<input type="checkbox" value="{{ $facetValue->value }}" />
<span
@class([
'text-blue-500' => $facetValue->active,
])
>
{{ $facetValue->label }}
</span>
{{ $facetValue->count }}
@endforeach
</div>
@endforeach
```

47 changes: 47 additions & 0 deletions packages/search/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "lunarphp/search",
"type": "project",
"description": "Ecommerce search for LunarPHP",
"keywords": ["lunarphp", "laravel", "ecommerce", "e-commerce", "headless", "store", "shop", "search"],
"license": "MIT",
"authors": [
{
"name": "Lunar",
"homepage": "https://lunarphp.io/"
}
],
"require": {
"php": "^8.2",
"lunarphp/core": "self.version",
"spatie/laravel-data": "^4.9.0",
"typesense/typesense-php": "^4.9",
"meilisearch/meilisearch-php": "^1.10",
"http-interop/http-factory-guzzle": "^1.2"
},
"autoload": {
"files": [
"src/helpers.php"
],
"psr-4": {
"Lunar\\Search\\": "src/"
}
},
"extra": {
"lunar": {
"name": "Search"
},
"laravel": {
"providers": [
"Lunar\\Search\\SearchServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true,
"php-http/discovery": true
}
}
}
15 changes: 15 additions & 0 deletions packages/search/config/search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

return [
'facets' => [
\Lunar\Models\Product::class => [
'brand' => [],
// 'size' => [],
// 'colour' => [
// 'Red' => [
// 'hex_value' => '#FF0000',
// ],
// ],
],
],
];
5 changes: 5 additions & 0 deletions packages/search/src/Contracts/InstantSearchContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Lunar\Search\Contracts;

interface InstantSearchContract {}
5 changes: 5 additions & 0 deletions packages/search/src/Contracts/SearchManagerContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Lunar\Search\Contracts;

interface SearchManagerContract {}
17 changes: 17 additions & 0 deletions packages/search/src/Data/Builder/SearchQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Lunar\Search\Data\Builder;

use Spatie\LaravelData\Attributes\MapName;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Mappers\SnakeCaseMapper;

#[MapName(SnakeCaseMapper::class)]
class SearchQuery extends Data
{
public function __construct(
public ?string $query = '',
public array $facets = [],
public array $facetFilters = []
) {}
}
18 changes: 18 additions & 0 deletions packages/search/src/Data/SearchFacet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Lunar\Search\Data;

use Lunar\Search\Data\SearchFacet\FacetValue;
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Data;

class SearchFacet extends Data
{
public function __construct(
public string $label,
public string $field,
#[DataCollectionOf(FacetValue::class)]
public array $values,
public bool $hierarchy = false
) {}
}
18 changes: 18 additions & 0 deletions packages/search/src/Data/SearchFacet/FacetValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Lunar\Search\Data\SearchFacet;

use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Data;

class FacetValue extends Data
{
public function __construct(
public string $label,
public string $value,
public int $count = 0,
public bool $active = false,
#[DataCollectionOf(FacetValue::class)]
public array $children = []
) {}
}
16 changes: 16 additions & 0 deletions packages/search/src/Data/SearchHit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Lunar\Search\Data;

use Lunar\Search\Data\SearchHit\Highlight;
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Data;

class SearchHit extends Data
{
public function __construct(
#[DataCollectionOf(Highlight::class)]
public array $highlights,
public array $document,
) {}
}
15 changes: 15 additions & 0 deletions packages/search/src/Data/SearchHit/Highlight.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Lunar\Search\Data\SearchHit;

use Spatie\LaravelData\Data;

class Highlight extends Data
{
public function __construct(
public string $field,
/** @var array<string> */
public array $matches,
public ?string $snippet,
) {}
}
Loading
Loading