From f35b42b8d0958ee7d0313ae56fa75ad28571f6ea Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 15 Jan 2020 14:35:15 +0100 Subject: [PATCH] wip #3323 @0.75 --- .../Console/Commands/SearchIndexCommand.php | 4 +- src/app/Magic/Search.php | 38 +++++++++++++++---- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/app/Console/Commands/SearchIndexCommand.php b/src/app/Console/Commands/SearchIndexCommand.php index 27c1928..a573157 100644 --- a/src/app/Console/Commands/SearchIndexCommand.php +++ b/src/app/Console/Commands/SearchIndexCommand.php @@ -16,12 +16,12 @@ use Cubist\Util\XML\DOMSelector; class SearchIndexCommand extends Command { - protected $signature = 'cubist:search:index {locale=all}'; + protected $signature = 'cubist:search:index {variant=all} {locale=all}'; protected $description = 'Index data for search engine'; public function handle() { - Search::index($this->argument('locale')); + Search::index($this->argument('variant'), $this->argument('locale')); } diff --git a/src/app/Magic/Search.php b/src/app/Magic/Search.php index d4d3f64..2a2f06f 100644 --- a/src/app/Magic/Search.php +++ b/src/app/Magic/Search.php @@ -4,6 +4,7 @@ namespace Cubist\Backpack\app\Magic; use Cubist\Backpack\app\Magic\Models\Locale; use Cubist\Backpack\app\Magic\Menu\Menu; +use Cubist\Backpack\Facades\App; use Cubist\Util\XML\DOMSelector; use Cviebrock\LaravelElasticsearch\Facade as Elasticsearch; @@ -129,20 +130,33 @@ class Search ] ]; - public static function index($locale = 'all') + public static function index($variant = 'all', $locale = 'all') + { + if ($variant !== 'all') { + self::indexVariant($variant, $locale); + return; + } + + foreach (App::getVariants() as $v) { + self::indexVariant($v, $locale); + } + } + + public static function indexVariant($variant, $locale = 'all') { if ($locale !== 'all') { - self::indexLocale($locale); + self::indexLocale($variant, $locale); return; } foreach (Locale::getLocales() as $l) { - self::indexLocale($l->locale); + self::indexLocale($variant, $l->locale); } } - public static function indexLocale($locale) + + public static function indexLocale($variant, $locale) { - $index = config('cubist.internal_search_index') . '_' . $locale; + $index = self::_getIndexKey($locale, $variant); try { Elasticsearch::indices()->delete(['index' => $index]); @@ -170,7 +184,7 @@ class Search ]); /** @var Cubist\Backpack\app\Magic\Menu\Item[] $pages */ - $pages = Menu::getAllNavigablePages($locale); + $pages = Menu::getAllNavigablePages($locale, $variant); $indexed = []; foreach ($pages as $url => $page) { @@ -256,9 +270,17 @@ class Search return self::$_analysis[$locale]; } - public static function query($term, $locale, $type = null, $limit = null) + protected static function _getIndexKey($locale, $variant = null) + { + if (null === $variant) { + $variant = App::getVariant(); + } + return md5(config('cubist.internal_search_index') . '_' . $variant . '_' . $locale); + } + + public static function query($term, $locale, $variant = null, $type = null, $limit = null) { - $index = config('cubist.internal_search_index') . '_' . $locale; + $index = self::_getIndexKey($locale, $variant); if (null === $limit) { $limit = 50; } -- 2.39.5