Implement Real-Time Search and Automatic Index Updating with PHP and Xunsearch
This article demonstrates how to set up Xunsearch, a C++-based full-text search engine, and integrate it with PHP to achieve real-time search capabilities and automatically update the index, covering environment preparation, installation, sample code for searching, and index maintenance.
When developing a website or application, search functionality is essential, and traditional database searches often lack the efficiency required for real-time results. Xunsearch, a C++‑based full‑text search engine supporting Chinese segmentation, can be used with PHP to provide fast, real‑time search and automatic index updates.
1. Environment Preparation
Before starting, ensure you have a PHP environment (version 5.4 or higher) and a Xunsearch server downloaded from the official site.
2. Install Xunsearch
Unzip the downloaded Xunsearch server package and run ./xunsearchd inside the server directory to start the Xunsearch daemon.
3. Index and Search Example
Create a PHP file named search.php and include the Xunsearch library:
require_once '/path/to/sdk/php/lib/XS.php';Instantiate the Xunsearch object and specify the index configuration:
$xs = new XS('/path/to/xunsearch/app.ini');Set up the search object, enable fuzzy search, limit results, configure segmentation, and assign field weights:
$search = $xs->search;
$search->setFuzzy();
$search->setLimit(10);
$search->setScwsMulti(3);
$search->addWeight('title', 10);
$search->addWeight('content', 5);Perform the search using a keyword from the query string and iterate over the results:
$keyword = $_GET['q'];
$result = $search->search($keyword);
foreach ($result as $item) {
echo $item->title . '
';
echo $item->content . '
';
}4. Automatic Index Updating
Create a PHP file named update.php , include the Xunsearch library, and instantiate the Xunsearch object:
require_once '/path/to/sdk/php/lib/XS.php';
$xs = new XS('/path/to/xunsearch/app.ini');Obtain the index object and fetch data that needs to be indexed (example uses a MySQL database):
$pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('SELECT * FROM articles');
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);Loop through the data, create an XSDocument for each record, and update the index:
foreach ($data as $item) {
$doc = new XSDocument();
$doc->setFields($item);
$index->update($doc);
}
$index->flushIndex();Conclusion
The guide shows how to leverage PHP and Xunsearch to add efficient real‑time search to a website or application and how to keep the search index up‑to‑date automatically, ensuring that users always receive the latest results.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.