Implement Real-Time Search and Automatic Index Updating with PHP and Xunsearch

This article provides a step‑by‑step guide on setting up Xunsearch with PHP to achieve real‑time full‑text search and automatic index updates, covering environment preparation, server installation, search script implementation, and index‑updating routines.

php Courses
php Courses
php Courses
Implement Real-Time Search and Automatic Index Updating with PHP and Xunsearch

Search functionality is crucial for websites and applications, but traditional database search cannot meet real‑time efficiency requirements.

1. Environment Preparation

Before starting, ensure a PHP environment (version 5.4 or higher) and the Xunsearch server are available.

2. Install Xunsearch

Unzip the downloaded Xunsearch server package and run ./xunsearchd to start the server.

3. Set Up Index and Search Example

Create a PHP file named search.php, include the Xunsearch library, instantiate the XS object, configure the search object, retrieve the query keyword, perform the search, and loop through the results to output titles and contents.

require_once '/path/to/sdk/php/lib/XS.php';
$xs = new XS('/path/to/xunsearch/app.ini');
$search = $xs->search;
$search->setFuzzy();
$search->setLimit(10);
$search->setScwsMulti(3);
$search->addWeight('title', 10);
$search->addWeight('content', 5);
$keyword = $_GET['q'];
$result = $search->search($keyword);
foreach ($result as $item) {
    echo $item->title . '<br>';
    echo $item->content . '<br><br>';
}

4. Automatic Index Updating

Create a PHP file named update.php, include the Xunsearch library, instantiate the XS object, connect to a MySQL database, fetch data, and update the Xunsearch index for each record, finally flushing the index.

require_once '/path/to/sdk/php/lib/XS.php';
$xs = new XS('/path/to/xunsearch/app.ini');
$index = $xs->index;
$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);
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 capabilities to a website or application and keep the search index automatically up‑to‑date.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

indexingBackend DevelopmentPHPFull‑Text Searchreal-time searchXunSearch
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.