Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions assets/src/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ searchSuggest = function (query) {
if (typeof yiiSearchLanguage != 'undefined') {
apiUrl += '&language=' + encodeURIComponent(yiiSearchLanguage);
}
if (typeof yiiSearchType != 'undefined') {
apiUrl += '&type=' + encodeURIComponent(yiiSearchType);
}


$.ajax({
Expand Down
16 changes: 13 additions & 3 deletions controllers/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
37 changes: 28 additions & 9 deletions models/search/SearchActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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', [
Expand Down
3 changes: 3 additions & 0 deletions views/layouts/partials/_searchForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down