Home >Backend Development >PHP Tutorial >Developing multilingual search capabilities using PHP and Manticore Search
Using PHP and Manticore Search to develop multilingual search functions
With the rapid development of globalization, more and more websites and applications need to support multilingual search functions to meet the needs of global users. This article will introduce how to use PHP and Manticore Search (a high-performance search engine based on the open source search engine Sphinx) to implement multi-language search capabilities, with code examples.
First, we need to install and configure Manticore Search on the server. You can refer to the official documentation of Manticore Search (https://manual.manticoresearch.com/Installation) to complete the installation.
Before searching, we need to import the data to be searched into Manticore Search. Suppose we have a table named "products" that contains product information, which contains multi-language fields, such as "product_name_en" and "product_name_cn".
First, we need to set up the index of Manticore Search. You can use the command line tool provided by Manticore Search to complete this step.
$ indexer -c /path/to/manticore.conf --all
Then, we can use the MySQL interface provided by Manticore Search to import data. First, we need to create a MySQL connection.
$mysqli = new mysqli('host', 'user', 'password', 'database'); if ($mysqli->connect_errno) { die("Failed to connect to MySQL: " . $mysqli->connect_error); }
Next, we can use SQL statements to insert data into Manticore Search.
$sql = 'SELECT id, product_name_en, product_name_cn FROM products'; $result = $mysqli->query($sql); $all_rows = ''; while ($row = $result->fetch_assoc()) { $all_rows .= '(' . $row['id'] . ', "' . $row['product_name_en'] . '", "' . $row['product_name_cn'] . '"),'; } $all_rows = rtrim($all_rows, ','); $sql = 'REPLACE INTO products (id, product_name_en, product_name_cn) VALUES ' . $all_rows; $mysqli->query($sql); if ($mysqli->error) { die("Failed to insert data into Manticore Search: " . $mysqli->error); }
Through the above code, we successfully imported the data into Manticore Search.
Next, we need to write PHP code that can support multi-language search.
$query = 'apple'; // 根据当前语言选择搜索字段 $language = 'en'; if ($language === 'cn') { $search_field = 'product_name_cn'; } else { $search_field = 'product_name_en'; } // 连接Manticore Search的搜索接口 $cl = new ManticoreSearchClient(); $cl->connect(); // 执行搜索 $result = $cl->search('products', $query, array('index' => 'products', 'field_weights' => array($search_field => 100))); // 解析搜索结果 $matches = array(); if ($result['total'] > 0) { foreach ($result['matches'] as $match) { $matches[] = $match['attrs']['id']; } } // 查询原始数据 $sql = 'SELECT * FROM products WHERE id IN (' . implode(',', $matches) . ')'; $result = $mysqli->query($sql); while ($row = $result->fetch_assoc()) { echo $row['product_name_en'] . '<br>'; }
Through the above code, we can select the corresponding search field according to the current language and perform a search based on the search keywords. Finally, we can obtain relevant search results through the original data table.
Summary
This article introduces how to use PHP and Manticore Search to develop multilingual search functions. With the above sample code, we can easily implement multi-language search functionality to meet the needs of global users. At the same time, Manticore Search also provides a wealth of configuration options and operation interfaces to further optimize search performance and functions.
I hope this article will be helpful to friends who develop multi-language search functions. If you have any questions, please feel free to leave a message in the comment area.
The above is the detailed content of Developing multilingual search capabilities using PHP and Manticore Search. For more information, please follow other related articles on the PHP Chinese website!