Skip to content

Commit 1f5af7a

Browse files
committed
Update README.md
1 parent f5b883d commit 1f5af7a

File tree

1 file changed

+159
-2
lines changed

1 file changed

+159
-2
lines changed

README.md

Lines changed: 159 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,159 @@
1-
# search-builder
2-
A package to build fast, index-friendly search queries for Laravel
1+
# Search Builder for Laravel
2+
A package to build fast, index-friendly search queries for Laravel.
3+
4+
## What does it do?
5+
The purpose of this package is to allow you to build multi-condition search queries with scores, and make use of covering indexes to be super fast.
6+
This means simply using this package as shown is not everything – you need to have a well designed database, with properly designed indexes for your searches, to get good results. To learn more about this topic, we recommend watching the free [PlanetScale MySQL for Developers course](https://planetscale.com/courses/mysql-for-developers/introduction/course-introduction).
7+
8+
## Installation
9+
10+
```bash
11+
composer require whitecube/laravel-search-builder
12+
```
13+
14+
## Usage
15+
16+
### How to structure your query
17+
18+
To make your search queries extremely fast, the search builder will pack all of your conditions in a subquery that will aim to hit as many covering indexes as possible, in order to build an aggregated table that only contains ids of the pertinent models (along with a score, more on this later). This aggregated table will then be used to filter down the actual table with an inner join. This means that the processing of your search logic is done entirely on your indexes, and the full table is only accessed at the end, which dramatically speeds everything up.
19+
20+
However, the package can not detect your database structure, so it is your responsibility to create your indexes correctly, and in such a way that your search condition queries will not have to access your main tables' data.
21+
22+
Here's an example of what we're looking to achieve, in raw SQL. Given we have a products table, and we want to search it by reference and by name, and prioritise the reference over the name:
23+
24+
```sql
25+
with id_and_total_score as (
26+
select id, sum(score) as score from (
27+
-- This query makes use of a covering index on the ref column
28+
select id, 100 as score
29+
from products
30+
where ref = 'SEARCH_STRING'
31+
32+
union all
33+
34+
-- This query makes use of a covering index on the name column
35+
select id, 50 as score
36+
from products
37+
where name = 'SEARCH_STRING'
38+
)
39+
as ids_and_scores
40+
group by id
41+
)
42+
43+
select * from products
44+
inner join id_and_total_score on id_and_total_score.id = products.id
45+
order by score desc;
46+
```
47+
48+
### The search builder instance
49+
50+
You can get a search builder instance just by passing it the model you want to search.
51+
52+
```php
53+
use \App\Models\Product;
54+
use \Whitecube\SearchBuilder\SearchBuilder;
55+
56+
$builder = new SearchBuilder(Product::class); // You can also pass it an instance of your model
57+
```
58+
59+
Or, if your model uses the `HasSearchBuilder` trait, you can easily get a search builder instance this way, which allows you to cleanly chain your condition methods later.
60+
61+
```php
62+
use Whitecube\SearchBuilder\HasSearchBuilder;
63+
64+
class Product extends Model
65+
{
66+
use HasSearchBuilder;
67+
}
68+
```
69+
70+
```php
71+
$builder = Product::searchBuilder();
72+
```
73+
74+
### Defining search conditions
75+
76+
Once you have a search builder instance, you can use it to define your search conditions, by passing eloquent builder instances to the `search` method.
77+
78+
```php
79+
Product::searchBuilder()
80+
->search(Product::select('id')->where('ref', 'SEARCH_STRING'), score: 100)
81+
->search(Product::select('id')->where('name', 'SEARCH_STRING'), score: 50);
82+
```
83+
84+
The score is optional and will be automatically computed if missing, using the order in which the conditions are defined, with the highest score on top.
85+
86+
```php
87+
Product::searchBuilder()
88+
->search(Product::select('id')->where('ref', 'SEARCH_STRING'), score: 100) // score = 100
89+
->search(Product::select('id')->where('name', 'SEARCH_STRING')) // score = 3
90+
->search(Product::select('id')->where('description', 'SEARCH_STRING')) // score = 2
91+
->search(Product::select('id')->where('content', 'SEARCH_STRING')); // score = 1
92+
```
93+
94+
You can easily search on related tables. Remember to only select the column that references the id of the table you're searching.
95+
96+
```php
97+
Product::searchBuilder()
98+
// Search on a related table
99+
->search(Lot::select('product_id')->where('barcode', 'SEARCH_STRING'))
100+
// Search on a relation of a related table
101+
->search(Lot::select('product_id')->whereHas('delivery', function ($query) {
102+
$query->where('address', 'SEARCH_STRING');
103+
}))
104+
```
105+
106+
If you wish to split the search terms on spaces, dashes, dots and underscores, and perform individual queries on each term, you can call the `splitTerms` method.
107+
108+
```php
109+
$terms = 'foo bar baz';
110+
111+
Product::searchBuilder()
112+
->splitTerms($terms, function (SearchBuilder $searchBuilder, string $term) {
113+
// Called once with $term = foo, once with $term = bar, and once with $term = baz
114+
return $searchBuilder->search(Product::select('id')->where('ref', $term));
115+
});
116+
```
117+
118+
### Getting the results
119+
120+
After defining your conditions, you can get the collection of results by calling the `get` method.
121+
122+
```php
123+
$results = Product::searchBuilder()
124+
->search(Product::select('id')->where('ref', 'SEARCH_STRING'), score: 100)
125+
->search(Product::select('id')->where('name', 'SEARCH_STRING'), score: 50)
126+
->get();
127+
```
128+
129+
Or, if you need to do more work on the query yourself, you can get the query builder instance.
130+
131+
```php
132+
$query = Product::searchBuilder()
133+
->search(Product::select('id')->where('ref', 'SEARCH_STRING'), score: 100)
134+
->search(Product::select('id')->where('name', 'SEARCH_STRING'), score: 50)
135+
->getQuery();
136+
```
137+
138+
## 💖 Sponsorships
139+
140+
If you are reliant on this package in your production applications, consider [sponsoring us](https://github.com/sponsors/whitecube)! It is the best way to help us keep doing what we love to do: making great open source software.
141+
142+
## Contributing
143+
144+
Feel free to suggest changes, ask for new features or fix bugs yourself. We're sure there are still a lot of improvements that could be made, and we would be very happy to merge useful pull requests.
145+
146+
Thanks!
147+
148+
### Unit tests
149+
150+
When adding a new feature or fixing a bug, please add corresponding unit tests. The current set of tests is limited, but every unit test added will improve the quality of the package.
151+
152+
Run PHPUnit by calling `composer test`.
153+
154+
## Made with ❤️ for open source
155+
156+
At [Whitecube](https://www.whitecube.be) we use a lot of open source software as part of our daily work.
157+
So when we have an opportunity to give something back, we're super excited!
158+
159+
We hope you will enjoy this small contribution from us and would love to [hear from you](mailto:[email protected]) if you find it useful in your projects. Follow us on [Twitter](https://twitter.com/whitecube_be) for more updates!

0 commit comments

Comments
 (0)