From 71ff58ef82151325d1770d0d39f385d3fbeb63b0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 29 Jul 2025 10:59:40 +0000 Subject: [PATCH 1/3] Initial plan From f9272454e3d98d8090bb965b8397b8b11fea019e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 29 Jul 2025 11:12:36 +0000 Subject: [PATCH 2/3] Fix search suggest to respect search type filter Co-authored-by: samdark <47294+samdark@users.noreply.github.com> --- assets/src/js/search.js | 3 +++ controllers/SearchController.php | 14 ++++++++-- models/search/SearchActiveRecord.php | 37 +++++++++++++++++++------- views/layouts/partials/_searchForm.php | 3 +++ 4 files changed, 46 insertions(+), 11 deletions(-) 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..829e05fc 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 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', From e8ce83c3ac32f929b3e7bd4c53681f91a246bd36 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 29 Jul 2025 11:17:23 +0000 Subject: [PATCH 3/3] Fix opensearch method call after adding type parameter Co-authored-by: samdark <47294+samdark@users.noreply.github.com> --- controllers/SearchController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/SearchController.php b/controllers/SearchController.php index 829e05fc..21a6018b 100644 --- a/controllers/SearchController.php +++ b/controllers/SearchController.php @@ -220,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;