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

Commit 7e3eb6d

Browse files
author
maxiloc
authored
Merge pull request #69 from algolia/add-synonyms-api
add synonyms api
2 parents be5746c + 2b8f283 commit 7e3eb6d

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,39 @@ You can propagate (save) the settings to algolia using the `setSetting` method:
113113
Contact::setSettings();
114114
```
115115

116+
#### Synonyms
117+
118+
Synonyms are used to tell the engine about words or expressions that should be considered equal in regard to the textual relevance.
119+
120+
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.
121+
122+
You can use the synonyms API by adding a `synonyms` in `$algoliaSettings` class property like this:
123+
124+
```php
125+
use Illuminate\Database\Eloquent\Model;
126+
127+
class Contact extends Model
128+
{
129+
use AlgoliaEloquentTrait;
130+
131+
public $algoliaSettings = [
132+
'synonyms' => [
133+
[
134+
'objectID' => 'red-color',
135+
'type' => 'synonym',
136+
'synonyms' => ['red', 'another red', 'yet another red']
137+
]
138+
]
139+
];
140+
}
141+
```
142+
143+
You can propagate (save) the settings to algolia using the `setSetting` method:
144+
145+
```php
146+
Contact::setSettings();
147+
```
148+
116149
## Frontend Search (realtime experience)
117150

118151
Traditional search implementations tend to have search logic and functionality on the backend. This made sense when the search experience consisted of a user entering a search query, executing that search, and then being redirected to a search result page.

src/AlgoliaEloquentTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ public function _setSettings($setToTmpIndices = false)
170170
}, $settings['slaves']);
171171
}
172172

173+
if (isset($settings['synonyms'])) {
174+
$index->batchSynonyms($settings['synonyms'], true, true);
175+
}
176+
173177
if (count(array_keys($settings)) > 0) {
174178
$index->setSettings($settings);
175179
}

tests/AlgoliaEloquentTraitTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use AlgoliaSearch\Tests\Models\Model2;
66
use AlgoliaSearch\Tests\Models\Model4;
77
use AlgoliaSearch\Tests\Models\Model6;
8+
use AlgoliaSearch\Tests\Models\Model10;
89
use Illuminate\Support\Facades\App;
910
use Mockery;
1011
use Orchestra\Testbench\TestCase;
@@ -86,6 +87,38 @@ public function testSetSettings()
8687
$model6->setSettings();
8788
}
8889

90+
public function testSetSynonyms()
91+
{
92+
$index = Mockery::mock('\AlgoliaSearch\Index');
93+
$index->shouldReceive('batchSynonyms')->with(
94+
[
95+
[
96+
'objectID' => 'red-color',
97+
'type' => 'synonym',
98+
'synonyms' => [
99+
'red',
100+
'really red',
101+
'much red'
102+
]
103+
]
104+
],
105+
true,
106+
true
107+
);
108+
$index->shouldReceive('setSettings');
109+
110+
/** @var \AlgoliaSearch\Laravel\ModelHelper $realModelHelper */
111+
$realModelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
112+
$modelHelper = Mockery::mock('\AlgoliaSearch\Laravel\ModelHelper');
113+
App::instance('\AlgoliaSearch\Laravel\ModelHelper', $modelHelper);
114+
$model10 = new Model10();
115+
$modelHelper->shouldReceive('getSettings')->andReturn($realModelHelper->getSettings($model10));
116+
$modelHelper->shouldReceive('getIndices')->andReturn([$index]);
117+
$modelHelper->shouldReceive('getSlavesSettings')->andReturn($realModelHelper->getSlavesSettings($model10));
118+
119+
$this->assertEquals(null, $model10->setSettings());
120+
}
121+
89122
public function tearDown()
90123
{
91124
Mockery::close();

tests/Models/Model10.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace AlgoliaSearch\Tests\Models;
4+
use AlgoliaSearch\Laravel\AlgoliaEloquentTrait;
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Model10 extends Model
8+
{
9+
use AlgoliaEloquentTrait;
10+
public $algoliaSettings = [
11+
'synonyms' => [
12+
[
13+
'objectID' => 'red-color',
14+
'type' => 'synonym',
15+
'synonyms' => [
16+
'red',
17+
'really red',
18+
'much red'
19+
]
20+
]
21+
]
22+
];
23+
}

0 commit comments

Comments
 (0)