From baaf4c3043f428ec009dd48b85e1c2d6cb8abee4 Mon Sep 17 00:00:00 2001 From: Rob Hoelz Date: Mon, 14 Apr 2014 23:18:33 -0500 Subject: Make search mechanism pluggable Currently, TinyTinyRSS can use raw SQL or the Sphinx search engine for searching. It would be nice if other search engines (such as Xapian) could be used, or if features of the underlying SQL engine (such as MySQL's FULLTEXT indexes) could be leveraged. This commit makes searching into a plugin hook, falling back to the builtin behavior if no search plugin is active. The Sphinx search behavior has been broken out into a plugin. --- plugins/search_sphinx/init.php | 60 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 plugins/search_sphinx/init.php (limited to 'plugins/search_sphinx/init.php') diff --git a/plugins/search_sphinx/init.php b/plugins/search_sphinx/init.php new file mode 100644 index 000000000..f2877e44d --- /dev/null +++ b/plugins/search_sphinx/init.php @@ -0,0 +1,60 @@ +add_hook($host::HOOK_SEARCH, $this); + + require_once __DIR__ . "/sphinxapi.php"; + } + + function hook_search($search) { + $offset = 0; + $limit = 500; + + $sphinxClient = new SphinxClient(); + + $sphinxpair = explode(":", SPHINX_SERVER, 2); + + $sphinxClient->SetServer($sphinxpair[0], (int)$sphinxpair[1]); + $sphinxClient->SetConnectTimeout(1); + + $sphinxClient->SetFieldWeights(array('title' => 70, 'content' => 30, + 'feed_title' => 20)); + + $sphinxClient->SetMatchMode(SPH_MATCH_EXTENDED2); + $sphinxClient->SetRankingMode(SPH_RANK_PROXIMITY_BM25); + $sphinxClient->SetLimits($offset, $limit, 1000); + $sphinxClient->SetArrayResult(false); + $sphinxClient->SetFilter('owner_uid', array($_SESSION['uid'])); + + $result = $sphinxClient->Query($search, SPHINX_INDEX); + + $ids = array(); + + if (is_array($result['matches'])) { + foreach (array_keys($result['matches']) as $int_id) { + $ref_id = $result['matches'][$int_id]['attrs']['ref_id']; + array_push($ids, $ref_id); + } + } + + $ids = join(",", $ids); + + if ($ids) + return array("ref_id IN ($ids)", array()); + else + return array("ref_id = -1", array()); + } + + function api_version() { + return 2; + } +} +?> -- cgit v1.2.3-54-g00ecf From af2c15f3d3bf922a333c50c9fa6629bfb8465645 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 23 Apr 2014 04:53:09 +0000 Subject: mention config.php settings in search_sphinx plugin description --- config.php-dist | 6 +++--- plugins/search_sphinx/init.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'plugins/search_sphinx/init.php') diff --git a/config.php-dist b/config.php-dist index 2d7a6d195..0c3e92b74 100644 --- a/config.php-dist +++ b/config.php-dist @@ -104,9 +104,9 @@ // Enable client PubSubHubbub support in tt-rss. When disabled, tt-rss // won't try to subscribe to PUSH feed updates. - // ********************* - // *** Sphinx search *** - // ********************* + // **************************** + // *** Sphinx search plugin *** + // **************************** define('SPHINX_SERVER', 'localhost:9312'); // Hostname:port combination for the Sphinx server. diff --git a/plugins/search_sphinx/init.php b/plugins/search_sphinx/init.php index f2877e44d..a6511034d 100644 --- a/plugins/search_sphinx/init.php +++ b/plugins/search_sphinx/init.php @@ -3,7 +3,7 @@ class Search_Sphinx extends Plugin { function about() { return array(1.0, - "Delegate searching for articles to Sphinx", + "Delegate searching for articles to Sphinx (don't forget to set options in config.php)", "hoelzro", true); } -- cgit v1.2.3-54-g00ecf From edd882ede443a077cb62e57265a7aa06447caeee Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 23 Apr 2014 05:01:13 +0000 Subject: search_sphinx: abort if system client library exists --- plugins/search_sphinx/init.php | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'plugins/search_sphinx/init.php') diff --git a/plugins/search_sphinx/init.php b/plugins/search_sphinx/init.php index a6511034d..557b2682c 100644 --- a/plugins/search_sphinx/init.php +++ b/plugins/search_sphinx/init.php @@ -11,6 +11,10 @@ class Search_Sphinx extends Plugin { function init($host) { $host->add_hook($host::HOOK_SEARCH, $this); + if (class_exists("SphinxClient")) { + user_error("Your PHP has a separate systemwide Sphinx client installed which conflicts with the client library used by tt-rss. Either remove the system library or disable Sphinx support."); + } + require_once __DIR__ . "/sphinxapi.php"; } -- cgit v1.2.3-54-g00ecf