Skip to content

Commit 775d6a7

Browse files
committed
Optimize the search action.
1 parent 3939081 commit 775d6a7

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

server/actions/patterns/search.php

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,52 +32,25 @@ public function execute() {
3232
$this->userProfile = $this->getUserProfile();
3333

3434
$result = null;
35-
$item = null;
3635

37-
// The default community search is cached in Maintenance.php
38-
if (empty($query)) {
39-
// $item = \core\Cache::LoadItem(\core\Cache::CommunityKey($query, $startIndex, $limit));
40-
}
41-
42-
// Disable cache, for testing
43-
$item = null;
44-
45-
if (!is_null($item)) {
46-
$result = $item;
47-
} else {
48-
$result = $this->searchCommunity($query, $startIndex, $limit, $type);
49-
}
36+
$result = $this->searchCommunity($query, $startIndex, $limit, $type);
5037

5138
return new \core\Result($result);
5239
}
5340

5441
public function searchCommunity($query, $startIndex, $limit, $type) {
5542
// Build the search query.
56-
$whereStatements = array();
43+
$whereStatements = [];
5744
$searchSqlParams = [];
5845

5946
// Search everything using the query.
6047
if (!empty($query)) {
61-
$whereStatements[] = " p.name LIKE ?";
62-
$whereStatements[] = " p.description LIKE ?";
63-
$whereStatements[] = " p.author LIKE ?";
64-
65-
$preparedQuery = "%{$query}%";
66-
$searchSqlParams[] = ["s", $preparedQuery];
67-
$searchSqlParams[] = ["s", $preparedQuery];
68-
$searchSqlParams[] = ["s", $preparedQuery];
48+
$whereStatements[] = "MATCH(`name`, `description`, `pattern`, `replace`, `author`) AGAINST(? IN NATURAL LANGUAGE MODE)";
49+
$searchSqlParams[] = ["s", $query];
6950
}
7051

7152
// Do the actual search.
72-
$q = "SELECT p.*, urJoin.rating AS userRating, fJoin.patternId as favorite
73-
FROM patterns p
74-
LEFT JOIN userRatings urJoin ON urJoin.patternId = p.id AND urJoin.userId = ?
75-
LEFT JOIN favorites as fJoin ON fJoin.userId = ? AND fJoin.patternId=p.id
76-
WHERE p.visibility='public'
77-
";
78-
79-
$searchSqlParams[] = ["s", $this->userProfile->userId];
80-
$searchSqlParams[] = ["s", $this->userProfile->userId];
53+
$q = "SELECT p.* FROM patterns p WHERE p.visibility='public'";
8154

8255
if (!is_null($type)) {
8356
$typeArray = quoteStringArray($type);
@@ -94,6 +67,37 @@ public function searchCommunity($query, $startIndex, $limit, $type) {
9467

9568
$result = $this->db->execute($q, $searchSqlParams);
9669

70+
// Inject userRating and favorite
71+
$patternIds = quoteStringArray(array_map(function ($pattern) {
72+
return idx($pattern, 'id');
73+
}, $result));
74+
75+
$userId = $this->userProfile->userId;
76+
77+
$userRatings = $this->db->execute("SELECT rating, patternId FROM userRatings WHERE patternId IN ($patternIds) AND userId=?", [
78+
['s', $userId]
79+
]);
80+
81+
$userFavorites = $this->db->execute("SELECT patternId FROM favorites WHERE patternId IN ($patternIds) AND userId=?", [
82+
['s', $userId]
83+
]);
84+
85+
function injectIntoResults($result, $sourceList, $sourceKey, $destKey)
86+
{
87+
for ($i = 0; $i < count($sourceList); $i++) {
88+
$sourceValue = $sourceList[$i];
89+
for ($j = 0; $j < count($result); $j++) {
90+
if (idx($result[$j], 'id') === $sourceValue->patternId) {
91+
$result[$i]->{$destKey} = idx($result[$j], $sourceKey, true);
92+
break;
93+
}
94+
}
95+
}
96+
}
97+
98+
injectIntoResults($result, $userRatings, 'rating', 'userRating');
99+
injectIntoResults($result, $userFavorites, null, 'favorite');
100+
97101
$json = createPatternSet($result);
98102

99103
return $json;
@@ -106,10 +110,10 @@ function getFlavorValues() {
106110
public function getSchema() {
107111
$flavorValues = $this->getFlavorValues();
108112
return array(
109-
"query" => array("type"=>self::STRING, "required"=>true),
110-
"startIndex" => array("type"=>self::NUMBER, "required"=>false, "default"=>0),
111-
"limit" => array("type"=>self::NUMBER, "required"=>false, "default"=>100),
112-
"flavor" => array("type"=>self::ENUM_ARRAY, "values"=>$flavorValues, "default"=>$flavorValues, "required"=>false)
113+
"query" => array("type" => self::STRING, "required" => true),
114+
"startIndex" => array("type" => self::NUMBER, "required" => false, "default" => 0),
115+
"limit" => array("type" => self::NUMBER, "required" => false, "default" => 100),
116+
"flavor" => array("type" => self::ENUM_ARRAY, "values" => $flavorValues, "default" => $flavorValues, "required" => false)
113117
);
114118
}
115119
}

0 commit comments

Comments
 (0)