PHP and Manticore Search Development: Tips to Improve Search Results Sorting
Search engines are an integral part of modern Internet applications. The ranking of search results directly affects user experience and search accuracy. In this article, we’ll cover tips on how to improve the ranking of search results during development using PHP and Manticore Search.
First, we need to install Manticore Search. It can be installed on Ubuntu through the following command:
sudo apt-get install manticoresearch
After the installation is completed, start the Manticore Search service:
sudo service manticoresearch start
First, we Data needs to be imported into Manticore Search. Here we will use a simple example, assuming we have an e-commerce website that contains product related information such as product name, description, price, etc.
For the convenience of demonstration, we create an index named "products" and add some sample data:
<?php // 连接Manticore Search服务 $dsn = 'mysql:host=127.0.0.1;port=9306'; $username = ''; $password = ''; $pdo = new PDO($dsn, $username, $password); // 创建索引 $sql = "CREATE TABLE products ( id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, description TEXT, price FLOAT(10,2) NOT NULL ) ENGINE = Manticore;"; $pdo->exec($sql); // 添加示例数据 $productData = [ ['name' => 'iPhone 12', 'description' => 'The latest iPhone model', 'price' => 999.99], ['name' => 'Samsung Galaxy S21', 'description' => 'Powerful Android phone', 'price' => 799.99], ['name' => 'Google Pixel 5', 'description' => 'Flagship Google phone', 'price' => 699.99], ]; foreach ($productData as $product) { $sql = "INSERT INTO products (name, description, price) VALUES (?, ?, ?)"; $stmt = $pdo->prepare($sql); $stmt->execute([$product['name'], $product['description'], $product['price']]); } echo 'Data imported successfully!';
Connect Next, let's implement a basic search function. Users can search product names and descriptions by keywords and return related product lists. We can use the full-text search function provided by Manticore Search to achieve this.
<?php // 连接Manticore Search服务 $dsn = 'mysql:host=127.0.0.1;port=9306'; $username = ''; $password = ''; $pdo = new PDO($dsn, $username, $password); // 执行搜索 $keyword = 'iPhone'; $sql = "SELECT * FROM products WHERE MATCH (:keyword)"; $stmt = $pdo->prepare($sql); $stmt->bindValue(':keyword', $keyword); $stmt->execute(); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); // 显示搜索结果 foreach ($results as $result) { echo 'Product Name: ' . $result['name'] . PHP_EOL; echo 'Description: ' . $result['description'] . PHP_EOL; echo 'Price: ' . $result['price'] . PHP_EOL; echo '------------------------' . PHP_EOL; }
In order to improve the sorting of search results, we can use the sorting function of Manticore Search. We can sort the search results according to different fields, such as price, sales volume, etc.
Here is an example of sorting search results based on product price:
<?php // 连接Manticore Search服务 $dsn = 'mysql:host=127.0.0.1;port=9306'; $username = ''; $password = ''; $pdo = new PDO($dsn, $username, $password); // 执行搜索 $keyword = 'iPhone'; $sql = "SELECT * FROM products WHERE MATCH (:keyword) ORDER BY price DESC"; $stmt = $pdo->prepare($sql); $stmt->bindValue(':keyword', $keyword); $stmt->execute(); $results = $stmt->fetchAll(PDO::FETCH_ASSOC); // 显示搜索结果 foreach ($results as $result) { echo 'Product Name: ' . $result['name'] . PHP_EOL; echo 'Description: ' . $result['description'] . PHP_EOL; echo 'Price: ' . $result['price'] . PHP_EOL; echo '------------------------' . PHP_EOL; }
In this example, we are sorting price in descending order by ORDER BY price DESC
.
In addition to price, we can also sort based on other fields, such as sales volume, ratings, etc. Just modify the ORDER BY
clause accordingly.
Summary
This article introduces how to use PHP and Manticore Search to develop search functions, and provides some tips to improve the ranking of search results. Through these techniques, we can perform targeted sorting of search results according to different needs and scenarios, improving user experience and search accuracy.
I hope this article can help you solve the problems you encounter when optimizing the sorting of search results in PHP development. Good luck with your development efforts!
The above is the detailed content of PHP and Manticore Search Development: Tips to Improve Search Results Ranking. For more information, please follow other related articles on the PHP Chinese website!