Home  >  Article  >  Backend Development  >  Use PHP and coreseek to implement precise recipe search function

Use PHP and coreseek to implement precise recipe search function

王林
王林Original
2023-08-05 13:16:431444browse

Use PHP and coreseek to achieve accurate recipe search function

Overview:
In today's fast-paced life, more and more people are beginning to pay attention to their dietary health. Finding the right recipe becomes a need. This article will introduce how to use PHP and coreseek search engine to implement accurate recipe search function to help users easily find recipes that meet their needs.

Preparation work:
Before we start, we need to prepare some tools:

  1. PHP environment: Set up a PHP environment locally or on the server to ensure that PHP scripts can be run.
  2. MySQL database: used to store recipe data.
  3. Coreseek Search Engine: A full-text search engine based on Sphinx. The latest version can be downloaded from its official website.

Core idea:

  1. Prepare recipe data and import it into the MySQL database.
  2. Configure and start the Coreseek search engine.
  3. Use PHP to write the search function code to realize user input and display of search results.

Step 1: Prepare recipe data and import it into the MySQL database

First, we need to create a database to store recipe data. You can use tools such as phpMyAdmin or directly execute the corresponding SQL statement on the command line to create it.

The SQL statements to create databases and tables are as follows:

CREATE DATABASE IF NOT EXISTS recipe;
USE recipe;
CREATE TABLE IF NOT EXISTS recipes (

id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
ingredients TEXT NOT NULL,
directions TEXT NOT NULL

);

Next, we can read the local recipe data by writing a PHP script and import it into the database. Assume our recipe data is saved in a text file called "recipes.txt" with the following format:

Title 1
Ingredients 1, Ingredients 2, Ingredients 3
Step 1 , Step 2, Step 3

Title 2
Ingredients 4, Ingredients 5, Ingredients 6
Step 4, Step 5, Step 6

The PHP script code is as follows:

a86bba0946e110e01ee9d05cd21a4830

In the above code, we open the file named "recipes.txt" and read the contents line by line. Differentiate the titles, ingredients, and steps of different recipes by using blank lines. After each complete recipe is read, we insert it into the database.

Step 2: Configure and start the Coreseek search engine

First, unzip the downloaded coreseek compressed package to the specified directory. Enter the Coreseek directory, edit the configuration file "sphinx.conf", and configure the following content:

source recipe
{

type = mysql
sql_host = localhost
sql_user = root
sql_pass = your_password
sql_db = recipe
sql_port = 3306
sql_query = SELECT id, title, ingredients, directions FROM recipes

}

index recipe
{

source = recipe
path = /path/to/index/recipe
docinfo = extern
min_word_len = 1
charset_type = utf-8

}

searchd
{

listen = 9312
log = /path/to/searchd.log
query_log = /path/to/query.log
max_matches = 1000

}

In the above configuration, we defined a data source named "recipe" , a MySQL database was used to obtain recipe data. Then, an index named "recipe" was created and the related paths and configurations were set.

After saving and exiting the configuration file, use the following command to start the Coreseek search engine:

/path/to/coreseek/bin/searchd --config /path/to/coreseek/etc/ sphinx.conf

Step 3: Use PHP to write the code for the search function

Now, we can use PHP to write the code for the search function. Suppose we have a search form on our web page where users can enter keywords to search.

The HTML code is as follows:

db98d42453e49bb1a842765f91fd0202

<input type="text" name="keyword" placeholder="请输入关键词">
<input type="submit" value="搜索">

f5a47148e367a6035fd7a2faa965022e

The PHP script code is as follows (search.php):

14613c24b02be9952b7c34a368be2491

In the above code, we first get the keyword entered by the user and store it in the variable $keyword. Then, we use the MATCH keyword to query the database and obtain recipe data that matches the keyword. The specific database query and result parsing code is omitted here, you can implement it according to your own needs.

Finally, in the search results page, we can display the search results according to specific needs, such as using loop statements to traverse the queried data and display it to the user in the form of a list or card.

Summary:
This article introduces how to use PHP and coreseek search engine to achieve accurate recipe search function. By preparing recipe data, configuring and starting the Coreseek search engine, and writing relevant PHP code, we can easily implement a powerful recipe search function to help users quickly find recipes that meet their needs. At the same time, you can also expand and optimize the code according to your own needs to achieve a better user experience.

The above is the detailed content of Use PHP and coreseek to implement precise recipe search function. 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