Current Location: Home> Latest Articles> How to Implement Advanced Multi-Condition Search with PHP and Xunsearch

How to Implement Advanced Multi-Condition Search with PHP and Xunsearch

M66 2025-06-12

How to Implement Advanced Multi-Condition Search with PHP and Xunsearch

As the internet rapidly evolves, search engines have become indispensable tools in our daily lives. In many cases, we need to implement complex search requirements, such as combining multiple search conditions. This article will explain how to use PHP and Xunsearch to implement multi-condition search functionality, helping developers build efficient search systems.

What is Xunsearch?

Xunsearch is a high-performance full-text search engine based on Xapian. It offers a rich API that makes it easy to perform full-text search operations. Xunsearch supports efficient data indexing and retrieval, especially in large-scale applications where performance is crucial.

How to Install Xunsearch

First, we need to install Xunsearch on the server. You can download the latest SDK from Xunsearch's official website and follow the documentation provided for installation and configuration steps.

Creating an Index

Before using Xunsearch for searching, we need to create an index and add data to it. Here's a simple PHP example demonstrating how to create an index and add data to it:


require_once '/path/to/xunsearch/sdk/lib/XS.php';
$xs = new XS('demo');  // Create a link to the "demo" index
$index = $xs->index;  // Get the index object
$index->clean();  // Clear existing index
$doc = new XSDocument();
$doc->setFields(array(
    'id' => 1,
    'title' => 'PHP Learning Guide',
    'content' => 'PHP is a popular server-side scripting language used for web application development.',
    'category' => 'Programming Tutorials'
));
$index->add($doc);  // Add data to the index
$xs->index = $index;  // Save the index
$xs->close();  // Close the index

The above code creates an index called "demo" and adds an article's data to it.

Multi-Condition Search with Xunsearch

Once the index is created, we can perform searches using Xunsearch. Xunsearch provides several powerful search features, such as full-text search, field search, and range search. Here's an example demonstrating how to perform a multi-condition search:


require_once '/path/to/xunsearch/sdk/lib/XS.php';
$xs = new XS('demo');  // Create a link to the "demo" index
$search = $xs->search;  // Get the search object
$query = new XSQuery();  // Create a query object
$query->setFuzzy(true);  // Enable fuzzy search
$query->setLimit(10);  // Limit the number of results to 10
$query->addQueryString('language:PHP OR language:Python');  // Set multi-condition search
$result = $search->search($query);  // Perform the search

// Iterate over the search results and display them
foreach ($result as $document) {
    echo $document->title . '<br>';
    echo $document->content . '<br>';
    echo $document->category . '<br>';
}
$xs->close();  // Close the index

This code demonstrates how to perform a fuzzy search with multiple conditions. The search results are returned as an iterator, and we can loop through the results to display the title, content, and category of each matching document.

Conclusion

This article introduced how to implement multi-condition search functionality using PHP and Xunsearch. By installing Xunsearch and creating an index, developers can leverage Xunsearch's powerful features to perform multi-condition searches. These techniques can be widely applied in e-commerce websites, content management systems, and other scenarios to improve search functionality and user experience. If you have any questions, feel free to leave a comment below and join the discussion.