How to use Sphinx with CakePHP?

PHPz
Release: 2023-06-05 22:12:01
Original
1384 people have browsed it

CakePHP is an excellent web application development framework that provides powerful functions and flexible design. Sphinx is a popular full-text search engine that helps us process massive amounts of data efficiently.

In this article, we will introduce how to use Sphinx in CakePHP to better handle our search needs.

  1. Install Sphinx

First, we need to install Sphinx. Sphinx provides a variety of installation methods, including source code installation, binary package installation, etc. Here, we introduce using APT under Ubuntu Linux to install Sphinx.

Open the terminal and enter the following command:

sudo apt-get update
sudo apt-get install sphinxsearch

After the installation is completed, we can use the following command to check Is Sphinx installed correctly:

sudo /usr/bin/searchd

If everything is fine, you should be able to see output similar to the following:

Sphinx 3.1.1-id64- release (commit 4b8c4635)
Copyright (c) 2001-2020, Andrew Aksyonoff
Copyright (c) 2008-2020, Sphinx Technologies Inc (http://sphinxsearch.com)

  1. Configuring Sphinx

Next, we need to configure Sphinx to suit our needs. The Sphinx configuration file is located in /etc/sphinxsearch/sphinx.conf. We can edit this file using the following command:

sudo nano /etc/sphinxsearch/sphinx.conf

Here is a simple configuration example:

source src1
{

type = mysql sql_host = localhost sql_user = username sql_pass = password sql_db = database sql_query = SELECT id, title, content FROM articles
Copy after login

}

index idx1
{

source = src1 path = /var/lib/sphinxsearch/data/idx1 docinfo = extern morphology = stem_en charset_type = utf-8 min_word_len = 3
Copy after login

}

searchd
{

listen = 127.0.0.1:9312 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 max_matches = 1000 seamless_rotate = 1
Copy after login

}

Here we define a data source named src1, use MySQL database for data retrieval, the data table to be retrieved is articles, and the data fields to be retrieved are id, title and content.

Next, an index named idx1 is defined, src1 is used as the data source, and the index file is saved in the /var/lib/sphinxsearch/data/idx1 directory.

Finally, some parameters of the searchd server are defined, such as listening IP and port, log file path, query timeout, etc.

  1. Create CakePHP Model

Next, create our model in CakePHP. We can use the following command to create a model class named Article:

./bin/cake bake model Article

After running, CakePHP will automatically create a model class named Article under src/Model Article's model class.

  1. Writing the CakePHP controller code

Finally, we need to write the CakePHP controller code to handle the search request. Here is a simple example:

namespace AppController;
use CakeCoreExceptionException;
use CakeUtilitySecurity;
use CakeUtilityHash;
use CakeORMTableRegistry;
use CakeHttpClient;
class ArticlesController extends AppController
{

public function search() { $this->loadModel('Articles'); $q = $this->request->getQuery('q'); $indexer = new SphinxClient(); $indexer->setServer('localhost', 9312); $indexer->setMatchMode(SphinxClient::SPH_MATCH_ALL); $result = $indexer->query($q, 'idx1'); $ids = Hash::extract($result['matches'], '{n}.id'); $articles = $this->Articles->find()->where(['id IN' => $ids]); $this->set(compact('articles', 'q')); }
Copy after login

}

Here we first load the Articles model class and get the search key named "q" in the HTTP query parameter Character.

Then create a SphinxClient object, set the Sphinx server address and port, and use SPH_MATCH_ALL mode for search queries.

Next, extract the ids from the results returned by Sphinx and look up these article data in the Articles model.

Finally, display the query results in the view.

Through the above steps, we can use Sphinx to implement full-text search in CakePHP. In actual development, we can further expand and optimize the search function as needed to meet different business needs.

The above is the detailed content of How to use Sphinx with CakePHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn