From 5ee43e7df5fdb27db768707d22a5caaceddaa2e1 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Wed, 30 Sep 2015 20:38:08 -0500
Subject: [PATCH] search: Avoid searching for very short terms

---
 include/ajax.orgs.php          | 3 +++
 include/ajax.tickets.php       | 5 +++--
 include/ajax.users.php         | 3 +++
 include/class.search.php       | 5 +++++
 include/client/tickets.inc.php | 4 ++--
 include/staff/tickets.inc.php  | 6 ++++--
 6 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/include/ajax.orgs.php b/include/ajax.orgs.php
index 202d2d8f0..0cf4f22d6 100644
--- a/include/ajax.orgs.php
+++ b/include/ajax.orgs.php
@@ -29,6 +29,9 @@ class OrgsAjaxAPI extends AjaxController {
         $q = $_REQUEST['q'];
         $limit = isset($_REQUEST['limit']) ? (int) $_REQUEST['limit']:25;
 
+        if (strlen($q) < 2)
+            return $this->encode(array());
+
         $orgs = Organization::objects()
             ->values_flat('id', 'name')
             ->limit($limit);
diff --git a/include/ajax.tickets.php b/include/ajax.tickets.php
index 9a5822e12..35bb513e9 100644
--- a/include/ajax.tickets.php
+++ b/include/ajax.tickets.php
@@ -47,8 +47,9 @@ class TicketsAjaxAPI extends AjaxController {
             ->limit($limit);
 
         $q = $_REQUEST['q'];
-        // Drop at sign in email addresses
-        $q = str_replace('@', ' ', $q);
+
+        if (strlen($q) < 2)
+            return $this->encode(array());
 
         global $ost;
         $hits = $ost->searcher->find($q, $hits)
diff --git a/include/ajax.users.php b/include/ajax.users.php
index 03e203000..bb94a1528 100644
--- a/include/ajax.users.php
+++ b/include/ajax.users.php
@@ -34,6 +34,9 @@ class UsersAjaxAPI extends AjaxController {
         $users=array();
         $emails=array();
 
+        if (strlen($q) < 2)
+            return $this->encode(array());
+
         if (!$type || !strcasecmp($type, 'remote')) {
             foreach (AuthenticationBackend::searchUsers($q) as $u) {
                 $name = new UsersName(array('first' => $u['first'], 'last' => $u['last']));
diff --git a/include/class.search.php b/include/class.search.php
index e4106eaee..474fa3132 100644
--- a/include/class.search.php
+++ b/include/class.search.php
@@ -327,6 +327,11 @@ class MysqlSearchBackend extends SearchBackend {
     function find($query, QuerySet $criteria, $addRelevance=true) {
         global $thisstaff;
 
+        // MySQL usually doesn't handle words shorter than three letters
+        // (except with special configuration)
+        if (strlen($query) < 3)
+            return $criteria;
+
         $criteria = clone $criteria;
 
         $mode = ' IN NATURAL LANGUAGE MODE';
diff --git a/include/client/tickets.inc.php b/include/client/tickets.inc.php
index cf3c5e1d9..f37874b55 100644
--- a/include/client/tickets.inc.php
+++ b/include/client/tickets.inc.php
@@ -90,10 +90,10 @@ if ($thisclient->canSeeOrgTickets()) {
 
 // Perform basic search
 if ($settings['keywords']) {
-    $q = $settings['keywords'];
+    $q = trim($settings['keywords']);
     if (is_numeric($q)) {
         $tickets->filter(array('number__startswith'=>$q));
-    } else { //Deep search!
+    } elseif (strlen($q) > 2) { //Deep search!
         // Use the search engine to perform the search
         $tickets = $ost->searcher->find($q, $tickets);
     }
diff --git a/include/staff/tickets.inc.php b/include/staff/tickets.inc.php
index a3701e627..929e27eda 100644
--- a/include/staff/tickets.inc.php
+++ b/include/staff/tickets.inc.php
@@ -93,8 +93,10 @@ case 'search':
                 ));
             }
         }
-        elseif ($_REQUEST['query']) {
-            $q = trim($_REQUEST['query']);
+        elseif (isset($_REQUEST['query'])
+            && ($q = trim($_REQUEST['query']))
+            && strlen($q) > 2
+        ) {
             // [Search] click, consider keywords
             $__tickets = $ost->searcher->find($q, $tickets);
             if (!count($__tickets) && preg_match('`\w$`u', $q)) {
-- 
GitLab