1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
4 include MTRACK_INC_DIR . '/search/lucene.php';
5 include MTRACK_INC_DIR . '/search/solr.php';
7 class MTrackSearchResult {
8 /** object identifier of result */
10 /** result ranking; higher is more relevant */
12 /** excerpt of matching text */
15 /* some implementations may need the caller to provide the context
16 * text; the default just returns what is there */
17 function getExcerpt($text) {
18 return $this->excerpt;
22 interface IMTrackSearchEngine {
23 public function setBatchMode();
24 public function commit($optimize = false);
25 public function add($object, $fields, $replace = false);
26 /** returns an array of MTrackSearchResult objects corresponding
27 * to matches to the supplied query string */
28 public function search($query);
31 class MTrackSearchDB {
33 static $engine = null;
35 static function getEngine() {
36 if (self::$engine === null) {
37 $name = MTrackConfig::get('core', 'search_engine');
38 if (!$name) $name = 'MTrackSearchEngineLucene';
39 self::$engine = new $name;
44 /* functions that can perform indexing */
45 static $funcs = array();
47 static function register_indexer($id, $func)
49 self::$funcs[$id] = $func;
52 static function index_object($id)
55 while (strlen($key)) {
56 if (isset(self::$funcs[$key])) {
59 $new_key = preg_replace('/:[^:]+$/', '', $key);
60 if ($key == $new_key) {
66 if (isset(self::$funcs[$key])) {
67 $func = self::$funcs[$key];
68 return call_user_func($func, $id);
73 static function get() {
74 return self::getEngine()->getIdx();
77 static function setBatchMode() {
78 self::getEngine()->setBatchMode();
81 static function commit($optimize = false) {
82 self::getEngine()->commit($optimize);
85 static function add($object, $fields, $replace = false) {
86 self::getEngine()->add($object, $fields, $replace);
89 static function search($query) {
90 return self::getEngine()->search($query);