Home >Backend Development >PHP Tutorial >PHP and coreseek are combined to develop an efficient movie search engine

PHP and coreseek are combined to develop an efficient movie search engine

王林
王林Original
2023-08-05 18:10:56871browse

PHP and coreseek are combined to develop an efficient movie search engine

Introduction: Movie search engines play an important role in today's Internet era, providing users with fast and accurate search results. In this article, we will introduce how to use PHP and coreseek to develop an efficient movie search engine.

1. What is coreseek?
Coreseek is an open source full-text search engine tool, customized and optimized based on the Sphinx search engine. Sphinx search engine is a fast and efficient full-text search engine that is widely used in various fields.

2. Why choose PHP and coreseek?
PHP is an easy-to-learn, powerful programming language that is widely used in the field of web development. As an efficient full-text search engine tool, coreseek can be used with PHP to quickly build an efficient search engine.

3. Steps to build a movie search engine

  1. Install coreseek
    First, we need to install coreseek. Use the command line to install it in a Linux environment. For specific steps, please refer to the coreseek official documentation. After the installation is complete, the coreseek configuration file is located in the /usr/local/coreseek/etc/ directory.
  2. Create database and table
    Next, we need to create a database and create a table to store movie information. Can be created using MySQL or other database management systems. The following is a sample SQL code to create a movie table:
CREATE DATABASE IF NOT EXISTS `movie_search`;
USE `movie_search`;

CREATE TABLE IF NOT EXISTS `movies` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `title` VARCHAR(255) NOT NULL,
  `director` VARCHAR(255) NOT NULL,
  `release_date` DATE NOT NULL,
  `rating` DECIMAL(3,1) NOT NULL,
  `description` TEXT NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  1. Import movie data
    To import movie data into the movies table, you can use the INSERT statement to insert data one by one, or Import large amounts of data through batch insertion. The following is a sample SQL code for inserting movie data:
INSERT INTO `movies` (`title`, `director`, `release_date`, `rating`, `description`) 
VALUES 
('The Shawshank Redemption', 'Frank Darabont', '1994-10-14', 9.3, 'The Shawshank Redemption is a 1994 American drama film.'),
('The Godfather', 'Francis Ford Coppola', '1972-03-24', 9.2, 'The Godfather is a 1972 American crime film.'),
('Pulp Fiction', 'Quentin Tarantino', '1994-05-21', 8.9, 'Pulp Fiction is a 1994 American crime film.');
  1. Configuring coreseek
    Open the coreseek configuration filesphinx.conf and make the necessary configurations to Adapted to our movie search engine needs. The sample code for modifying the configuration items is as follows:
source movies {
    type = mysql
    sql_host = localhost
    sql_user = your_mysql_username
    sql_pass = your_mysql_password
    sql_db = movie_search
    sql_sock = /var/run/mysqld/mysqld.sock
    sql_query = SELECT id, title, director, release_date, rating, description FROM movies
}

index movies {
    source = movies
    path = /usr/local/coreseek/data/movie_search
    docinfo = extern
    mlock = 1
    mlock_retry = 5
    morphology = stem_en, soundex_en
    min_word_len = 3
}

searchd {
    listen = 9312
    listen = 9306:mysql41
    log = /var/log/coreseek/searchd.log
    query_log = /var/log/coreseek/query.log
    read_timeout = 5
    max_children = 30
    pid_file = /usr/local/coreseek/var/searchd.pid
    seamless_rotate = 1
    preopen_indexes = 0
    unlink_old = 1
    workers = threads
    binlog_path =
}
  1. PHP code example
    Now we start writing PHP code to search for movies through coreseek. First, create a file called search.php and copy and paste the following code into it:
<?php

require_once('sphinxapi.php');

$sphinx = new SphinxClient();
$sphinx->SetServer('localhost', 9312);
$sphinx->SetMatchMode(SPH_MATCH_ALL);

$query = isset($_GET['q']) ? $_GET['q'] : '';

$result = $sphinx->Query($query, 'movies');

if ($result === false) {
    echo "Error: " . $sphinx->GetLastError();
} else {
    if ($sphinx->GetTotalFound() > 0) {
        echo "Search results for: " . $query . "<br>";
        foreach ($result['matches'] as $match) {
            echo "<div>";
            echo "Title: " . $match['attrs']['title'] . "<br>";
            echo "Director: " . $match['attrs']['director'] . "<br>";
            echo "Release Date: " . $match['attrs']['release_date'] . "<br>";
            echo "Rating: " . $match['attrs']['rating'] . "<br>";
            echo "Description: " . $match['attrs']['description'] . "<br>";
            echo "</div>";
        }
    } else {
        echo "No results found for: " . $query;
    }
}

?>
  1. Test the search engine
    Access in your browser search.php, pass the search keyword through the URL parameter q to search. For example: http://localhost/search.php?q=The Shawshank Redemption.

Through the above steps, we successfully built a movie search engine based on PHP and coreseek. You can expand and optimize this search engine according to your own needs, such as adding more search conditions, increasing sorting functions, etc.

Conclusion:
In this article we introduced how to use PHP and coreseek to develop an efficient movie search engine. By using coreseek as a full-text search engine tool, combined with the PHP programming language, we can quickly build a powerful search engine. I hope this article can provide you with some help in developing a movie search engine.

The above is the detailed content of PHP and coreseek are combined to develop an efficient movie search engine. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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