Skip to content

Commit dbd2cfe

Browse files
committedNov 8, 2020
Switching to ElasticSearch Scout Driver & Plus for pure Elastic results.
1 parent 4a30d8a commit dbd2cfe

File tree

14 files changed

+2326
-836
lines changed

14 files changed

+2326
-836
lines changed
 

‎.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ yarn-error.log
2121

2222
## PHPStorm stuff
2323
_ide_helper.php
24-
.phpstorm.meta.php
24+
.phpstorm.meta.php
25+
/storage/debugbar/

‎app/Http/Controllers/MediaController.php

+18-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Http\Request;
77
use Illuminate\Http\JsonResponse;
8-
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8+
use\Illuminate\Pagination\LengthAwarePaginator;
99

1010
use App\Media;
1111

@@ -15,20 +15,27 @@ class MediaController extends Controller
1515

1616
public function __construct(Request $request)
1717
{
18-
// Set limit
18+
// Set default limit
1919
$this->limit = $this->page_limit($request);
2020
}
2121

2222
/**
2323
* API Resource for Media Model
24-
* @return LengthAwarePaginator|void
24+
* @param Request $request
25+
* @return LengthAwarePaginator
2526
*/
26-
public function index()
27+
public function index(Request $request)
2728
{
28-
// Default model listing
29-
$data = Media::search('*')->paginate(50);
30-
return $data->isNotEmpty() ? $data : abort(404);
29+
// Default documents listing
30+
$matches = Media::matchAllSearch()->size($this->limit)->execute();
31+
$docs = $matches->documents();
32+
$res = [];
33+
foreach ($docs as $media) {
34+
$res[] = array_merge(['id' => (int) $media->getId()], $media->getContent());
35+
}
36+
return new LengthAwarePaginator($res, $matches->total(), $this->limit, $request->input('page') ?? 1);
3137
}
38+
3239
// public function index(Request $request)
3340
// {
3441
//
@@ -119,7 +126,6 @@ public function get($id) {
119126

120127
/**
121128
* Return top models by week
122-
*
123129
* @return LengthAwarePaginator
124130
*/
125131
public function best()
@@ -135,7 +141,6 @@ public function best()
135141

136142
/**
137143
* Increment likes column on
138-
*
139144
* @param $id
140145
* @return JsonResponse
141146
*/
@@ -149,7 +154,6 @@ public function like($id)
149154

150155
/**
151156
* Increment dislikes column
152-
*
153157
* @param $id
154158
* @return JsonResponse
155159
*/
@@ -161,8 +165,10 @@ public function dislike($id)
161165
return response()->json(['success' => false], 404);
162166
}
163167

