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

Commit fa639a4

Browse files
committed
Merge branch 'master' of github.com:algolia/algoliasearch-laravel
2 parents ce8794e + 5d91565 commit fa639a4

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor
22
composer.lock
33
phpunit.xml
4+
*.sublime*

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ class Contact extends Model
8989
}
9090
```
9191

92+
After setting up your model, you need to manually do the initial import of your data. You can do this by calling `reindex` on your model class. Using our previous example, this would be:
93+
94+
```php
95+
Contact::reindex();
96+
```
97+
9298
### Ranking & Relevance
9399

94100
We provide many ways to configure your index settings to tune the overall relevancy but the most important ones are the **searchable attributes** and the attributes reflecting the **record popularity**. You can configure them with the following code:
@@ -396,6 +402,17 @@ To keep settings that you set on the Algolia dashboard when reindexing and setti
396402
Contact::reindex(true, true, true);
397403
```
398404

405+
To implement a callback that gets called everytime a batch of entities is indexed:
406+
407+
```php
408+
Contact::reindex(true, true, false, function ($entities)
409+
{
410+
foreach ($entities as $entity)
411+
{
412+
var_dump($entity->id); // Contact::$id
413+
}
414+
});
415+
```
399416

400417
### Clearing an Index
401418

src/AlgoliaEloquentTrait.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ trait AlgoliaEloquentTrait
1818
* @param bool $setSettings
1919
* @param bool $mergeOldSettings
2020
*/
21-
public function _reindex($safe = true, $setSettings = true, $mergeOldSettings = false)
21+
public function _reindex($safe = true, $setSettings = true, $mergeOldSettings = false, \Closure $onInsert = null)
2222
{
2323
/** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
2424
$modelHelper = App::make('\AlgoliaSearch\Laravel\ModelHelper');
@@ -31,18 +31,27 @@ public function _reindex($safe = true, $setSettings = true, $mergeOldSettings =
3131
$this->_setSettings($setToTmpIndices, $mergeOldSettings);
3232
}
3333

34-
static::chunk(100, function ($models) use ($indicesTmp, $modelHelper) {
34+
static::chunk(100, function ($models) use ($indicesTmp, $modelHelper, $onInsert) {
3535
/** @var \AlgoliaSearch\Index $index */
3636
foreach ($indicesTmp as $index) {
37-
$records = [];
37+
$records = [];
38+
$recordsAsEntity = [];
3839

3940
foreach ($models as $model) {
4041
if ($modelHelper->indexOnly($model, $index->indexName)) {
4142
$records[] = $model->getAlgoliaRecordDefault($index->indexName);
43+
44+
if ($onInsert && is_callable($onInsert)) {
45+
$recordsAsEntity[] = $model;
46+
}
4247
}
4348
}
4449

4550
$index->addObjects($records);
51+
52+
if ($onInsert && is_callable($onInsert)) {
53+
call_user_func_array($onInsert, [$recordsAsEntity]);
54+
}
4655
}
4756

4857
});

src/EloquentSubscriber.php

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public function saved($model)
2121
foreach ($this->modelHelper->getIndices($model) as $index) {
2222
if ($this->modelHelper->indexOnly($model, $index->indexName)) {
2323
$index->addObject($this->modelHelper->getAlgoliaRecord($model, $index->indexName), $this->modelHelper->getObjectId($model));
24+
} elseif ($this->modelHelper->wouldBeIndexed($model, $index->indexName)) {
25+
$index->deleteObject($this->modelHelper->getObjectId($model));
2426
}
2527
}
2628

src/ModelHelper.php

+13
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ private function hasAlgoliaTrait(Model $class, $autoload = false)
4545
return (isset($traits['AlgoliaSearch\Laravel\AlgoliaEloquentTrait']));
4646
}
4747

48+
public function wouldBeIndexed(Model $model, $index_name)
49+
{
50+
if (! method_exists($model, 'indexOnly')) {
51+
return false;
52+
}
53+
54+
$cloned = clone $model;
55+
56+
$cloned->setRawAttributes($cloned->getOriginal());
57+
58+
return $cloned->indexOnly($index_name) === true;
59+
}
60+
4861
public function isAutoIndex(Model $model)
4962
{
5063
return ($this->hasAlgoliaTrait($model) && $model->autoIndex());

0 commit comments

Comments
 (0)