diff --git a/assets/src/js/search.js b/assets/src/js/search.js index e7f31717..5f47f825 100644 --- a/assets/src/js/search.js +++ b/assets/src/js/search.js @@ -148,6 +148,9 @@ searchSuggest = function (query) { if (typeof yiiSearchLanguage != 'undefined') { apiUrl += '&language=' + encodeURIComponent(yiiSearchLanguage); } + if (typeof yiiSearchType != 'undefined') { + apiUrl += '&type=' + encodeURIComponent(yiiSearchType); + } $.ajax({ diff --git a/controllers/SearchController.php b/controllers/SearchController.php index ea0eba29..21a6018b 100644 --- a/controllers/SearchController.php +++ b/controllers/SearchController.php @@ -79,13 +79,23 @@ public function actionGlobal($q = null, $version = null, $language = null, $type ); } - public function actionSuggest($q, $version = null, $language = null) + public function actionSuggest($q, $version = null, $language = null, $type = null) { try { $q = $this->trimLongQuery($q); + if (!in_array($version, $this->getVersions(), true)) { + $version = null; + } + if (!array_key_exists($language, $this->getLanguages())) { + $language = null; + } + if (!array_key_exists($type, $this->getTypes())) { + $type = null; + } + Yii::$app->response->format = Response::FORMAT_JSON; - $query = SearchActiveRecord::searchAsYouType($q, $version, $language); + $query = SearchActiveRecord::searchAsYouType($q, $version, $language, $type); $results = $query->search()['suggest']; } catch (\yii\base\InvalidArgumentException $e) { // if input breaks the search, provide empty result @@ -210,7 +220,7 @@ public function actionOpensearchSuggest($q) $q = $this->trimLongQuery($q); Yii::$app->response->format = Response::FORMAT_JSON; - $query = SearchActiveRecord::searchAsYouType($q, null, null); + $query = SearchActiveRecord::searchAsYouType($q, null, null, null); $results = $query->search()['suggest']; Yii::$app->response->format = Response::FORMAT_RAW; diff --git a/models/search/SearchActiveRecord.php b/models/search/SearchActiveRecord.php index 5c52811c..d9362e97 100644 --- a/models/search/SearchActiveRecord.php +++ b/models/search/SearchActiveRecord.php @@ -222,7 +222,7 @@ public static function search($queryString, $version = null, $language = null, $ return $query; } - public static function searchAsYouType($queryString, $version = null, $language = null) + public static function searchAsYouType($queryString, $version = null, $language = null, $type = null) { $query = static::find(); @@ -236,14 +236,33 @@ public static function searchAsYouType($queryString, $version = null, $language $indexes = [static::index() . "-$language"]; } - $types = [ - SearchApiType::TYPE, - // SearchApiPrimitive::TYPE, - SearchGuideSection::TYPE, - SearchWiki::TYPE, - SearchExtension::TYPE, - SearchNews::TYPE - ]; + if ($type === null) { + $types = [ + SearchApiType::TYPE, + // SearchApiPrimitive::TYPE, + SearchGuideSection::TYPE, + SearchWiki::TYPE, + SearchExtension::TYPE, + SearchNews::TYPE + ]; + } elseif (in_array($type, [self::SEARCH_WIKI, self::SEARCH_EXTENSION, self::SEARCH_NEWS], true)) { + $types = [$type]; + } elseif ($type === self::SEARCH_API) { + $types = [SearchApiType::TYPE,/* SearchApiPrimitive::TYPE,*/]; + } elseif ($type === self::SEARCH_GUIDE) { + $types = [SearchGuideSection::TYPE]; + } else { + // fallback to all types if unknown type is provided + $types = [ + SearchApiType::TYPE, + // SearchApiPrimitive::TYPE, + SearchGuideSection::TYPE, + SearchWiki::TYPE, + SearchExtension::TYPE, + SearchNews::TYPE + ]; + } + $query->from($indexes, $types); // TODO filter by version if possible $query->addSuggester('suggest-title', [ diff --git a/views/layouts/partials/_searchForm.php b/views/layouts/partials/_searchForm.php index ada9d7c3..44399ef4 100644 --- a/views/layouts/partials/_searchForm.php +++ b/views/layouts/partials/_searchForm.php @@ -17,6 +17,9 @@ } elseif (isset($this->context->searchScope)) { $url['type'] = $this->context->searchScope; } +if (!empty($url['type'])) { + $this->registerJs('yiiSearchType = ' . \yii\helpers\Json::htmlEncode($url['type']), \yii\web\View::POS_HEAD); +} $searchPlaceholder = trim(implode(' ', [ 'Search',