Skip to content

Commit 5be8619

Browse files
committed
Dependency & docs update
1 parent 0d6e285 commit 5be8619

File tree

4 files changed

+44
-35
lines changed

4 files changed

+44
-35
lines changed

README.md

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ElasticLens for Laravel uses Elasticsearch to create and sync a searchable index
2424
</div>
2525

2626
```php
27-
User::viaIndex()->phrase('loves dogs')->where('status','active')->search();
27+
User::viaIndex()->searchPhrase('loves dogs')->where('status','active')->get();
2828
```
2929

3030
---
@@ -33,19 +33,20 @@ User::viaIndex()->phrase('loves dogs')->where('status','active')->search();
3333

3434
Yes, but mostly no.
3535

36-
**ElasticLens is built from the ground up around Elasticsearch**.
36+
**ElasticLens is built from the ground up around Elasticsearch**.
3737

38-
It integrates directly with the [Laravel-Elasticsearch](https://github.com/pdphilip/laravel-elasticsearch) package (Elasticsearch using Eloquent), creating a dedicated `Index Model` that is fully accessible and automatically synced with your `Base Model`.
38+
It integrates directly with the [Laravel-Elasticsearch](https://github.com/pdphilip/laravel-elasticsearch) package (Elasticsearch using Eloquent), creating a dedicated `Index Model` that is fully accessible and automatically synced with
39+
your `Base Model`.
3940

4041
<details>
4142
<summary> How? </summary>
4243

4344

4445
> The `Index Model` acts as a separate Elasticsearch model managed by ElasticLens, yet you retain full control over it, just like any other Laravel model. In addition to working directly with the `Index Model`, ElasticLens offers tools for
45-
mapping fields (with embedding relationships) during the build process, and managing index migrations.
46+
> mapping fields (with embedding relationships) during the build process, and managing index migrations.
4647
4748
> For Example, a base `User` Model will sync with an Elasticsearch `IndexedUser` Model that provides all the features from [Laravel-Elasticsearch](https://github.com/pdphilip/laravel-elasticsearch) to search your `Base Model`.
48-
49+
4950
</details>
5051

5152
---
@@ -80,7 +81,7 @@ Publish the config file and run the migrations with:
8081
php artisan lens:install
8182
```
8283

83-
# Usage
84+
# Usage
8485

8586
The Walkthrough below will demonstrate all the features by way of an example.
8687

@@ -99,7 +100,9 @@ class User extends Eloquent implements Authenticatable, CanResetPassword
99100
```
100101

101102
### 2. Create an Index Model for Your Base Model:
102-
- ElasticLens expects the `Index Model` to be named as `Indexed` + `BaseModelName` and located in the `App\Models\Indexes` directory.
103+
104+
- ElasticLens expects the `Index Model` to be named as `Indexed` + `BaseModelName` and located in the `App\Models\Indexes` directory.
105+
103106
```php
104107
/**
105108
* Create: App\Models\Indexes\IndexedUser.php
@@ -113,10 +116,11 @@ class IndexedUser extends IndexModel{}
113116

114117

115118
```
119+
116120
- 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:
117121

118122
```php
119-
User::viaIndex()->term('running')->orTerm('swimming')->search();
123+
User::viaIndex()->searchTerm('running')->orSearchTerm('swimming')->get();
120124
```
121125

122126
---
@@ -138,8 +142,8 @@ To truly harness the power of [Laravel-Elasticsearch](https://github.com/pdphili
138142
```php
139143
BaseModel::viaIndex()->{build your ES Eloquent query}->first();
140144
BaseModel::viaIndex()->{build your ES Eloquent query}->get();
141-
BaseModel::viaIndex()->{build your ES Eloquent query}->search();
142-
BaseModel::viaIndex()->{build your ES Eloquent query}->avg();
145+
BaseModel::viaIndex()->{build your ES Eloquent query}->paginate();
146+
BaseModel::viaIndex()->{build your ES Eloquent query}->avg('orders');
143147
BaseModel::viaIndex()->{build your ES Eloquent query}->distinct();
144148
BaseModel::viaIndex()->{build your ES Eloquent query}->{etc}
145149
```
@@ -149,9 +153,9 @@ BaseModel::viaIndex()->{build your ES Eloquent query}->{etc}
149153
#### 1. Basic Term Search:
150154

151155
```php
152-
User::viaIndex()->term('nara')
156+
User::viaIndex()->searchTerm('nara')
153157
->where('state','active')
154-
->limit(3)->search();
158+
->limit(3)->get();
155159
```
156160

157161
> This searches all users who are `active` for the term 'nara' across all fields and return the top 3 results.
@@ -160,25 +164,21 @@ User::viaIndex()->term('nara')
160164
#### 2. Phrase Search:
161165

162166
```php
163-
User::viaIndex()->phrase('Ice bathing')
167+
User::viaIndex()->searchPhrase('Ice bathing')
164168
->orderByDesc('created_at')
165-
->limit(5)->search();
169+
->limit(5)->get();
166170
```
167171

168172
> Searches all fields for the phrase 'Ice bathing' and returns the 3 newest results. Phrases match exact words in order.
169173
> - https://elasticsearch.pdphilip.com/full-text-search#phrase-search-phrase
170174
171-
#### 3. Boosting Terms and Minimum Score:
175+
#### 3. Boosting Terms fields:
172176

173177
```php
174-
User::viaIndex()->term('David')
175-
->field('first_name', 3)
176-
->field('last_name', 2)
177-
->field('bio')
178-
->minScore(2.1)->search();
178+
User::viaIndex()->searchTerm('David',['first_name^3', 'last_name^2', 'bio'])->get();
179179
```
180180

181-
> Searches for the term 'David', boosts the first_name field by 3, last_name by 2, and also checks the bio field. Returns results with a minimum score of 2.1, ordered by the highest score.
181+
> Searches for the term 'David', boosts the first_name field by 3, last_name by 2, and also checks the bio field. Results are ordered by score.
182182
> - https://elasticsearch.pdphilip.com/full-text-search#boosting-terms
183183
> - https://elasticsearch.pdphilip.com/full-text-search#minimum-score
184184
@@ -228,7 +228,10 @@ User::viaIndex()->whereNestedObject('user_logs', function (Builder $query) {
228228
#### 8. Fuzzy Search:
229229

230230
```php
231-
User::viaIndex()->fuzzyTerm('quikc')->orFuzzyTerm('brwn')->andFuzzyTerm('foks')->search();
231+
User::viaIndex()->searchTerm('quikc')->asFuzzy()
232+
->orSearchTerm('brwn')->asFuzzy()
233+
->orSearchTerm('foks')->asFuzzy()
234+
->get();
232235
```
233236

234237
> No spell, no problem. Search Fuzzy.
@@ -237,16 +240,24 @@ User::viaIndex()->fuzzyTerm('quikc')->orFuzzyTerm('brwn')->andFuzzyTerm('foks')-
237240
#### 9. Highlighting Search Results:
238241

239242
```php
240-
User::viaIndex()->term('espresso')->highlight()->search();
243+
User::viaIndex()->searchTerm('espresso')->withHighlights()->get();
241244
```
242245

243246
> Searches for 'espresso' across all fields and highlights where it was found.
244247
> - https://elasticsearch.pdphilip.com/full-text-search#highlighting
245248
249+
#### 10. Phrase prefix search:
250+
251+
```php
252+
User::viaIndex()->searchPhrasePrefix('loves espr')->withHighlights()->get();
253+
```
254+
255+
> Searches for the phrase prefix 'loves espr' across all fields and highlights where it was found.
256+
> - https://elasticsearch.pdphilip.com/full-text-search#highlighting
246257
247258
### Note on `Index Model` vs `Base Model` Results
248259

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

@@ -255,7 +266,7 @@ User::viaIndex()->term('espresso')->highlight()->search();
255266
- Simply chain `->asModel()` at the end of your query:
256267

257268
```php
258-
User::viaIndex()->term('david')->orderByDesc('created_at')->limit(3)->search()->asModel();
269+
User::viaIndex()->searchTerm('david')->orderByDesc('created_at')->limit(3)->get()->asModel();
259270
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')->get()->asModel();
260271
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')->first()->asModel();
261272
```
@@ -275,8 +286,6 @@ User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')->paginate(10);
275286
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')->paginate(10)->asModel();
276287
```
277288

278-
279-
280289
---
281290

282291
## Step 3: Create a field Map
@@ -755,7 +764,7 @@ ElasticLens includes a built-in `IndexableBuildState` model that allows you to m
755764

756765
- @property-read string `$state_name`: The name of the current state.
757766
- @property-read string `$state_color`: The color associated with the current state.
758-
767+
759768
</details>
760769

761770

@@ -767,7 +776,8 @@ IndexableBuildState::countModelErrors($indexModel);
767776
IndexableBuildState::countModelRecords($indexModel);
768777
```
769778

770-
**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.
779+
**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
780+
used for reading purposes only to ensure accurate monitoring and reporting.
771781

772782
---
773783

@@ -790,7 +800,7 @@ ElasticLens includes a built-in `IndexableMigrationLog` model for monitoring and
790800
- @property-read string `$version`: Parsed version ex v2.03
791801
- @property-read string `$state_name`: Current state name.
792802
- @property-read string `$state_color`: Color representing the current state.
793-
803+
794804
</details>
795805

796806

@@ -801,7 +811,8 @@ IndexableMigrationLog::getLatestVersion($indexModel);
801811
IndexableMigrationLog::getLatestMigration($indexModel);
802812
```
803813

804-
**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.
814+
**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
815+
ensure accuracy.
805816

806817
---
807818

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"php": "^8.2",
2222
"spatie/laravel-package-tools": "^1.16",
2323
"nunomaduro/termwind": "*",
24-
"pdphilip/elasticsearch": "^4.1.1",
24+
"pdphilip/elasticsearch": "^4.2",
2525
"illuminate/contracts": "^10.0||^11.0"
2626
},
2727
"require-dev": {

src/Commands/Scripts/IndexCheck.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public static function configState(&$checks, $index): void
154154
];
155155
if ($status['status'] !== 'ok') {
156156
$checks['config']['help'] = [
157-
'For details, run: php artisan elasticlens:health '.$index['state']['index']['modelName'],
157+
'For details, run: php artisan lens:health '.$index['state']['index']['modelName'],
158158
];
159159
}
160160
}

src/Index/LensBuilder.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,11 @@ public function _createIndex(): bool
261261
if (! $index) {
262262
$index = new $this->indexModelInstance;
263263
}
264-
//@phpstan-ignore-next-line
265264
$index->_id = $modelId;
266265

267266
foreach ($this->buildResult->map as $field => $value) {
268267
$index->{$field} = $value;
269268
}
270-
//@phpstan-ignore-next-line
271269
$index->saveWithoutRefresh();
272270
} catch (Exception $e) {
273271
$this->buildResult->setMessage('Index build Error', $e->getMessage());

0 commit comments

Comments
 (0)