Backend Development 5 min read

Build Real-Time Search with PHP and Xunsearch: Step-by-Step Guide

This tutorial explains how to set up Xunsearch, a C++‑based full‑text engine, with PHP to create fast, real‑time search functionality and automatically keep the index updated, covering environment preparation, installation, coding examples, and best practices.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Build Real-Time Search with PHP and Xunsearch: Step-by-Step Guide

When developing a website or application, search functionality is essential. Traditional database search is inefficient for real‑time needs. Xunsearch is a C++‑based full‑text search engine supporting Chinese segmentation and fast queries. This article shows how to implement real‑time search and automatic index updates using PHP and Xunsearch.

1. Environment Preparation

Before starting, prepare the following environment:

PHP (version 5.4 or higher)

Xunsearch server (download from http://www.xunsearch.com/download)

2. Install Xunsearch

1. Extract the downloaded Xunsearch server package.

2. Enter the server directory and run

./xunsearchd

to start the Xunsearch server.

3. Set Up Index and Search Example

1. Create a PHP file named

search.php

.

2. Include the Xunsearch library:

<code>require_once '/path/to/sdk/php/lib/XS.php';</code>

3. Create a Xunsearch object and specify the index path:

<code>$xs = new XS('/path/to/xunsearch/app.ini');</code>

4. Create a search object and configure fields:

<code>$search = $xs->search;
$search->setFuzzy();
$search->setLimit(10);
$search->setScwsMulti(3);
$search->addWeight('title', 10);
$search->addWeight('content', 5);
</code>

5. Perform the search:

<code>$keyword = $_GET['q'];
$result = $search->search($keyword);
</code>

6. Loop through and output results:

<code>foreach ($result as $item) {
    echo $item->title . '<br>';
    echo $item->content . '<br><br>';
}
</code>

4. Automatic Index Updating

1. Create a PHP file named

update.php

.

2. Include the Xunsearch library:

<code>require_once '/path/to/sdk/php/lib/XS.php';</code>

3. Instantiate Xunsearch and specify the index path:

<code>$xs = new XS('/path/to/xunsearch/app.ini');</code>

4. Get the index object:

<code>$index = $xs->index;</code>

5. Retrieve data to be indexed (example uses MySQL):

<code>$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);
</code>

6. Loop to update the index:

<code>foreach ($data as $item) {
    $doc = new XSDocument();
    $doc->setFields($item);
    $index->update($doc);
}
</code>

7. Flush the index to save changes:

<code>$index->flushIndex();
</code>

Conclusion

This guide demonstrated how to use PHP and Xunsearch to add efficient real‑time search to a site or application and keep the index up‑to‑date automatically.

backend developmentreal-time searchxunsearchfull-text indexing
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

login 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.