Home > Article > Backend Development > Practical cases and project applications of Sphinx PHP
Sphinx PHP actual cases and project applications
Introduction:
In today's Internet era, with the explosive growth of information volume and the diversification of user needs , search engines have become one of the main ways for us to obtain the information we need. To meet this need, the full-text search engine Sphinx came into being. Using Sphinx in combination with PHP language has also become the choice of many projects.
This article will take specific cases and project applications as examples to introduce the application of Sphinx PHP in actual projects. Some code examples will also be provided for readers to better understand.
1. Case 1: Article Search Function
Suppose we have a news website and need to provide an efficient article search function so that users can quickly find articles of interest. In this case, we will use Sphinx PHP for implementation.
sudo apt-get install sphinxsearch
in the terminal. news.conf
and write the following content: source news { type = mysql sql_host = localhost sql_user = username sql_pass = password sql_db = database sql_port = 3306 sql_query = SELECT article_id, article_title, article_content FROM articles sql_attr_timestamp = article_publish_time } index news_index { source = news path = /var/lib/sphinxsearch/data/news docinfo = extern mlock = 0 mlock_recs = 0 index_exact_words = 1 min_word_len = 3 charset_table = 0..9, A..Z->a..z, _, a..z, U+410..U+42F->U+430..U+44F, U+430..U+44F morphology = stem_en } searchd { listen = 9312 listen = 9306:mysql41 log = /var/log/sphinxsearch/searchd.log query_log = /var/log/sphinxsearch/query.log read_timeout = 5 max_children = 30 pid_file = /var/run/sphinxsearch/searchd.pid seamless_rotate = 1 preopen_indexes = 1 unlink_old = 1 workers = threads binlog_path = /var/lib/sphinxsearch/data }
In the above configuration file, we define a data source news
, specifies the relevant information about connecting to the database and the fields that need to be indexed. Then, we define an index named news_index
, specify the path to the index file and other related configuration.
<?php require_once('sphinxapi.php'); $sphinx = new SphinxClient(); $sphinx->SetServer("localhost", 9312); $keyword = $_GET['keyword']; // 从用户输入中获取关键词 $result = $sphinx->Query($keyword, 'news_index'); // 在索引中搜索关键词 if ($result && $result['total']) { foreach ($result['matches'] as $match) { echo "文章标题:" . $match['attrs']['article_title'] . "<br>"; echo "文章内容:" . $match['attrs']['article_content'] . "<br><br>"; } } else { echo "没有找到相关文章"; } ?>
In the above code, we first create a SphinxClient object and set the relevant information to connect to the server. We then get the keywords from the user input and use Sphinx’s Query method to search the index for relevant articles. Finally, we take the title and content of the article from the search results and display them.
The above is a simple case of using Sphinx PHP to implement article search function. In this way, we can quickly find what we need from a large number of articles.
2. Case 2: Product full-text search
In e-commerce websites, the product full-text search function is essential. In this case, we will use Sphinx PHP to implement a real-time product search function.
sudo apt-get install sphinxsearch
to install. products.conf
and write the following content: source products { type = mysql sql_host = localhost sql_user = username sql_pass = password sql_db = database sql_port = 3306 sql_query = SELECT product_id, product_name, product_description FROM products sql_attr_uint = product_price } index products_index { source = products path = /var/lib/sphinxsearch/data/products docinfo = extern mlock = 0 morphology = stem_en } searchd { listen = 9312 listen = 9306:mysql41 log = /var/log/sphinxsearch/searchd.log query_log = /var/log/sphinxsearch/query.log read_timeout = 5 max_children = 30 pid_file = /var/run/sphinxsearch/searchd.pid seamless_rotate = 1 preopen_indexes = 1 unlink_old = 1 workers = threads binlog_path = /var/lib/sphinxsearch/data }
In the above configuration file, we define a data source products
, specifies the relevant information about connecting to the database and the fields that need to be indexed. Then, we defined an index named products_index
, specifying the path to the index file and other related configurations.
<?php require_once('sphinxapi.php'); $sphinx = new SphinxClient(); $sphinx->SetServer("localhost", 9312); $keyword = $_GET['keyword']; // 从用户输入中获取关键词 $result = $sphinx->Query($keyword, 'products_index'); // 在索引中搜索关键词 if ($result && $result['total']) { foreach ($result['matches'] as $match) { echo "商品名称:" . $match['attrs']['product_name'] . "<br>"; echo "商品描述:" . $match['attrs']['product_description'] . "<br>"; echo "商品价格:" . $match['attrs']['product_price'] . "<br><br>"; } } else { echo "没有找到相关商品"; } ?>
The above code is similar to the article search function, except that the field names are different. We can also search for related products in the index based on the keywords entered by the user and display the search results.
Conclusion:
Through the introduction of the above cases, we can see the application of Sphinx PHP in actual projects. By combining the PHP language and the Sphinx full-text search engine, we can achieve efficient article search and product search functions. Whether it is a news website or an e-commerce website, you can benefit from it.
Of course, Sphinx has many other functions and application scenarios, such as sorting, paging, filtering, etc. I hope the examples in this article can bring some inspiration to readers and inspire more creativity and ideas.
The above is the detailed content of Practical cases and project applications of Sphinx PHP. For more information, please follow other related articles on the PHP Chinese website!