Install. Environment: win7 64 bit
1. Download the sphinx file package
Download address: http://sphinxsearch.com/downloads/archive/
2. Unzip to D:/sphinx. Create new folders data and log, and import the example.sql file in the local test library. The structure is as follows
3. Configure and copy the sphinx.conf.in file to the bin directory. Rename to sphinx.conf. The configuration content is as follows. What each line means, I can't tell at the moment, let's follow up...
Note two points: the red is the content that I have modified, and the place with the yellow background needs to be named all the time.
# configure the data source source src1 { type = mysql sql_host = localhsot sql_user = root sql_pass = sql_db = test sql_port = 3306 sql_query_pre = SET NAMES utf8 sql_query = SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \ FROM documents sql_attr_uint = group_id sql_attr_timestamp = date_added sql_ranged_throttle = 0 } source src1throttled : src1 { sql_ranged_throttle = 100 } # Configure the location where the index files generated by the data source are stored index test1 { source = src1 path = D:/sphinx/data/ test1 # Note that the generated file path and name are included here. A file in test1.*** format will be generated in the data directory charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U +430..U+44F, U+430..U+ 44F docinfo = extern dict = keywords mlock = 0 morphology = none min_word_len = 1 ngram_len = 1 ngram_chars = U+3000..U+2FA1F html_strip = 0 } indexer { mem_limit = 128M } # Configure Sphinx server information searchd { listen = 9312 listen = 9306:mysql41 log = D:/sphinx/log/searchd.log query_log = D:/sphinx/log/query.log read_timeout = 5 client_timeout = 300 max_children = 30 persistent_connections_limit = 30 pid_file = D:/sphinx/log/searchd.pid preopen_indexes = 1 unlink_old = 1 mva_updates_pool = 1M max_packet_size = 8M max_filters = 256 max_filter_values = 4096 max_batch_queries = 32 workers = threads
# Start the searchd service under windows, be sure to comment out this
# seamless_rotate = 1
}
4. Create an index
indexer.exe test1
5. Start the service
searchd.exe --pidfile
6. If you need to close the service, close the DOC window directly
7, php installation sphinx extension not to mention. Add dll file under ext
Download address: http://pecl.php.net/package/sphinx
Configured under php.ini. restart apache
extension=php_sphinx.dll
This completes the configuration.
Test case 1. Output the ID that matches the search keyword
<?php $keyword = 'test'; $sphinx = new SphinxClient; $sphinx->setServer("localhost", 9312); $sphinx ->setMatchMode(SPH_MATCH_ANY); // The matching mode ANY is automatic word splitting for keywords, ALL is no word splitting matching (exact match) $sphinx->SetArrayResult ( true ); // The returned result set is an array $result = $sphinx->query($keyword, " test1 " ); // Asterisks are all index sources $count=$result[ ' total ' ]; // Number of results found $time=$result[ ' time ' ]; // Time-consuming $arr=$result[ ' matches ' ]; // Result set $id= '' ; for($i=0;$i<$count;$i++){ $id.=$arr[$i]['id'].','; } $id =substr($id, 0 ,- 1 ); // The id string of the result set echo $id;
Results show:
Test case 2. Match search keywords highlighted
<?php $keyword = 'test'; $sphinx = new SphinxClient(); $sphinx->SetServer('localhost',9312); $sphinx ->setMatchMode(SPH_MATCH_ANY); // Match mode SPH_MATCH_ALL: exact match $result = $sphinx->query($keyword, ' * ' ); // * means search in all indexes $ids = implode( ' , ' ,array_keys($result[ ' matches ' ])); $conn = mysqli_connect('localhost','root',''); mysqli_query($conn,'set names utf8'); mysqli_select_db($conn,'test'); $sql = "select * from documents where id in (".$ids.")"; $rst = mysqli_query($conn,$sql); // Add styles to matching keywords $opts = array( ' before_match ' => ' <font style="font-weight:bold;color:#f00;"> ' , ' after_match ' => ' </font> ' ); echo '<pre>'; while($row = mysqli_fetch_assoc($rst)){ $row2 = $sphinx->buildExcerpts($row, ' test1 ' ,$keyword,$opts); // Main data source index in test1 configuration file print_r($row2); }
Results display