FIX: Change search to fully match the query (#145)

FIX: Change search to fully match the query (#145)

This changes the search algorithm to select topics whose title matches all words from the search query. It used to match topics that contained at least one word from the search query.

diff --git a/assets/javascripts/discourse/initializers/add-search-results.js b/assets/javascripts/discourse/initializers/add-search-results.js
index 56c8a55..68347fc 100644
--- a/assets/javascripts/discourse/initializers/add-search-results.js
+++ b/assets/javascripts/discourse/initializers/add-search-results.js
@@ -61,7 +61,8 @@ function addEncryptedSearchResultsFromCache(cache, results) {
   const terms = results.grouped_search_result.term
     .toLowerCase()
     .trim()
-    .split(/\s+/);
+    .split(/\s+/)
+    .filter((term) => !term.startsWith("@") && !term.includes(":"));
 
   // Add to results encrypted topics that have matching titles
   const existentTopicIds = new Set(results.topics.map((topic) => topic.id));
@@ -71,21 +72,10 @@ function addEncryptedSearchResultsFromCache(cache, results) {
       return;
     }
 
-    const matchedWordsCount = terms.filter((term) =>
-      topic.title.toLowerCase().includes(term)
-    ).length;
-    if (matchedWordsCount === 0) {
-      return;
+    if (terms.every((term) => topic.title.toLowerCase().includes(term))) {
+      topics[topic.id] = topic = Topic.create(topic);
+      results.topics.unshift(topic);
     }
-
-    topics[topic.id] = topic = Topic.create(topic);
-    results.topics.unshift(topic);
-
-    topic.set(
-      "searchPriority",
-      matchedWordsCount +
-        matchedWordsCount / topic.title.trim().split(/\s+/).length
-    );
   });
 
   // Add associated posts for each new topic
@@ -104,11 +94,6 @@ function addEncryptedSearchResultsFromCache(cache, results) {
     results.grouped_search_result.post_ids.unshift(post.id);
   });
 
-  // Move new encrypted results to top because they might be more relevant
-  results.posts.sort(
-    (a, b) => (b.topic.searchPriority || 0) - (a.topic.searchPriority || 0)
-  );
-
   // Reset topic_title_headline for encrypted results
   results.posts.map((post) => {
     if (cache.topics[post.topic_id]) {

GitHub sha: 621fe0406ac9d792e57d5d839b136dee7e07dfe9

This commit appears in #145 which was approved by SamSaffron. It was merged by SamSaffron.