Skip to content

Commit 1e1f32a

Browse files
Edit doc
1 parent ad9c6b3 commit 1e1f32a

File tree

1 file changed

+37
-66
lines changed

1 file changed

+37
-66
lines changed

README.md

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,39 @@ The point is to avoid pull data from the database and building the collection.
3434
$nbHits = Model::search('query')->count();
3535
```
3636

37-
## `aroundLatLng`
37+
### `hydrate`
3838

39-
The`aroundLatLng` method will add [geolocation parameter](1) to the search request. You
40-
can define a point with its coordinate.
39+
The `hydrate` method is similar to the standard get() method, except it hydrates the models from your Algolia index.
4140

42-
```php
43-
// Models around Paris latitude and longitude
44-
Model::search('query')->aroundLatLng(48.8588536, 2.3125377)->get();
45-
```
41+
By default, Scout will use the IDs of the results from Algolia and pull the data from the local database to create the collection.
4642

47-
Where clauses can also be added
43+
Hence, `hydrate` will be much faster than `get`.
4844

49-
```php
50-
Model::search('query')
51-
->aroundLatLng(48.8588536, 2.3125377)
52-
->where('something_id', 1)
53-
->get();
54-
```
5545

46+
**Note**: By using this method you must be sure that you are correctly keeping your algolia index in sync with your database
47+
to avoid populating stale data.
48+
49+
#### Restrict attributes
5650

57-
## `with`
51+
By default, this method will add all attributes from Algolia's record to your model.
52+
53+
If you want to remove sensitive or irrelevant data from your model, you can set a list of retrievable attributes in your Algolia dashboard. In this case, Algolia will only return these attributes while still searching every `searchableAttributes`.
54+
55+
### `with`
5856

5957
The `with` method gives you complete access to the Algolia options parameter. This allows you
6058
to customise the search parameters exactly the same as you would using the algolia php library directly.
6159

60+
#### Simple example
61+
62+
```php
63+
$result = Model::search('')
64+
->with(['hitsPerPage' => 30])
65+
->get();
66+
```
67+
68+
#### Advanced example
69+
6270
```php
6371

6472
$filters = [
@@ -83,65 +91,28 @@ $result = Model::search('')->with($params)->get();
8391
```
8492

8593

86-
## `hydrate`
87-
88-
The `hydrate` method is similar to the standard get() method, except it hydrates the models from your Algolia index
89-
instead of of using the objects keys to pull the related models from your database, meaning much quicker response times.
94+
### `aroundLatLng`
9095

91-
This also gives you the ability to overide the fill() method on any model to use the additional data that you store
92-
in your indexes.
93-
94-
Note: By using this method you must be sure that you are correctly keeping your algolia index in sync with your database
95-
to avoid populating stale data.
96+
The`aroundLatLng` method will add [geolocation parameter](1) to the search request. You
97+
can define a point with its coordinate.
9698

97-
Example model with additional data from Algolia Index being populated:
99+
Note that this method is pure syntactic sugar, you can use `with` to specify more location details (like radius for instance)
98100

99101
```php
102+
// Models around Paris latitude and longitude
103+
Model::search('query')->aroundLatLng(48.8588536, 2.3125377)->get();
104+
```
100105

101-
class ExampleModel extends Model
102-
{
103-
use Searchable;
104-
105-
protected $appends = [
106-
'rankingInfo' //Add rankingInfo when converted to array
107-
];
108-
109-
protected $rankingInfo = [];
110-
protected $highlightResult = [];
111-
112-
/**
113-
* Adds the ranking & highlight results from the search request to get search score/geo distance etc
114-
*
115-
* @param array $attributes
116-
* @return mixed
117-
*/
118-
public function fill(array $attributes)
119-
{
120-
121-
if (isset($attributes['_rankingInfo']))
122-
{
123-
$this->setRankingInfo($attributes['_rankingInfo']);
124-
}
125-
126-
//Add any additional data stored in algolia as required
127-
128-
return parent::fill($attributes);
129-
}
130-
131-
public function getRankingInfoAttribute(): array
132-
{
133-
return $this->rankingInfo;
134-
}
135-
136-
public function setRankingInfo(array $rankingInfo)
137-
{
138-
$this->rankingInfo = $rankingInfo;
139-
}
140-
141-
$result = ExampleModel::search('')->with($params)->get();
106+
Where clauses can also be added
142107

108+
```php
109+
Model::search('query')
110+
->aroundLatLng(48.8588536, 2.3125377)
111+
->where('something_id', 1)
112+
->get();
143113
```
144114

115+
145116
## Contributing
146117

147118
Feel free to open an issue to request a macro.

0 commit comments

Comments
 (0)