Skip to content
Snippets Groups Projects
Commit 36b7312e authored by Jared Hancock's avatar Jared Hancock
Browse files

search: Simplify MySQL indexing logic

parent af0baa9b
No related branches found
No related tags found
No related merge requests found
...@@ -386,12 +386,10 @@ class MysqlSearchBackend extends SearchBackend { ...@@ -386,12 +386,10 @@ class MysqlSearchBackend extends SearchBackend {
$title = Format::searchable($row[1]); $title = Format::searchable($row[1]);
if (!$body && !$title) if (!$body && !$title)
continue; continue;
$records[] = array('H', $row[0], $title, $body); $record = array('H', $row[0], $title, $body);
if (count($records) > self::$BATCH_SIZE) if (!$this->__index($record))
if (null === ($records = self::__searchFlush($records))) return;
return;
} }
$records = self::__searchFlush($records);
// TICKETS ---------------------------------- // TICKETS ----------------------------------
...@@ -409,14 +407,12 @@ class MysqlSearchBackend extends SearchBackend { ...@@ -409,14 +407,12 @@ class MysqlSearchBackend extends SearchBackend {
foreach ($cdata as $k=>$a) foreach ($cdata as $k=>$a)
if ($k != 'subject' && ($v = $a->getSearchable())) if ($k != 'subject' && ($v = $a->getSearchable()))
$content[] = $v; $content[] = $v;
$records[] = array('T', $ticket->getId(), $record = array('T', $ticket->getId(),
Format::searchable($ticket->getSubject()), Format::searchable($ticket->getNumber().' '.$ticket->getSubject()),
implode("\n", $content)); implode("\n", $content));
if (count($records) > self::$BATCH_SIZE) if (!$this->__index($record))
if (null === ($records = self::__searchFlush($records))) return;
return;
} }
$records = self::__searchFlush($records);
// USERS ------------------------------------ // USERS ------------------------------------
...@@ -437,14 +433,12 @@ class MysqlSearchBackend extends SearchBackend { ...@@ -437,14 +433,12 @@ class MysqlSearchBackend extends SearchBackend {
foreach ($e->getAnswers() as $a) foreach ($e->getAnswers() as $a)
if ($c = $a->getSearchable()) if ($c = $a->getSearchable())
$content[] = $c; $content[] = $c;
$records[] = array('U', $user->getId(), $record = array('U', $user->getId(),
Format::searchable($user->getFullName()), Format::searchable($user->getFullName()),
trim(implode("\n", $content))); trim(implode("\n", $content)));
if (count($records) > self::$BATCH_SIZE) if (!$this->__index($record))
if (null === ($records = self::__searchFlush($records))) return;
return;
} }
$records = self::__searchFlush($records);
// ORGANIZATIONS ---------------------------- // ORGANIZATIONS ----------------------------
...@@ -463,13 +457,12 @@ class MysqlSearchBackend extends SearchBackend { ...@@ -463,13 +457,12 @@ class MysqlSearchBackend extends SearchBackend {
foreach ($e->getAnswers() as $a) foreach ($e->getAnswers() as $a)
if ($c = $a->getSearchable()) if ($c = $a->getSearchable())
$content[] = $c; $content[] = $c;
$records[] = array('O', $org->getId(), $record = array('O', $org->getId(),
Format::searchable($org->getName()), Format::searchable($org->getName()),
trim(implode("\n", $content))); trim(implode("\n", $content)));
if (count($records) > self::$BATCH_SIZE) if (!$this->__index($record))
$records = self::__searchFlush($records); return null;
} }
$records = self::__searchFlush($records);
// KNOWLEDGEBASE ---------------------------- // KNOWLEDGEBASE ----------------------------
...@@ -483,35 +476,48 @@ class MysqlSearchBackend extends SearchBackend { ...@@ -483,35 +476,48 @@ class MysqlSearchBackend extends SearchBackend {
while ($row = db_fetch_row($res)) { while ($row = db_fetch_row($res)) {
$faq = FAQ::lookup($row[0]); $faq = FAQ::lookup($row[0]);
$records[] = array('K', $faq->getId(), $q = $faq->getQuestion();
Format::searchable($faq->getQuestion()), if ($k = $faq->getKeywords())
$q = $k.' '.$q;
$record = array('K', $faq->getId(),
Format::searchable($q),
$faq->getSearchableAnswer()); $faq->getSearchableAnswer());
if (count($records) > self::$BATCH_SIZE) if (!$this->__index($record))
if (null === ($records = self::__searchFlush($records))) return;
return;
} }
$records = self::__searchFlush($records);
// FILES ------------------------------------ // FILES ------------------------------------
// Flush non-full batch of records
$this->__index(null, true);
} }
function __searchFlush($records) { function __index($record, $force_flush=false) {
if (!$records) static $queue = array();
return $records;
if ($record)
$queue[] = $record;
elseif (!$queue)
return;
foreach ($records as &$r) if (!$force_flush && count($queue) < $this::$BATCH_SIZE)
return true;
foreach ($queue as &$r)
$r = sprintf('(%s)', implode(',', db_input($r))); $r = sprintf('(%s)', implode(',', db_input($r)));
unset($r); unset($r);
$sql = 'INSERT INTO `'.TABLE_PREFIX.'_search` (`object_type`, `object_id`, `title`, `content`) $sql = 'INSERT INTO `'.TABLE_PREFIX.'_search` (`object_type`, `object_id`, `title`, `content`)
VALUES '.implode(',', $records); VALUES '.implode(',', $queue);
if (!db_query($sql) || count($records) != db_affected_rows()) if (!db_query($sql) || count($queue) != db_affected_rows())
throw new Exception('Unable to index content'); throw new Exception('Unable to index content');
$queue = array();
if (!--$this->max_batches) if (!--$this->max_batches)
return null; return null;
return array(); return true;
} }
} }
MysqlSearchBackend::register(); MysqlSearchBackend::register();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment