Xunsearch is a high-performance and feature-rich open-source Chinese full-text search engine. It supports Chinese word segmentation, synonym search, multi-field keyword matching, and more, making it suitable for full-text search requirements in various websites and applications.
First, we need to download and install Xunsearch. The installation process is relatively simple, so it will not be elaborated here. After installing Xunsearch, we need to configure it. Go to the installation directory and find the Main.php file, then modify the following parameters:
$server = 'localhost'; // Xunsearch server address
$port = 8383; // Xunsearch service port, default is 8383
$index_name = 'your_index_name'; // Index name, can be customized
Once these configurations are completed, Xunsearch will be ready to use.
Before we can implement the search functionality, we need to create a full-text index. In the Xunsearch installation directory, there is an indexer.php file that we can use to generate the index.
First, run the following command to generate the required configuration file:
php api/cli/phpcmd.php indexer --config-charset=utf8 --ini > api/cli/phpcmd.ini
Next, open the phpcmd.ini file and modify the following parameters:
[xs1]
project.name = your_project_name # Custom search project name
charset = utf8 # Set character encoding
After completing the configuration, run the following command to build the full-text index:
php api/cli/phpcmd.php indexer --all
This command will add all content to the index. If new content is added to the database, you will need to periodically run this command to keep the index up to date.
Once Xunsearch is installed and the index is created, we can begin writing PHP code to implement the sitewide search functionality. The first step is to create a search page where users can input keywords and click the "Search" button to get search results.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sitewide Search</title>
<style>
form { text-align: center; margin-top: 50px; }
input[type=text] { padding: 6px; font-size: 16px; }
input[type=submit] { padding: 6px 20px; font-size: 16px; background-color: #2980b9; color: #fff; border: none; cursor: pointer; }
</style>
</head>
<body>
<form action="search.php" method="GET">
<input type="text" name="keyword" placeholder="Enter a keyword">
<input type="submit" value="Search">
</form>
</body>
</html>
The above is a simple search form where users can input keywords and click "Search" to initiate the search.
Next, we need to create the search.php file to process user search requests and return the search results. Here is an example of the code for search.php:
<?php
require_once 'sdk/php/app/Main.php'; // Include the Xunsearch configuration file
$xs = new XS($index_name); // Create Xunsearch object
$keyword = $_GET['keyword']; // Get the user-inputted keyword
$search = $xs->search; // Get the search object
$search->setQuery($keyword); // Set the search keyword
$search->setCharset('UTF-8'); // Set character encoding
$result = $search->search(); // Execute the search
foreach ($result as $doc) { // Loop through the search results
echo '<h3>' . $doc->title . '</h3>';
echo '<p>' . $doc->content . '</p>';
echo '<hr>';
}
?>
This code includes the Main.php file to initialize the Xunsearch object. It then gets the user inputted keyword and performs the search. Finally, it loops through the search results and displays the title and content of each result on the page.
By using PHP and Xunsearch, we can easily implement an efficient sitewide search function that greatly improves the user experience on the website. This solution is not only fast and simple but also offers rich features provided by Xunsearch, making it suitable for more complex search requirements.
In addition, Xunsearch supports features like boolean search and pinyin search, among others. If you have more advanced search needs, you can further explore Xunsearch's additional features.
We hope this article helps you improve the search performance and user experience of your website.