164-
/*
165-
* Set page limit (respectively query limit)
168+
/**
169+
* Set default page limit
170+
* @param $request
171+
* @return int
166172
*/
167173
public function page_limit($request) {
168174
return ($request->has('limit') && $request->input('limit') < 50) ? (int) $request->input('limit') : 50;

‎app/Media.php

+6-27
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,15 @@
22

33
namespace App;
44

5-
use ScoutElastic\Searchable;
5+
use ElasticScoutDriverPlus\CustomSearch;
66
use Illuminate\Database\Eloquent\Model;
77
use Illuminate\Database\Eloquent\Factories\HasFactory;
8+
use Illuminate\Database\Eloquent\SoftDeletes;
9+
use Laravel\Scout\Searchable;
810

911
class Media extends Model
1012
{
11-
use Searchable, HasFactory;
12-
13-
protected $indexConfigurator = MediaIndex::class;
14-
15-
protected $mapping = [
16-
'properties' => [
17-
'title' => [
18-
'type' => 'text',
19-
'fields' => [
20-
'raw' => [
21-
'type' => 'keyword',
22-
],
23-
],
24-
],
25-
],
26-
];
27-
28-
/**
29-
* Get the index name for the model.
30-
*
31-
* @return string
32-
*/
33-
public function searchableAs()
34-
{
35-
return 'videos_idx';
36-
}
13+
use SoftDeletes, Searchable, CustomSearch, HasFactory;
3714

3815
/**
3916
* Get the index data array for the model.
@@ -44,6 +21,8 @@ public function toSearchableArray()
4421
{
4522
return $this->only([
4623
'title',
24+
'thumbnail',
25+
'album',
4726
'categories',
4827
'views',
4928
'likes',

‎app/MediaIndex.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ class MediaIndex extends IndexConfigurator
99
{
1010
use Migratable;
1111

12-
// Index name for Elastic search index
12+
// Elastic search index
1313
protected $name = 'media_idx';
1414

1515
/**
1616
* Elastic Search settings
17-
*
1817
* @var array
19-
* @return json
2018
*/
2119
protected $settings = [
2220
'analysis' => [

‎composer.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
"require": {
1111
"php": "^7.3",
1212
"algolia/algoliasearch-client-php": "2.2",
13-
"babenkoivan/scout-elasticsearch-driver": "^4.2",
13+
"babenkoivan/elastic-client": "^1.1",
14+
"babenkoivan/elastic-migrations": "^1.2",
15+
"babenkoivan/elastic-scout-driver": "^1.2",
16+
"babenkoivan/elastic-scout-driver-plus": "^1.11",
1417
"fideloper/proxy": "^4.2",
1518
"fruitcake/laravel-cors": "^1.0",
1619
"guzzlehttp/guzzle": "^7.0.1",
@@ -23,6 +26,7 @@
2326
},
2427
"require-dev": {
2528
"aalaap/faker-youtube": "^1.2",
29+
"barryvdh/laravel-debugbar": "^3.5",
2630
"barryvdh/laravel-ide-helper": "^2.8",
2731
"facade/ignition": "^2.3.6",
2832
"fzaninotto/faker": "^1.9.1",

‎composer.lock

+2,227-768
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎config/const.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
| Here you may configure custom constants specific to your app.
1010
|
1111
*/
12-
// Videos cateogories excempt from main listings
12+
// Media categories exempt from main listings
1313
'excluded_cats' => env('EXCLUDED_CATEGORIES', 'null'),
1414
];

‎config/elastic.client.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php declare(strict_types=1);
2+
3+
return [
4+
'hosts' => [
5+
env('ELASTIC_HOST', 'localhost:9200'),
6+
]
7+
];

‎database/factories/MediaFactory.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public function definition()
2424
{
2525
$this->faker->addProvider(new Youtube($this->faker));
2626
return [
27-
'embed' => $this->faker->youtubeEmbedCode(),
28-
'thumbnail' => $this->faker->imageUrl(205, 155, null, true),
29-
'album' => $this->faker->imageUrl(205, 155, null, true) .';'. $this->faker->imageUrl(205, 155, null, true),
27+
'embed' => $this->faker->youtubeShortUri(),
28+
'thumbnail' => 'https://picsum.photos/205/155/?image='.$this->faker->randomDigit,
29+
'album' => 'https://picsum.photos/205/155/?image='.$this->faker->randomDigit.';'.'https://picsum.photos/205/155/?image='.$this->faker->randomDigit.';'.'https://picsum.photos/205/155/?image='.$this->faker->randomDigit,
3030
'title' => $this->faker->sentence($nbWords = 6, $variableNbWords = true),
3131
'categories' => $this->faker->word .";". $this->faker->word .";". $this->faker->word,
3232
'author' => $this->faker->name,

‎database/factories/RootSeed.php

-16
This file was deleted.

‎database/migrations/2020_04_13_073834_media.php

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function up()
2727
$table->integer('views');
2828
$table->integer('likes');
2929
$table->integer('dislikes');
30+
$table->softDeletes();
3031
$table->timestamps();
3132

3233
});

‎database/seeds/MediaSeeder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class MediaSeeder extends Seeder
1515
public function run()
1616
{
1717
Media::factory()
18-
->times(10)
18+
->times(300)
1919
->create();
2020
}
2121
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use ElasticAdapter\Indices\Mapping;
5+
use ElasticAdapter\Indices\Settings;
6+
use ElasticMigrations\Facades\Index;
7+
use ElasticMigrations\MigrationInterface;
8+
9+
final class CreateMediaIndex implements MigrationInterface
10+
{
11+
/**
12+
* Run the migration.
13+
*/
14+
public function up(): void
15+
{
16+
Index::create('media', function (Mapping $mapping, Settings $settings) {
17+
// Mapping
18+
$mapping->text('thumbnail');
19+
$mapping->text('embed');
20+
$mapping->text('album');
21+
$mapping->text('title');
22+
$mapping->keyword('categories');
23+
$mapping->text('author');
24+
$mapping->float('duration');
25+
$mapping->float('views');
26+
$mapping->float('likes');
27+
$mapping->float('dislikes');
28+
$mapping->date('created_at');
29+
$mapping->date('updated_at');
30+
31+
// Settings
32+
$settings->analysis([
33+
'properties' => [
34+
'categories' => [
35+
'type' => 'text',
36+
'fields' => [
37+
'raw' => [
38+
'type' => 'keyword',
39+
],
40+
],
41+
],
42+
],
43+
]);
44+
});
45+
}
46+
47+
/**
48+
* Reverse the migration.
49+
*/
50+
public function down(): void
51+
{
52+
Index::drop('media');
53+
}
54+
}

‎resources/sass/_style.scss

-3
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,6 @@ a, .btn-link {
308308
overflow: hidden;
309309
img {
310310
border-radius: 0;
311-
filter: blur(35px);
312311
}
313312
.carousel {
314313
position: absolute;
@@ -345,7 +344,6 @@ a, .btn-link {
345344
.video-title {
346345
word-break: break-word;
347346
a {
348-
//filter: blur(2px);
349347
color: $lilac;
350348
&:hover {
351349
color: $white;
@@ -416,7 +414,6 @@ a, .btn-link {
416414
}
417415
.all-categories {
418416
a {
419-
filter: blur(2px);
420417
color: $lilac;
421418
&:hover {
422419
color: $white;

0 commit comments

Comments
 (0)
Please sign in to comment.