Skip to content

Commit 5ee70bf

Browse files
committed
Several upgrades and bug fixes
1 parent fb61572 commit 5ee70bf

File tree

13 files changed

+733
-482
lines changed

13 files changed

+733
-482
lines changed

README.md

Lines changed: 147 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,8 @@ You can use these values in a normal [where](#where) clause, or use the built-in
320320
$products = Product::whereDate('created_at', '2022-01-29')->get();
321321
```
322322

323-
**Note:** The usage for `whereMonth` / `whereDay` / `whereYear` / `whereTime` has disabled for the current version of
323+
**Note:** The usage for `whereMonth` / `whereDay` / `whereYear` / `whereTime` has been disabled for the current version
324+
of
324325
this plugin
325326

326327
### Aggregation
@@ -532,8 +533,8 @@ $updates = Product::where('status', 1)->update(['status' => 4]); //Updates all s
532533
**Saving 'without refresh'**
533534

534535
Elasticsearch will write a new document and return the `_id` before it has been indexed. This means that there could be
535-
a delay in looking up the document that has just been created. To keep the indexed data consistent, the default is to *
536-
write a new document and wait until it has been indexed* - If you know that you won't need to look up or manipulate the
536+
a delay in looking up the document that has just been created. To keep the indexed data consistent, the default is to
537+
*write a new document and wait until it has been indexed* - If you know that you won't need to look up or manipulate the
537538
new document immediately, then you can leverage the speed benefit of `write and move on` with `saveWithoutRefresh()`
538539
and `createWithoutRefresh()`
539540

@@ -1185,7 +1186,7 @@ class MyIndexes extends Migration
11851186

11861187
//Disk space considerations ::
11871188
//Not indexed and not searchable:
1188-
$index->text('internal_notes')->docValues(false);
1189+
$index->keyword('internal_notes')->docValues(false);
11891190
//Remove scoring for search:
11901191
$index->array('tags')->norms(false);
11911192
//Remove from index, can't search by this field but can still use for aggregations:
@@ -1242,20 +1243,90 @@ class MyIndexes extends Migration
12421243
```
12431244

12441245
All methods
1246+
Note: If you have configured a prefix in your config file, then all schema methods will be scoped to that prefix.
1247+
1248+
Index look-ups.
12451249

12461250
```php
1247-
Schema::getIndices();
1248-
Schema::getMappings('my_index')
1249-
Schema::getSettings('my_index')
1251+
Schema::getIndex('my_index');
1252+
/**
1253+
return [
1254+
"my_prefix_my_index" => [
1255+
"aliases" => [],
1256+
"mappings" => [],
1257+
"settings" => [],
1258+
],
1259+
];
1260+
**/
1261+
1262+
Schema::getIndex('page_hits_*');
1263+
/**
1264+
return [
1265+
"my_prefix_page_hits_2023-01-01" => [
1266+
"aliases" => [],
1267+
"mappings" => [],
1268+
"settings" => [],
1269+
],
1270+
"my_prefix_page_hits_2023-01-02" => [
1271+
"aliases" => [],
1272+
"mappings" => [],
1273+
"settings" => [],
1274+
],
1275+
....
1276+
];
1277+
**/
1278+
Schema::getIndex('my_non_existing_index'); //returns [];
1279+
1280+
Schema::getIndices(); //Equivalent to Schema::getIndex('*');
1281+
1282+
Schema::getMappings('my_index');
1283+
Schema::getSettings('my_index');
1284+
1285+
//Booleans
1286+
Schema::hasField('my_index','my_field');
1287+
Schema::hasFields('my_index',['field_a','field_b','field_c']);
1288+
Schema::hasIndex('my_index');
1289+
1290+
```
1291+
1292+
Overriding the prefix:
1293+
1294+
```php
1295+
Schema::overridePrefix(null)->getIndex('my_index');
1296+
/**
1297+
return [
1298+
"my_index" => [
1299+
"aliases" => [],
1300+
"mappings" => [],
1301+
"settings" => [],
1302+
],
1303+
];
1304+
**/
1305+
Schema::overridePrefix('some_other_prefix')->getIndex('my_index');
1306+
/**
1307+
return [
1308+
"some_other_prefix_my_index" => [
1309+
"aliases" => [],
1310+
"mappings" => [],
1311+
"settings" => [],
1312+
],
1313+
];
1314+
**/
1315+
```
1316+
1317+
Index Creation, Modification, Deletion
1318+
1319+
```php
1320+
12501321
Schema::create('my_index',function (IndexBlueprint $index) {
12511322
//......
1252-
})
1323+
});
12531324
Schema::createIfNotExists('my_index',function (IndexBlueprint $index) {
12541325
//......
1255-
})
1326+
});
12561327
Schema::reIndex('from_index','to_index') {
12571328
//......
1258-
})
1329+
});
12591330
Schema::modify('my_index',function (IndexBlueprint $index) {
12601331
//......
12611332
});
@@ -1264,12 +1335,62 @@ Schema::deleteIfExists('my_index')
12641335
Schema::setAnalyser('my_index',function (AnalyzerBlueprint $settings) {
12651336
//......
12661337
});
1267-
//Booleans
1268-
Schema::hasField('my_index','my_field')
1269-
Schema::hasFields('my_index',['field_a','field_b','field_c'])
1270-
Schema::hasIndex('my_index')
1338+
12711339
//DIY
1272-
Schema::dsl('indexMethod',$dslParams)
1340+
Schema::dsl('indexMethod',$dslParams);
1341+
```
1342+
1343+
Re-indexing example:
1344+
1345+
```php
1346+
//create a new index
1347+
Schema::create('site_logs',function (IndexBlueprint $index) {
1348+
$index->keyword('url');
1349+
$index->ip('user_ip');
1350+
});
1351+
1352+
//Create a record assuming you have a model for this index
1353+
SiteLog::create([
1354+
'url' => 'https://example.com/contact-us',
1355+
'user_ip' => '0.0.0.0',
1356+
'location' => ['lat' => -7.3,'lon' => 3.1]
1357+
]);
1358+
//'location' index will be created by elasticsearch as an object
1359+
1360+
//Trying to filter by location will fail due to the incorrect mapping
1361+
$logs = SiteLog::filterGeoBox('location',[-10,10],[10,-10])->get();
1362+
1363+
//Step 1: Create a temporary index with the correct mapping
1364+
Schema::create('temp_site_logs',function (IndexBlueprint $index) {
1365+
$index->keyword('url');
1366+
$index->ip('user_ip');
1367+
$index->geo('location');
1368+
});
1369+
1370+
//Step 2: Re-index the data from the original index to the temporary index
1371+
//This will copy all the records from site_logs to temp_site_logs
1372+
$result = Schema::reIndex('site_logs','temp_site_logs');
1373+
dd($result->data); //confirm that the re-indexing was successful
1374+
$copiedRecords = DB::connection('elasticsearch')->table('my_prefix_temp_site_logs')->count(); //count the records in the new index, make sure everything is there
1375+
1376+
//Step 3: Once deemed safe; delete the original index
1377+
Schema::delete('site_logs');
1378+
1379+
//Step 4: Recreate the original index with the correct mapping
1380+
Schema::create('site_logs',function (IndexBlueprint $index) {
1381+
$index->keyword('url');
1382+
$index->ip('user_ip');
1383+
$index->geo('location');
1384+
});
1385+
1386+
//Step 5: Re-index the data from the temporary index to the original index
1387+
$result = Schema::reIndex('temp_site_logs','site_logs');
1388+
1389+
//Step 6: Delete the temporary index
1390+
Schema::delete('temp_site_logs');
1391+
1392+
//Now you can filter by location
1393+
$logs = SiteLog::filterGeoBox('location',[-10,10],[10,-10])->get();
12731394

12741395
```
12751396

@@ -1364,7 +1485,7 @@ You will need to set the record's actual index when creating a new record, with
13641485
Create example:
13651486

13661487
```php
1367-
$pageHit = new PageHit
1488+
$pageHit = new PageHit;
13681489
$pageHit->page_id = 4;
13691490
$pageHit->ip = $someIp;
13701491
$pageHit->setIndex('page_hits_'.date('Y-m-d'));
@@ -1378,6 +1499,16 @@ Each eloquent model will have the current record's index embedded into it, to re
13781499
$pageHit->getRecordIndex(); //returns page_hits_2021-01-01
13791500
```
13801501

1502+
You can search within a specific index of your dynamic indices model by calling `setIndex('value')` before building
1503+
your query:
1504+
1505+
```php
1506+
//Search within the index page_hits_2023-01-01 for page_id = 3
1507+
$model = new PageHit;
1508+
$model->setIndex('page_hits_2023-01-01');
1509+
$pageHits = $model->where('page_id', 3)->get();
1510+
```
1511+
13811512
RAW DSL
13821513
========
13831514

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"illuminate/container": "^10.0",
2525
"illuminate/database": "^10.0",
2626
"illuminate/events": "^10.0",
27-
"elasticsearch/elasticsearch": "8.9"
27+
"elasticsearch/elasticsearch": "8.10"
2828
},
2929
"require-dev": {
3030
"phpunit/phpunit": "^10.3",

src/Connection.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
use PDPhilip\Elasticsearch\DSL\Bridge;
77
use Elastic\Elasticsearch\ClientBuilder;
88
use Illuminate\Database\Connection as BaseConnection;
9-
use Illuminate\Support\Arr;
109
use Illuminate\Support\Str;
11-
use InvalidArgumentException;
1210
use RuntimeException;
1311

1412

@@ -44,6 +42,11 @@ public function getIndexPrefix(): string
4442
return $this->indexPrefix;
4543
}
4644

45+
public function setIndexPrefix($newPrefix): void
46+
{
47+
$this->indexPrefix = $newPrefix;
48+
}
49+
4750

4851
public function getTablePrefix(): string
4952
{
@@ -77,12 +80,14 @@ public function setMaxSize($value)
7780
$this->maxSize = $value;
7881
}
7982

83+
8084
public function table($table, $as = null)
8185
{
82-
return $this->setIndex($table);
86+
$query = new Query\Builder($this, new Query\Processor());
87+
88+
return $query->from($table);
8389
}
8490

85-
8691
/**
8792
* @inheritdoc
8893
*/
@@ -195,6 +200,10 @@ protected function _cloudConnection(): Client
195200

196201
public function __call($method, $parameters)
197202
{
203+
if (!$this->index) {
204+
$this->index = $this->indexPrefix.'*';
205+
}
206+
198207
$bridge = new Bridge($this->client, $this->index, $this->maxSize);
199208

200209
return $bridge->{'process'.Str::studly($method)}(...$parameters);

0 commit comments

Comments
 (0)