Introduction to Sphinx:
Sphinx is a standalone full- text indexing engine intended to provide high-speed , low-space for other applications
Full - text search function with high occupancy and high relevance of search results . Sphinx can be very easy to use with
SQL database and scripting language integration. Built-in MySQL and PostgreSQL database data sources
support. The search API supports PHP , Python, Perl, Rudy and Java.
The use background of Sphinx: When optimizing in mysql, when querying these data for varchar, char, and text, if we use like '% word', the index cannot be used. If the amount of data on the website is relatively large, it will drag crash the speed of the website.
The principle of Sphinx:
Index the data source first. Using word segmentation technology, an index table is formed. When querying a word, first go to the index established by sphinx to find it, and then go to the database to find it by id.
The process of installing sphinx on Windows:
After downloading and decompressing, copy the mysql template configuration file csft_mysql.conf under the etc directory to the upper-level directory, and modify it to sphinx.conf. Then configure the data source for it, and there are detailed comments in the template.
Then go to the bin directory to generate an index file based on the configured file:
Order:
Indexer.exe -c sphinx.conf -all // --all: Create index files for all indexes in the configuration file
Execute a program under sphinx indexer.exe –c configuration file –all | the name of the index
Indexer.exe -c sphinx.conf index name (configured in sphinx.conf)
Install and start sphinx:
grammar:
searchd.exe -c config file --install
Parameters corresponding to this command:
searchd open the server
searchd -c config file index name
The server listens on port 9312 by default. Common commands:
-c : specify the configuration file path
--stop : stop the current service
--status : View current status
--install : install as a windows service
--delete: delete windows service
--port port: listening port
--index indexName : only query a certain index, query all indexes by default
After the service is started, go to services.msc to start searchd, and check the port netstat -an to see if 9312 is enabled for verification.
Code:
<?php
require('sphinxapi.php'); // This file is in the sphinx api directory
$sc = new SphinxClient(); // generate client
$sc->setServer('localhost',9312); // set server
$res = $sc->query('Martial Arts','mysql'); // The first parameter is the query content, and the second parameter is the name of the index (configured in sphinx.conf)
$ids = implode(",",array_keys($res['matches']));
$conn = mysqli_connect("localhost","root","root","test");
mysqli_query($conn,"set names utf8");
$sql = "select * from test where id in($ids)";
$res = mysqli_query($conn,$sql);
$list = array();
while($row = mysqli_fetch_assoc($res)){
$list[] = $row;
}
mysqli_free_result($res);
mysqli_close($conn);
foreach($list as $v){
echo $v['name'].'<br/>'.$v['desc'].'<hr>';
}