From 86c98cf1953038c3e2c95fda82edc7f5c7a852b5 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Sat, 14 Mar 2020 11:44:35 +0000 Subject: [PATCH 1/4] Install scout extended --- composer.json | 2 +- composer.lock | 129 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 129 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index a5deecfac..4807f952e 100644 --- a/composer.json +++ b/composer.json @@ -6,13 +6,13 @@ "require": { "php": "^7.3", "algolia/algoliasearch-client-php": "^2.2", + "algolia/scout-extended": "^1.9", "doctrine/dbal": "^2.5", "facade/ignition": "^1.0", "fideloper/proxy": "^4.0", "guzzlehttp/guzzle": "^6.3", "laravel/framework": "^6.0", "laravel/horizon": "^3.2.7", - "laravel/scout": "^8.0", "laravel/socialite": "^4.2", "laravel/tinker": "^1.0", "lasserafn/php-initial-avatar-generator": "^2.0", diff --git a/composer.lock b/composer.lock index e51c402ff..f81bf89c2 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": "b5998e76ef7dfbb514d8ddd36f9cf307", + "content-hash": "2ae675cb4793f850d2faba6076d104fa", "packages": [ { "name": "algolia/algoliasearch-client-php", @@ -76,6 +76,82 @@ ], "time": "2020-03-09T09:11:44+00:00" }, + { + "name": "algolia/scout-extended", + "version": "v1.9.0", + "source": { + "type": "git", + "url": "https://github.com/algolia/scout-extended.git", + "reference": "1ca3d75c21f7dd8d209c044c1447cf54a7fba5c0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/algolia/scout-extended/zipball/1ca3d75c21f7dd8d209c044c1447cf54a7fba5c0", + "reference": "1ca3d75c21f7dd8d209c044c1447cf54a7fba5c0", + "shasum": "" + }, + "require": { + "algolia/algoliasearch-client-php": "^2.5.1", + "ext-json": "*", + "illuminate/console": "^6.0|^7.0", + "illuminate/contracts": "^6.0|^7.0", + "illuminate/database": "^6.0|^7.0", + "illuminate/filesystem": "^6.0|^7.0", + "illuminate/support": "^6.0|^7.0", + "laravel/scout": "^8.0", + "php": "^7.2", + "riimu/kit-phpencoder": "^2.4" + }, + "require-dev": { + "fzaninotto/faker": "^1.8", + "mockery/mockery": "^1.1", + "nunomaduro/larastan": "^0.5", + "orchestra/testbench": "^4.0|^5.0", + "phpstan/phpstan": "^0.12.14", + "phpunit/phpunit": "^8.0|^9.0" + }, + "suggest": { + "ext-dom": "Required to use the HTML Splitter." + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Algolia\\ScoutExtended\\ScoutExtendedServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "Algolia\\ScoutExtended\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nuno Maduro", + "email": "enunomaduro@gmail.com" + }, + { + "name": "Algolia Team", + "email": "contact@algolia.com" + } + ], + "description": "Scout Extended extends Laravel Scout adding algolia-specific features", + "keywords": [ + "algolia", + "analytics", + "extended", + "laravel", + "places", + "scout", + "search" + ], + "time": "2020-03-03T14:27:21+00:00" + }, { "name": "aws/aws-sdk-php", "version": "3.133.35", @@ -3108,6 +3184,57 @@ ], "time": "2020-02-21T04:36:14+00:00" }, + { + "name": "riimu/kit-phpencoder", + "version": "v2.4.0", + "source": { + "type": "git", + "url": "https://github.com/Riimu/Kit-PHPEncoder.git", + "reference": "7e876d25019c3f6c23321ab5ac1a55c72fcd0933" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Riimu/Kit-PHPEncoder/zipball/7e876d25019c3f6c23321ab5ac1a55c72fcd0933", + "reference": "7e876d25019c3f6c23321ab5ac1a55c72fcd0933", + "shasum": "" + }, + "require": { + "php": ">=5.6.0" + }, + "require-dev": { + "phpunit/phpunit": "^7.2 || ^6.5 || ^5.7" + }, + "suggest": { + "ext-gmp": "To convert GMP numbers into PHP code" + }, + "type": "library", + "autoload": { + "psr-4": { + "Riimu\\Kit\\PHPEncoder\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Riikka Kalliomäki", + "email": "riikka.kalliomaki@gmail.com", + "homepage": "http://riimu.net" + } + ], + "description": "Highly customizable alternative to var_export for PHP code generation", + "homepage": "http://kit.riimu.net", + "keywords": [ + "code", + "encoder", + "export", + "generator", + "variable" + ], + "time": "2018-07-03T12:46:23+00:00" + }, { "name": "roave/security-advisories", "version": "dev-master", From 743ac0a47a1617ea644bbbb8f930a79fb27dd37f Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Sat, 14 Mar 2020 11:45:02 +0000 Subject: [PATCH 2/4] Split body content --- app/Models/Thread.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/Models/Thread.php b/app/Models/Thread.php index b2078773b..000fb704a 100644 --- a/app/Models/Thread.php +++ b/app/Models/Thread.php @@ -224,4 +224,23 @@ public function toSearchableArray(): array 'slug' => $this->slug(), ]; } + + public function splitBody($value): array + { + // First split on new paragraph. + return collect(preg_split("/\r\n\r\n/", $value)) + ->flatMap(function ($chunk) { + // If the chunk is still too long, split it on new line. + if (strlen($chunk) > 5000) { + return preg_split("/\r\n/", $chunk); + } + + return [$chunk]; + }) + ->filter(function ($chunk) { + // Ensure there are no empty rows. + return $chunk !== ""; + }) + ->toArray(); + } } From 089de6e86d970da31598d58f3f5f08195dbb0b27 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Sat, 14 Mar 2020 11:45:11 +0000 Subject: [PATCH 3/4] Set index configuration --- config/scout-threads.php | 158 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 config/scout-threads.php diff --git a/config/scout-threads.php b/config/scout-threads.php new file mode 100644 index 000000000..858e4922e --- /dev/null +++ b/config/scout-threads.php @@ -0,0 +1,158 @@ + ['subject', 'body', 'slug'], + + /* + |-------------------------------------------------------------------------- + | Custom Ranking + |-------------------------------------------------------------------------- + | + | Custom Ranking is about leveraging business metrics to effectively rank search + | results - it's crucial for any successful search experience. Make sure that + | only "numeric" attributes are used, such as the number of sales or views. + | + | Supported: Null, Array + | Examples: ['desc(comments_count)', 'desc(views_count)'] + | + */ + + 'customRanking' => null, + + /* + |-------------------------------------------------------------------------- + | Remove Stop Words + |-------------------------------------------------------------------------- + | + | Stop word removal is useful when you have a query in natural language, e.g. + | “what is a record?”. In that case, the engine will remove “what”, “is”, + | before executing the query, and therefore just search for “record”. + | + | Supported: Null, Boolean, Array + | + */ + + 'removeStopWords' => null, + + /* + |-------------------------------------------------------------------------- + | Disable Typo Tolerance + |-------------------------------------------------------------------------- + | + | Algolia provides robust "typo-tolerance" out-of-the-box. This parameter accepts an + | array of attributes for which typo-tolerance should be disabled. This is useful, + | for example, products that might require SKU search without "typo-tolerance". + | + | Supported: Null, Array + | Example: ['id', 'sku', 'reference', 'code'] + | + */ + + 'disableTypoToleranceOnAttributes' => ['slug'], + + /* + |-------------------------------------------------------------------------- + | Attributes For Faceting + |-------------------------------------------------------------------------- + | + | Your index comes with no categories. By designating an attribute as a facet, this enables + | Algolia to compute a set of possible values that can later be used to create categories + | or filters. You can also get a count of records that match those values. + | + | Supported: Null, Array + | Example: ['type', 'filterOnly(country)', 'searchable(city)',] + | + */ + + 'attributesForFaceting' => null, + + /* + |-------------------------------------------------------------------------- + | Unretrievable Attributes + |-------------------------------------------------------------------------- + | + | This is particularly important for security or business reasons, where some attributes are + | used only for ranking or other technical purposes, but should never be seen by your end + | users, such us: total_sales, permissions, stock_count, and other private information. + | + | Supported: Null, Array + | Example: ['total_sales', 'permissions', 'stock_count',] + | + */ + + 'unretrievableAttributes' => null, + + /* + |-------------------------------------------------------------------------- + | Ignore Plurals + |-------------------------------------------------------------------------- + | + | Treats singular, plurals, and other forms of declensions as matching terms. When + | enabled, will make the engine consider “car” and “cars”, or “foot” and “feet”, + | equivalent. This is used in conjunction with the "queryLanguages" setting. + | + | Supported: Null, Boolean, Array + | + */ + + 'ignorePlurals' => null, + + /* + |-------------------------------------------------------------------------- + | Query Languages + |-------------------------------------------------------------------------- + | + | Sets the languages to be used by language-specific settings such as + | "removeStopWords" or "ignorePlurals". For optimum relevance, it is + | recommended to only enable languages that are used in your data. + | + | Supported: Null, Array + | Example: ['en', 'fr',] + | + */ + + 'queryLanguages' => ['en'], + + /* + |-------------------------------------------------------------------------- + | Distinct + |-------------------------------------------------------------------------- + | + | Using this attribute, you can limit the number of returned records that contain the same + | value in that attribute. For example, if the distinct attribute is the series_name and + | several hits (Episodes) have the same value for series_name (Laravel From Scratch). + | + | Supported(distinct): Boolean + | Supported(attributeForDistinct): Null, String + | Example(attributeForDistinct): 'slug' + */ + + 'distinct' => true, + 'attributeForDistinct' => 'slug', + + /* + |-------------------------------------------------------------------------- + | Other Settings + |-------------------------------------------------------------------------- + | + | The easiest way to manage your settings is usually to go to your Algolia dashboard because + | it has a nice UI and you can test the relevancy directly there. Once you fine-tuned your + | configuration, just use the command `scout:sync` to get remote settings in this file. + | + */ +]; From 3c582f8c9e8563535b4c7d6a63944fdb380f9fe9 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Wed, 18 Mar 2020 20:02:44 +0000 Subject: [PATCH 4/4] Apply fixes from StyleCI --- app/Models/Thread.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Models/Thread.php b/app/Models/Thread.php index 000fb704a..207505622 100644 --- a/app/Models/Thread.php +++ b/app/Models/Thread.php @@ -239,7 +239,7 @@ public function splitBody($value): array }) ->filter(function ($chunk) { // Ensure there are no empty rows. - return $chunk !== ""; + return $chunk !== ''; }) ->toArray(); }