Skip to content

Commit 58dd85b

Browse files
committed
Release 1.1.0
1 parent dc0046a commit 58dd85b

File tree

5 files changed

+86
-33
lines changed

5 files changed

+86
-33
lines changed

README.md

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ ElasticLens for Laravel uses Elasticsearch to create and sync a searchable index
1818
</div>
1919
<div align="center">
2020
<img
21-
src="https://cdn.snipform.io/pdphilip/elasticlens/elasticlens-build.gif"
22-
alt="ElasticLens Build"
21+
src="https://cdn.snipform.io/pdphilip/elasticlens/lens-migrate.gif"
22+
alt="ElasticLens Migrate"
2323
/>
2424
</div>
2525

@@ -50,6 +50,7 @@ For Example, a base `User` Model will sync with an Elasticsearch `IndexedUser` M
5050
- [Manage Elasticsearch Migrations](#step-6-define-your-index-models-migrationmap): Define a required blueprint for your index migrations.
5151
- [Comprehensive CLI Tools](#step-7-monitor-and-administer-all-your-indexes-with-artisan-commands): Manage index health, migrate/rebuild indexes, and more with Artisan commands.
5252
- [Built-in IndexableBuildState model](#step-8-optionally-access-the-built-in-indexablebuildstate-model-to-track-index-build-states): Track the build states of your indexes.
53+
- [Built-in Migration Logs](#step-9-optionally-access-the-built-in-indexablemigrationlog-model-for-index-migration-status): Track the build states of your indexes.
5354

5455
# Requirements
5556

@@ -70,13 +71,15 @@ Publish config file and run the migrations with:
7071
php artisan lens:install
7172
```
7273

73-
# Usage (Walkthrough)
74+
# Usage
75+
76+
The Walkthrough Steps below will show all the features by way of eaxmple
7477

7578
## Step 1: Zero config setup
7679

7780
1. Add the Indexable Trait to Your Base Model:
7881

79-
Include the `Indexable` trait in your base model to enable automatic indexing.
82+
- Include the `Indexable` trait in your base model to enable automatic indexing.
8083

8184
```php
8285
use PDPhilip\ElasticLens\Indexable;
@@ -88,21 +91,21 @@ class User extends Eloquent implements Authenticatable, CanResetPassword
8891

8992
2. Create an Index Model for Your Base Model:
9093

91-
Define a corresponding Index Model that extends `IndexModel`. This model will sync and manage the Elasticsearch index for your `Base Model`.
92-
93-
By default, ElasticLens expects the `Index Model` to be named as `Indexed` + `BaseModelName` and located in the `App\Models\Indexes` directory. For example:
94-
95-
`App\Models\Indexes\IndexedUser.php`
94+
- Define a corresponding Index Model that extends `IndexModel`. This model will sync and manage the Elasticsearch index for your `Base Model`.
95+
- By default, ElasticLens expects the `Index Model` to be named as `Indexed` + `BaseModelName` and located in the `App\Models\Indexes` directory.
96+
- For example: `App\Models\Indexes\IndexedUser.php`
9697

9798
```php
9899
namespace App\Models\Indexes;
99100

100101
use PDPhilip\ElasticLens\IndexModel;
101102

102103
class IndexedUser extends IndexModel{}
104+
103105
```
104106

105-
That's it! Your User model will now automatically sync with the IndexedUser model whenever changes occur. You can search your User model effortlessly, like:
107+
- That's it! Your User model will now automatically sync with the IndexedUser model whenever changes occur. You can search your User model effortlessly, like:
108+
106109

107110
```php
108111
User::viaIndex()->term('running')->orTerm('swimming')->search();
@@ -172,7 +175,7 @@ User::viaIndex()->where('status', 'active')
172175
->get();
173176
```
174177

175-
> Finds all active users within a 5km radius from the coordinates [0, 0], ordering them from closest to farthest.
178+
> Finds all active users within a 5km radius from the coordinates [0, 0], ordering them from closest to farthest. Not kidding.
176179
> - https://elasticsearch.pdphilip.com/es-specific#geo-point
177180
> - https://elasticsearch.pdphilip.com/ordering-and-pagination#order-by-geo-distance
178181
@@ -218,43 +221,43 @@ User::viaIndex()->fuzzyTerm('quikc')->orFuzzyTerm('brwn')->andFuzzyTerm('foks')-
218221

219222
```php
220223
User::viaIndex()->term('espresso')->highlight()->search();
221-
222224
```
223225

224226
> Searches for 'espresso' across all fields and highlights where it was found.
225227
> - https://elasticsearch.pdphilip.com/full-text-search#highlighting
226228
229+
227230
### Note on `Index Model` vs `Base Model` Results
228231

229-
Since the `viaIndex()` taps into the `IndexModel`, the results returned will be instances of `IndexedUser`, not the base `User` model. This can be useful for display purposes, such as highlighting embedded fields.
232+
- Since the `viaIndex()` taps into the `IndexModel`, the results returned will be instances of `IndexedUser`, not the base `User` model.
233+
- This can be useful for display purposes, such as highlighting embedded fields.
234+
- **<u>However, in most cases you'll need to return and work with the `Base Model`</u>**
230235

231-
### However, in most cases you'll need to return and work with the `Base Model`
236+
### To search and return results as `Base Models` use `asModel()`
232237

233-
To get the results as base models simply chain `->asModel()` at the end of your query:
238+
- Simply chain `->asModel()` at the end of your query:
234239

235240
```php
236241
User::viaIndex()->term('david')->orderByDesc('created_at')->limit(3)->search()->asModel();
237242
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')->get()->asModel();
238243
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')->first()->asModel();
239244
```
240245

241-
### For Pagination: `paginateModels()`
246+
### To search and paginate results as `Base Models` use: `paginateModels()`
242247

243-
- Direct Pagination (no paginator):
248+
- Complete the query string with `->paginateModels()`
244249

245250
```php
246-
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')->paginate(10)->asModel();
247-
```
248-
249-
This will return the 10 results as models but without a paginator.
251+
// Returns a pagination instance of Users ✔️:
252+
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')->paginateModels(10);
250253

251-
- Paginate and Return Base Models use `paginateModels()`:
254+
// Returns a pagination instance of IndexedUsers:
255+
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')->paginate(10);
252256

253-
```php
254-
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')->paginateModels(10);
257+
// Will not paginate ❌ (but will at least return a collection of 10 Users):
258+
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')->paginate(10)->asModel();
255259
```
256260

257-
This will paginate the results from Elasticsearch and return the original base models.
258261

259262

260263
---
@@ -668,15 +671,15 @@ Provides a comprehensive state of a specific index, in this case, for the `User`
668671
3. Migrate and Build/Rebuild an Index:
669672

670673
```bash
671-
php artisan lens:build User
674+
php artisan lens:migrate User
672675
```
673676

674677
Deletes the existing User index, runs the migration, and rebuilds all records.
675678

676679
<div align="center">
677680
<img
678-
src="https://cdn.snipform.io/pdphilip/elasticlens/elasticlens-build.gif"
679-
alt="ElasticLens Build"
681+
src="https://cdn.snipform.io/pdphilip/elasticlens/lens-migrate.gif"
682+
alt="ElasticLens Migrate"
680683
/>
681684
</div>
682685

@@ -695,6 +698,21 @@ Generates a new index for the `Profile` model.
695698
/>
696699
</div>
697700

701+
5. Bulk (Re)Build Indexes for a `Base Model`:
702+
703+
```bash
704+
php artisan lens:build Profile
705+
```
706+
707+
Rebuilds all the `IndexedProfile` records for the `Profile` model.
708+
709+
<div align="center">
710+
<img
711+
src="https://cdn.snipform.io/pdphilip/elasticlens/lens-build-v2.gif"
712+
alt="ElasticLens Build"
713+
/>
714+
</div>
715+
698716
---
699717

700718
## Step 8: Optionally access the built-in `IndexableBuildState` model to track index build states
@@ -726,8 +744,39 @@ IndexableBuildState::countModelErrors($indexModel);
726744
IndexableBuildState::countModelRecords($indexModel);
727745
```
728746

729-
**Note**: While you can query the `IndexableBuildState` model directly, avoid writing or deleting records within it manually, as this can interfere with the health checks and overall integrity of the indexing process. The model should be
730-
used for reading purposes only to ensure accurate monitoring and reporting.
747+
**Note**: While you can query the `IndexableBuildState` model directly, avoid writing or deleting records within it manually, as this can interfere with the health checks and overall integrity of the indexing process. The model should be used for reading purposes only to ensure accurate monitoring and reporting.
748+
749+
---
750+
751+
## Step 9: Optionally Access the Built-in `IndexableMigrationLog` Model for Index Migration Status
752+
753+
ElasticLens includes a built-in `IndexableMigrationLog` model for monitoring and tracking the state of index migrations. This model logs each migration related to an `Index Model`.
754+
755+
### Model Fields:
756+
757+
- string `$index_model`: The migrated Index Model.
758+
- IndexableMigrationLogState `$state`: State of the migration
759+
- array `$map`: Migration map that was passed to Elasticsearch.
760+
- int `$version_major`: Major version of the indexing process.
761+
- int `$version_minor`: Minor version of the indexing process.
762+
- Carbon `$created_at`: Timestamp of when the migration was created.
763+
764+
### Attributes:
765+
766+
- @property-read string `$version`: Parsed version ex v2.03
767+
- @property-read string `$state_name`: Current state name.
768+
- @property-read string `$state_color`: Color representing the current state.
769+
770+
Built-in methods include:
771+
772+
```php
773+
IndexableMigrationLog::getLatestVersion($indexModel);
774+
IndexableMigrationLog::getLatestMigration($indexModel);
775+
```
776+
777+
**Note**: While you can query the `IndexableMigrationLog` model directly, avoid writing or deleting records within it manually, as this can interfere with versioing of the migrations. The model should be used for reading purposes only to ensure accuracy.
778+
779+
---
731780

732781
## Credits
733782

resources/views/cli/bulk.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
?>
1010
<div>
11-
@include('elasticlens::cli.components.loader-blocks',['message' => $title,'i' => $i,'state' => $state])
11+
@include('elasticlens::cli.components.loader-spin',['message' => $title,'i' => $i,'state' => $state])
1212
@include('elasticlens::cli.components.hr')
1313
@include('elasticlens::cli.components.data-row-value',['key' => 'Created','value' => $created,'class' => 'text-sky-500'])
1414
@include('elasticlens::cli.components.data-row-value',['key' => 'Updated','value' => $updated,'class' => 'text-emerald-500'])

resources/views/cli/components/data-row-value.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
1717
if ($value === 0) {
1818
$class = 'text-stone-600';
19+
} elseif (is_int($value)) {
20+
$value = number_format($value);
1921
}
2022
if (empty($class)) {
2123
$class = '';

src/Commands/LensBuildCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public function handle(): int
5959

6060
$this->indexModel = Lens::fetchIndexModelClass($this->model);
6161
$this->newLine();
62-
render((string) view('elasticlens::cli.components.title', ['title' => 'Rebuild '.class_basename($this->indexModel), 'color' => 'cyan']));
62+
$name = Str::plural($this->model);
63+
render((string) view('elasticlens::cli.components.title', ['title' => 'Rebuild '.$name, 'color' => 'cyan']));
6364
$this->newLine();
6465
$health = new LensState($this->indexModel);
6566
$this->baseModel = $health->baseModel;
@@ -164,6 +165,7 @@ private function processAsyncBuild($health, $model): bool
164165
$total = $this->created + $this->modified;
165166
$time = $this->getTime();
166167
if ($total > 0) {
168+
$total = number_format($total);
167169
render((string) view('elasticlens::cli.components.info', ['message' => 'Indexed '.$total.' '.$name.' in '.$time['sec'].' seconds']));
168170
} else {
169171
render((string) view('elasticlens::cli.components.error', ['message' => 'All indexes failed to build']));

src/Commands/Terminal/AsyncHtmlRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function withFailOver(string $html): void
6262

6363
public function withTask(callable $task): self
6464
{
65-
$this->task = $task;
65+
$this->task = $task(...);
6666

6767
return $this;
6868
}

0 commit comments

Comments
 (0)