Skip to content
This repository was archived by the owner on Sep 11, 2018. It is now read-only.

Commit 0c78cdd

Browse files
author
maxiloc
authored
Merge pull request #4 from GiampaoloFalqui/readme-formatting-fix
Adds synonyms and improves slightly the README.md
2 parents ec38d4a + 29c0850 commit 0c78cdd

File tree

1 file changed

+60
-27
lines changed

1 file changed

+60
-27
lines changed

README.md

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use Illuminate\Database\Eloquent\Model;
6767
class Contact extends Model
6868
{
6969
use AlgoliaEloquentTrait;
70-
70+
7171
public function getAlgoliaRecord()
7272
{
7373
return array_merge($this->toArray(), [
@@ -87,20 +87,47 @@ use Illuminate\Database\Eloquent\Model;
8787
class Contact extends Model
8888
{
8989
use AlgoliaEloquentTrait;
90-
90+
9191
public $algoliaSettings = [
9292
'attributesToIndex' => [
93-
'id',
93+
'id',
9494
'name',
9595
],
9696
'customRanking' => [
97-
'desc(popularity)',
97+
'desc(popularity)',
9898
'asc(name)',
9999
],
100100
];
101101
}
102102
```
103103

104+
#### Synonyms
105+
106+
Synonyms are used to tell the engine about words or expressions that should be considered equal in regard to the textual relevance.
107+
108+
Our [synonyms API](https://www.algolia.com/doc/relevance/synonyms) has been designed to manage as easily as possible a large set of synonyms for an index and its slaves.
109+
110+
You can use the synonyms API by adding a `synonyms` in `$algoliaSettings` class property like this:
111+
112+
```php
113+
use Illuminate\Database\Eloquent\Model;
114+
115+
class Contact extends Model
116+
{
117+
use AlgoliaEloquentTrait;
118+
119+
public $algoliaSettings = [
120+
'synonyms' => [
121+
[
122+
'objectID' => 'red-color',
123+
'type' => 'synonym',
124+
'synonyms' => ['red', 'another red', 'yet another red']
125+
]
126+
]
127+
];
128+
}
129+
```
130+
104131
You can propagate (save) the settings to algolia using the `setSetting` method:
105132

106133
```php
@@ -131,26 +158,32 @@ You could also use the `search` method but it's not recommended to implement ins
131158
Contact::search('jon doe');
132159
```
133160

161+
You can also pass additional parameters to the search function:
162+
163+
```php
164+
Contact::search('jon doe', array('hitsPerPage' => 5));
165+
```
166+
134167
## Options
135168

136169
#### Auto-indexing & Asynchronism
137170

138171
Each time a record is saved; it will be - asynchronously - indexed. On the other hand, each time a record is destroyed, it will be - asynchronously - removed from the index.
139172

140173
You can disable the auto-indexing and auto-removing setting the following options:
141-
174+
142175
```php
143176
use Illuminate\Database\Eloquent\Model;
144177

145178
class Contact extends Model
146179
{
147180
use AlgoliaEloquentTrait;
148-
181+
149182
public static $autoIndex = false;
150183
public static $autoDelete = false;
151184
}
152185
```
153-
186+
154187
You can temporary disable auto-indexing. This is often used for performance reason.
155188

156189
```php
@@ -174,7 +207,7 @@ use Illuminate\Database\Eloquent\Model;
174207
class Contact extends Model
175208
{
176209
use AlgoliaEloquentTrait;
177-
210+
178211
public $indices = ['contact_all'];
179212
}
180213
```
@@ -189,7 +222,7 @@ use Illuminate\Database\Eloquent\Model;
189222
class Contact extends Model
190223
{
191224
use AlgoliaEloquentTrait;
192-
225+
193226
public static $perEnvironment = true; // Index name will be 'Contacts_{\App::environnement()}';
194227
}
195228
```
@@ -204,7 +237,7 @@ use Illuminate\Database\Eloquent\Model;
204237
class Contact extends Model
205238
{
206239
use AlgoliaEloquentTrait;
207-
240+
208241
public static $objectIdKey = 'new_key';
209242
}
210243
```
@@ -219,7 +252,7 @@ use Illuminate\Database\Eloquent\Model;
219252
class Contact extends Model
220253
{
221254
use AlgoliaEloquentTrait;
222-
255+
223256
public function indexOnly($index_name)
224257
{
225258
return (bool) $condition;
@@ -235,22 +268,22 @@ If you want to index records that didn't yet load any relations you can do it by
235268

236269
It will look like:
237270

238-
```
271+
```php
239272
public function getAlgoliaRecord()
240273
{
241274
/**
242275
* Load the categories relation so that it's available
243276
* in the laravel toArray method
244277
*/
245-
$this->categories;
246-
278+
$this->categories;
279+
247280
return $this->toArray();
248281
}
249282
```
250283

251-
In the resulted object you will have categories converted to array by Laravel. If you want a custom relation structure you will instead do something like :
284+
In the resulted object you will have categories converted to array by Laravel. If you want a custom relation structure you will instead do something like:
252285

253-
```
286+
```php
254287
public function getAlgoliaRecord()
255288
{
256289
/**
@@ -261,7 +294,7 @@ public function getAlgoliaRecord()
261294
$extra_data['categories'] = array_map(function ($data) {
262295
return $data['name'];
263296
}, $this->categories->toArray();
264-
297+
265298
return array_merge($this->toArray(), $extra_data);
266299
}
267300
```
@@ -287,6 +320,7 @@ And trigger the removing using the `removeFromIndex` instance method.
287320
$contact = Contact::firstOrCreate(['name' => 'Jean']);
288321
$contact->removeFromIndex();
289322
```
323+
290324
#### Reindexing
291325

292326
To *safely* reindex all your records (index to a temporary index + move the temporary index to the current one atomically), use the `reindex` class method:
@@ -305,7 +339,7 @@ Contact::reindex(false);
305339

306340
To clear an index, use the `clearIndices` class method:
307341

308-
```ruby
342+
```php
309343
Contact::clearIndices();
310344
```
311345

@@ -319,14 +353,14 @@ use Illuminate\Database\Eloquent\Model;
319353
class Contact extends Model
320354
{
321355
use AlgoliaEloquentTrait;
322-
356+
323357
public $algoliaSettings = [
324358
'attributesToIndex' => [
325-
'id',
359+
'id',
326360
'name',
327361
],
328362
'customRanking' => [
329-
'desc(popularity)',
363+
'desc(popularity)',
330364
'asc(name)',
331365
],
332366
'slaves' => [
@@ -367,12 +401,12 @@ use Illuminate\Database\Eloquent\Model;
367401
class Contact extends Model
368402
{
369403
use AlgoliaEloquentTrait;
370-
404+
371405
public $indices = [
372-
'contact_public',
406+
'contact_public',
373407
'contact_private',
374408
];
375-
409+
376410
public function indexOnly($indexName)
377411
{
378412
if ($indexName == 'contact_public')
@@ -392,7 +426,7 @@ Book::search('foo bar', ['index' => 'contacts_private']);
392426

393427
## Eloquent compatibility
394428

395-
Doing :
429+
Doing:
396430

397431
```
398432
Ad::where('id', $id)->update($attributes);
@@ -413,5 +447,4 @@ Compatible with 5.x applications
413447

414448
## License
415449

416-
Laravel Algolia Search is licensed under [The MIT License (MIT)](LICENSE).
417-
450+
Laravel Algolia Search is licensed under [The MIT License (MIT)](LICENSE).

0 commit comments

Comments
 (0)