From aa68f84750095ed5335ffa37496ef99e9fed413d Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Mon, 21 Mar 2022 17:17:49 +0800 Subject: [PATCH] add scout --- app/Console/Commands/Test.php | 17 +- app/Models/Torrent.php | 32 ++- composer.json | 2 + composer.lock | 431 +++++++++++++++++++++++++++++++++- config/explorer.php | 30 +++ config/scout.php | 137 +++++++++++ 6 files changed, 641 insertions(+), 8 deletions(-) create mode 100644 config/explorer.php create mode 100644 config/scout.php diff --git a/app/Console/Commands/Test.php b/app/Console/Commands/Test.php index db247f98..5c8d9079 100644 --- a/app/Console/Commands/Test.php +++ b/app/Console/Commands/Test.php @@ -13,6 +13,7 @@ use App\Models\Peer; use App\Models\SearchBox; use App\Models\Snatch; use App\Models\Tag; +use App\Models\Torrent; use App\Models\User; use App\Repositories\AgentAllowRepository; use App\Repositories\AttendanceRepository; @@ -30,6 +31,7 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Storage; +use JeroenG\Explorer\Infrastructure\Scout\ElasticEngine; use Rhilip\Bencode\Bencode; class Test extends Command @@ -65,12 +67,15 @@ class Test extends Command */ public function handle() { - $peerId = '-UT355W-%af%b0ky%86N%a6%17i%f8%c1%0a'; - $peerId = '-UT355W-%af%b0ky%86N%a6%17i%f8%c1%0a'; - $peerId = '-UT355W-%AF%B0ky%86N%A6%17i%F8%C1'; - $peerId = '-UT355W-%AF%B0ky%86N%A6%17i%F8%C1%0A'; - $r = strlen(urldecode($peerId)); - dd($r); + $class = Torrent::class; + try { + $this->call("scout:import App\\Models\Torrent"); + } catch (\Throwable $e) { + $this->info($e->getMessage()); + $lastQueryAsJson = ElasticEngine::debug()->json(); + dd($lastQueryAsJson); + } + } diff --git a/app/Models/Torrent.php b/app/Models/Torrent.php index 05f1e7e9..b025339d 100644 --- a/app/Models/Torrent.php +++ b/app/Models/Torrent.php @@ -3,9 +3,13 @@ namespace App\Models; use App\Repositories\TagRepository; +use JeroenG\Explorer\Application\Explored; +use Laravel\Scout\Searchable; -class Torrent extends NexusModel +class Torrent extends NexusModel implements Explored { + use Searchable; + protected $fillable = [ 'name', 'filename', 'save_as', 'descr', 'small_descr', 'ori_descr', 'category', 'source', 'medium', 'codec', 'standard', 'processing', 'team', 'audiocodec', @@ -71,6 +75,32 @@ class Torrent extends NexusModel self::PROMOTION_ONE_THIRD_DOWN => ['text' => '30%', 'up_multiplier' => 1, 'down_multiplier' => 0.3], ]; + public function mappableAs(): array + { + return [ + 'id' => 'long', + 'name' => 'text', + 'descr' => 'text', + 'source' => 'long', + 'leechers' => 'long', + 'seeders' => 'long', + 'added' => 'date', + ]; + } + + public function toSearchableArray() + { + return [ + 'id' => $this->id, + 'name' => $this->name, + 'descr' => $this->descr, + 'source' => $this->source, + 'leechers' => $this->leechers, + 'seeders' => $this->seeders, + 'added' => $this->added, + ]; + } + public function getSpStateRealTextAttribute() { $spStateReal = $this->sp_state_real; diff --git a/composer.json b/composer.json index ebafe41b..e49affce 100644 --- a/composer.json +++ b/composer.json @@ -36,10 +36,12 @@ "guzzlehttp/guzzle": "~6.0", "hashids/hashids": "^4.1", "imdbphp/imdbphp": "^7.0", + "jeroen-g/explorer": "^2.5", "laravel-lang/lang": "~7.0", "laravel/framework": "^8.12", "laravel/octane": "^1.2", "laravel/sanctum": "^2.10", + "laravel/scout": "^9.4", "laravel/tinker": "^2.5", "nao-pon/flysystem-google-drive": "^1.1", "orangehill/iseed": "^3.0", diff --git a/composer.lock b/composer.lock index 7c72bcce..85983e74 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "cd072a9d2174478ddd674b8ea6e0919f", + "content-hash": "6bbf6c91d5313119bf3eec1087dab109", "packages": [ { "name": "asm89/stack-cors", @@ -916,6 +916,201 @@ ], "time": "2020-12-29T14:50:06+00:00" }, + { + "name": "elasticsearch/elasticsearch", + "version": "v7.17.0", + "source": { + "type": "git", + "url": "https://github.com/elastic/elasticsearch-php.git", + "reference": "1890f9d7fde076b5a3ddcf579a802af05b2e781b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/elastic/elasticsearch-php/zipball/1890f9d7fde076b5a3ddcf579a802af05b2e781b", + "reference": "1890f9d7fde076b5a3ddcf579a802af05b2e781b", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "ext-json": ">=1.3.7", + "ezimuel/ringphp": "^1.1.2", + "php": "^7.3 || ^8.0", + "psr/log": "^1|^2|^3" + }, + "require-dev": { + "ext-yaml": "*", + "ext-zip": "*", + "mockery/mockery": "^1.2", + "phpstan/phpstan": "^0.12", + "phpunit/phpunit": "^9.3", + "squizlabs/php_codesniffer": "^3.4", + "symfony/finder": "~4.0" + }, + "suggest": { + "ext-curl": "*", + "monolog/monolog": "Allows for client-level logging and tracing" + }, + "type": "library", + "autoload": { + "files": [ + "src/autoload.php" + ], + "psr-4": { + "Elasticsearch\\": "src/Elasticsearch/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0", + "LGPL-2.1-only" + ], + "authors": [ + { + "name": "Zachary Tong" + }, + { + "name": "Enrico Zimuel" + } + ], + "description": "PHP Client for Elasticsearch", + "keywords": [ + "client", + "elasticsearch", + "search" + ], + "support": { + "issues": "https://github.com/elastic/elasticsearch-php/issues", + "source": "https://github.com/elastic/elasticsearch-php/tree/v7.17.0" + }, + "time": "2022-02-03T13:40:04+00:00" + }, + { + "name": "ezimuel/guzzlestreams", + "version": "3.0.1", + "source": { + "type": "git", + "url": "https://github.com/ezimuel/guzzlestreams.git", + "reference": "abe3791d231167f14eb80d413420d1eab91163a8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ezimuel/guzzlestreams/zipball/abe3791d231167f14eb80d413420d1eab91163a8", + "reference": "abe3791d231167f14eb80d413420d1eab91163a8", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Stream\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Fork of guzzle/streams (abandoned) to be used with elasticsearch-php", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "Guzzle", + "stream" + ], + "support": { + "source": "https://github.com/ezimuel/guzzlestreams/tree/3.0.1" + }, + "time": "2020-02-14T23:11:50+00:00" + }, + { + "name": "ezimuel/ringphp", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/ezimuel/ringphp.git", + "reference": "92b8161404ab1ad84059ebed41d9f757e897ce74" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ezimuel/ringphp/zipball/92b8161404ab1ad84059ebed41d9f757e897ce74", + "reference": "92b8161404ab1ad84059ebed41d9f757e897ce74", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "ezimuel/guzzlestreams": "^3.0.1", + "php": ">=5.4.0", + "react/promise": "~2.0" + }, + "replace": { + "guzzlehttp/ringphp": "self.version" + }, + "require-dev": { + "ext-curl": "*", + "phpunit/phpunit": "~9.0" + }, + "suggest": { + "ext-curl": "Guzzle will use specific adapters if cURL is present" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Ring\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Fork of guzzle/RingPHP (abandoned) to be used with elasticsearch-php", + "support": { + "source": "https://github.com/ezimuel/ringphp/tree/1.2.0" + }, + "time": "2021-11-16T11:51:30+00:00" + }, { "name": "fideloper/proxy", "version": "4.4.1", @@ -1791,6 +1986,80 @@ }, "time": "2021-07-15T12:15:21+00:00" }, + { + "name": "jeroen-g/explorer", + "version": "2.5.1", + "source": { + "type": "git", + "url": "https://github.com/Jeroen-G/Explorer.git", + "reference": "5ffd1a813875c44508cb3fb4916fa31ecd8098d9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Jeroen-G/Explorer/zipball/5ffd1a813875c44508cb3fb4916fa31ecd8098d9", + "reference": "5ffd1a813875c44508cb3fb4916fa31ecd8098d9", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "elasticsearch/elasticsearch": "^7.9", + "illuminate/support": "~7|~8", + "laravel/scout": "~8|~9", + "php": "^7.4|^8.0", + "webmozart/assert": "^1.9" + }, + "require-dev": { + "infection/infection": "^0.20", + "mockery/mockery": "^1.4", + "orchestra/testbench": "~6", + "phpunit/phpunit": "~9.0", + "symplify/easy-coding-standard": "^9.0" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "JeroenG\\Explorer\\ExplorerServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "JeroenG\\Explorer\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "EUPL-1.2" + ], + "authors": [ + { + "name": "Jeroen", + "email": "jeroengjeroeng@gmail.com", + "homepage": "https://jeroeng.dev" + } + ], + "description": "Next-gen Elasticsearch driver for Laravel Scout.", + "homepage": "https://jeroen-g.github.io/Explorer/", + "keywords": [ + "elastic", + "elasticsearch", + "explorer", + "laravel", + "scout", + "search" + ], + "support": { + "issues": "https://github.com/Jeroen-G/Explorer/issues", + "source": "https://github.com/Jeroen-G/Explorer/tree/2.5.1" + }, + "time": "2022-02-09T18:56:40+00:00" + }, { "name": "laminas/laminas-diactoros", "version": "2.8.0", @@ -2259,6 +2528,84 @@ }, "time": "2022-01-12T15:07:43+00:00" }, + { + "name": "laravel/scout", + "version": "v9.4.5", + "source": { + "type": "git", + "url": "https://github.com/laravel/scout.git", + "reference": "ac962d2e052e90ca271e9bb1b30229ed870c4963" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/scout/zipball/ac962d2e052e90ca271e9bb1b30229ed870c4963", + "reference": "ac962d2e052e90ca271e9bb1b30229ed870c4963", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "illuminate/bus": "^8.0|^9.0", + "illuminate/contracts": "^8.0|^9.0", + "illuminate/database": "^8.0|^9.0", + "illuminate/http": "^8.0|^9.0", + "illuminate/pagination": "^8.0|^9.0", + "illuminate/queue": "^8.0|^9.0", + "illuminate/support": "^8.0|^9.0", + "php": "^7.3|^8.0" + }, + "require-dev": { + "meilisearch/meilisearch-php": "^0.19", + "mockery/mockery": "^1.0", + "orchestra/testbench": "^6.17|^7.0", + "phpunit/phpunit": "^9.3" + }, + "suggest": { + "algolia/algoliasearch-client-php": "Required to use the Algolia engine (^2.2).", + "meilisearch/meilisearch-php": "Required to use the MeiliSearch engine (^0.17)." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "9.x-dev" + }, + "laravel": { + "providers": [ + "Laravel\\Scout\\ScoutServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Laravel\\Scout\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "Laravel Scout provides a driver based solution to searching your Eloquent models.", + "keywords": [ + "algolia", + "laravel", + "search" + ], + "support": { + "issues": "https://github.com/laravel/scout/issues", + "source": "https://github.com/laravel/scout" + }, + "time": "2022-02-22T16:20:15+00:00" + }, { "name": "laravel/serializable-closure", "version": "v1.0.5", @@ -4516,6 +4863,88 @@ ], "time": "2021-09-25T23:10:38+00:00" }, + { + "name": "react/promise", + "version": "v2.9.0", + "source": { + "type": "git", + "url": "https://github.com/reactphp/promise.git", + "reference": "234f8fd1023c9158e2314fa9d7d0e6a83db42910" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/reactphp/promise/zipball/234f8fd1023c9158e2314fa9d7d0e6a83db42910", + "reference": "234f8fd1023c9158e2314fa9d7d0e6a83db42910", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.36" + }, + "type": "library", + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "React\\Promise\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jan Sorgalla", + "email": "jsorgalla@gmail.com", + "homepage": "https://sorgalla.com/" + }, + { + "name": "Christian Lück", + "email": "christian@clue.engineering", + "homepage": "https://clue.engineering/" + }, + { + "name": "Cees-Jan Kiewiet", + "email": "reactphp@ceesjankiewiet.nl", + "homepage": "https://wyrihaximus.net/" + }, + { + "name": "Chris Boden", + "email": "cboden@gmail.com", + "homepage": "https://cboden.dev/" + } + ], + "description": "A lightweight implementation of CommonJS Promises/A for PHP", + "keywords": [ + "promise", + "promises" + ], + "support": { + "issues": "https://github.com/reactphp/promise/issues", + "source": "https://github.com/reactphp/promise/tree/v2.9.0" + }, + "funding": [ + { + "url": "https://github.com/WyriHaximus", + "type": "github" + }, + { + "url": "https://github.com/clue", + "type": "github" + } + ], + "time": "2022-02-11T10:27:51+00:00" + }, { "name": "rhilip/bencode", "version": "v1.1.1", diff --git a/config/explorer.php b/config/explorer.php new file mode 100644 index 00000000..4eaa1edf --- /dev/null +++ b/config/explorer.php @@ -0,0 +1,30 @@ + [ + 'host' => 'localhost', + 'port' => '9200', + 'scheme' => 'http', + ], + + /** + * An index may be defined on an Eloquent model or inline below. A more in depth explanation + * of the mapping possibilities can be found in the documentation of Explorer's repository. + */ + 'indexes' => [ + \App\Models\Torrent::class + ], + + /** + * You may opt to keep the old indices after the alias is pointed to a new index. + * A model is only using index aliases if it implements the Aliased interface. + */ + 'prune_old_aliases' => true, +]; diff --git a/config/scout.php b/config/scout.php new file mode 100644 index 00000000..2caf4b9c --- /dev/null +++ b/config/scout.php @@ -0,0 +1,137 @@ + env('SCOUT_DRIVER', 'elastic'), + + /* + |-------------------------------------------------------------------------- + | Index Prefix + |-------------------------------------------------------------------------- + | + | Here you may specify a prefix that will be applied to all search index + | names used by Scout. This prefix may be useful if you have multiple + | "tenants" or applications sharing the same search infrastructure. + | + */ + + 'prefix' => env('SCOUT_PREFIX', ''), + + /* + |-------------------------------------------------------------------------- + | Queue Data Syncing + |-------------------------------------------------------------------------- + | + | This option allows you to control if the operations that sync your data + | with your search engines are queued. When this is set to "true" then + | all automatic data syncing will get queued for better performance. + | + */ + + 'queue' => env('SCOUT_QUEUE', false), + + /* + |-------------------------------------------------------------------------- + | Database Transactions + |-------------------------------------------------------------------------- + | + | This configuration option determines if your data will only be synced + | with your search indexes after every open database transaction has + | been committed, thus preventing any discarded data from syncing. + | + */ + + 'after_commit' => false, + + /* + |-------------------------------------------------------------------------- + | Chunk Sizes + |-------------------------------------------------------------------------- + | + | These options allow you to control the maximum chunk size when you are + | mass importing data into the search engine. This allows you to fine + | tune each of these chunk sizes based on the power of the servers. + | + */ + + 'chunk' => [ + 'searchable' => 500, + 'unsearchable' => 500, + ], + + /* + |-------------------------------------------------------------------------- + | Soft Deletes + |-------------------------------------------------------------------------- + | + | This option allows to control whether to keep soft deleted records in + | the search indexes. Maintaining soft deleted records can be useful + | if your application still needs to search for the records later. + | + */ + + 'soft_delete' => false, + + /* + |-------------------------------------------------------------------------- + | Identify User + |-------------------------------------------------------------------------- + | + | This option allows you to control whether to notify the search engine + | of the user performing the search. This is sometimes useful if the + | engine supports any analytics based on this application's users. + | + | Supported engines: "algolia" + | + */ + + 'identify' => env('SCOUT_IDENTIFY', false), + + /* + |-------------------------------------------------------------------------- + | Algolia Configuration + |-------------------------------------------------------------------------- + | + | Here you may configure your Algolia settings. Algolia is a cloud hosted + | search engine which works great with Scout out of the box. Just plug + | in your application ID and admin API key to get started searching. + | + */ + + 'algolia' => [ + 'id' => env('ALGOLIA_APP_ID', ''), + 'secret' => env('ALGOLIA_SECRET', ''), + ], + + /* + |-------------------------------------------------------------------------- + | MeiliSearch Configuration + |-------------------------------------------------------------------------- + | + | Here you may configure your MeiliSearch settings. MeiliSearch is an open + | source search engine with minimal configuration. Below, you can state + | the host and key information for your own MeiliSearch installation. + | + | See: https://docs.meilisearch.com/guides/advanced_guides/configuration.html + | + */ + + 'meilisearch' => [ + 'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'), + 'key' => env('MEILISEARCH_KEY', null), + ], + +];