Home > Article > Backend Development > PHP and Manticore Search Development: Building a Tag-Based Search Engine
PHP and Manticore Search development: Building a tag-based search engine
Search engines are an important part of today's Internet. They can quickly query, match and present the information required by users from large amounts of data. However, traditional search engines often only provide basic full-text retrieval functions, which are not sufficient for precise search and filtering of data. In this article, we will introduce how to use PHP and Manticore Search to build a tag-based search engine to provide a more precise and efficient search experience.
Manticore Search is a full-text search engine developed based on the open source search engine Sphinx. It not only provides fast and powerful full-text search capabilities, but also supports real-time index updates and complex query syntax. PHP is a popular server-side scripting language with a wide range of applications and rich development libraries.
Before we begin, we need to install Manticore Search. You can install it by following these steps:
Download and install Manticore Search:
$ wget https://github.com/manticoresoftware/manticoresearch/releases/download/3.6.0/manticore-3.6.0-200714-58157c26-release.tar.gz $ tar -xvf manticore-3.6.0-200714-58157c26-release.tar.gz $ cd manticore-3.6.0/bin $ ./searchd
Create index:
$ ./searchd $ mysql -P9306 -e "CREATE TABLE documents (id int, title text, content text, tags multi)" $ mysql -P9306 -e "INSERT INTO documents VALUES (1, 'Document 1', 'This is the content of document 1', 'php, search')" $ mysql -P9306 -e "INSERT INTO documents VALUES (2, 'Document 2', 'This is the content of document 2', 'mysql, database')" $ mysql -P9306 -e "INSERT INTO documents VALUES (3, 'Document 3', 'This is the content of document 3', 'php, database')"
Now that we have completed the installation and index creation of Manticore Search, we will next enter the writing process of PHP code.
First, we need to install the Manticore Search extension for PHP. It can be installed by following these steps:
Download and compile the extension:
$ git clone https://github.com/manticoresoftware/php-manticore.git $ cd php-manticore $ phpize $ ./configure $ make $ sudo make install
Enable the extension in the php.ini file:
extension=manticore.so
After completing the above steps, we can start writing PHP code to build a tag-based search engine. The following is a sample code:
<?php $host = 'localhost'; $port = 9306; $index = 'default'; $query = 'php'; // 连接Manticore Search $conn = new ManticoreSearchConnection(); $conn->connect($host, $port); // 创建查询 $search = new ManticoreSearchSearch($conn); $search->index($index); $search->limit(10); $search->setMatchMode(ManticoreSearchSearch::SPH_MATCH_EXTENDED); // 添加标签过滤条件 $search->setFilter('tags', [$query], true); // 发送查询请求 $result = $search->query(''); // 处理查询结果 if (!empty($result['matches'])) { foreach ($result['matches'] as $match) { echo 'ID: ' . $match['id'] . '<br>'; echo 'Title: ' . $match['attrs']['title'] . '<br>'; echo 'Content: ' . $match['attrs']['content'] . '<br>'; echo 'Tags: ' . $match['attrs']['tags'] . '<br><br>'; } } else { echo 'No results found.'; } // 关闭连接 $conn->close();
The above code demonstrates how to use Manticore Search for tag-based search. We first created a connection object through the ManticoreSearchConnection class, and then created a query object through the ManticoreSearchSearch class. We specify the index of the query and the maximum number of returned results by setting the index and limit attributes. Next, we set the filter criteria for the tag so that only documents containing that tag will be returned.
Finally, we send the query request by calling the query method and process the returned results. If the query result is not empty, we can traverse the matches array and output the corresponding document ID, title, content and tags. If the query result is empty, "No results found." is output.
Through the above steps, we successfully built a tag-based search engine using PHP and Manticore Search. Through reasonable index structure and query conditions, we can achieve more accurate and efficient search functions to meet the personalized needs of users. I hope this article will be helpful to readers who use PHP and Manticore Search for search engine development.
The above is the detailed content of PHP and Manticore Search Development: Building a Tag-Based Search Engine. For more information, please follow other related articles on the PHP Chinese website!