Home > Backend Development > PHP Problem > PHP search function principle

PHP search function principle

藏色散人
Release: 2023-02-28 12:48:01
Original
2878 people have browsed it

PHP search function principle

PHP search function principle

PHP search function idea (word segmentation matching)

1. The simplest search (using like statement matching)

This search can only be used for single word search

For example: user nickname Search, group name search, etc.

Recommended: "PHP Tutorial"

2. Real search (word segmentation matching)

This method can be used to search data segments, such as retrieving article content titles, etc.

Principle:

Use the full-text search in Mysql match against

Implementation steps

1. Prerequisites for using Mysql full-text search fulltext:

The type of table must be MyISAM (MySQL5. 6 Innodb can also be used later)

The field type to establish a full-text search must be char, varchar, text

2. Create a full-text search advance configuration (configure mysql)

Since the default configuration of Mysql is that the length of indexed words is 4, so if you want to support Chinese characters, you must first change this.

*Unix users need to modify my.cnf. Generally, this file is in /etc /my.cnf, if not found, first search find / -name 'my.cnf'

Add in [mysqld] position:

ft_min_word_len = 2
Copy after login

Other attributes are also

ft_wordlist_charset = gbk

ft_wordlist_file = /home/soft/mysql/share/mysql/wordlist-gbk.txt

ft_stopword_file = /home/soft/mysql/share/mysql/stopwords- gbk.txt

A little explanation:

ft_wordlist_charset represents the character set of the dictionary. Currently well supported ones are (UTF-8, gbk, gb2312, big5)

ft_wordlist_file Yes Vocabulary file, each line includes a word and its word frequency (separated by a number of tabs or spaces, only for disambiguation)

ft_stopword_file means filtering out non-indexed vocabulary, one per line.

ft_min_word_len is the minimum length of words added to the index. The default is 4. In order to support Chinese words, it is changed to 2

3. Create a full-text search (add an index to the table)

Use the FullText keyword to identify fields when creating a table, and use ALTER TABLE (or CREATE INDEX) to create an index for an existing table

CREATE fulltext INDEX index_name ON table_name(colum_name);
Copy after login

4. Use full-text search (SQL statement)

Use the MATCH function in the WHERE clause of SELECT

Overall syntax: MATCH(col1,col2,…) AGAINST (expr[search_modifier]).

The content in MATCH is the column in which the FULLTEXT index has been established and the data is to be found.

expr in AGAINST is the text content to be found,

search_modifier is optional Search type.

The possible values ​​of search_modifier are:

IN NATURAL LANGUAGEMODE、IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION、IN BOOLEAN MODE、WITH QUERY EXPANSION。
Copy after login

Each value of search_modifier represents a type of full-text search, which are natural language full-text search, natural language full-text search with query expansion, Boolean full-text search, query-expanded full-text search (IN NATURAL LANGUAGE MODE is used by default).

SELECT * FROM articles WHERE MATCH (tags) AGAINST ('旅游' IN BOOLEAN MODE);
Copy after login

The above is the detailed content of PHP search function principle. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template