deutsch

Using Sphinx with Symfony

For using Sphinx with Symfony you got to do the following steps:

1.) Install your Symfony app as well as the Sphinx daemon
2.) Configure your Sphinx with proper indizes and start the daemon
3.) create a mySphinxSearch.class.php in your /lib directory
4.) save the PHP sphinx api providey by Sphinx in your /lib directory
5.) create a search function inside the SphinxSearchIndex class in mySphinxSearch.class.php
6.) fill the function with the following code

15 public static function search($query)
16 {
17 $cl = new SphinxClient();
18 $cl->SetServer('127.0.0.1', 3312);
19 $cl->SetMatchMode(SPH_MATCH_EXTENDED);
20
21 // Set Weights (title, description, tags)
22 $cl->SetWeights(array(100,50,100));
23
29 $result = $cl->Query($query, 'document
document_delta document_fulltext
document_fulltext_delta');
30
31 // Check if array key exists (search found
documents) or otherwise return an empty
array
32 if(isset($result['matches']))
33 {
34 return $result['matches'];
35 }
36 else
37 {
38 return array();
39 }
40 }

7. build a function to update the index

145 /**
146 * Launch sphinx indexer to build a new
index
147 *
148 * @return void
149 */
150 public static function reIndex(
$full_rebuild=false,
$debug = false)
151 {
152 // Configuration of sphinx
153 $configDir = sfConfig::get
('sf_config_dir');
154
155 if( $full_rebuild )
156 {
157 $indexes = '--all';
158 }
159 else
160 {
161 $indexes = 'document_delta
document_fulltext_delta';
162 }
165
166 $out = shell_exec("indexer --config
$configDir/sphinx.conf --rotate
$indexes");
167
168 // Show output in debug mode
169 sfContext::getInstance()->getLogger()
->debug("Command execution
result: \n" . $out);
170 if ($debug) echo $out;
171
172 return TRUE;
173 }

8.) in your modules action set


$hits = SphinxSearchIndex::search
($this->query);

where $query is the string to search for.

if you got any questions regarding some values used in here, just leave a comment!

This entry was posted on Wednesday, August 13th, 2008 at 9:54 am and is filed under PHP, Symfony. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply