Home  >  Article  >  Backend Development  >  How to implement site search in php

How to implement site search in php

藏色散人
藏色散人Original
2020-09-30 09:21:334123browse

php method to implement on-site search: 1. Use SQL LIKE to implement on-site search; 2. Use Google search engine API and Google search function to establish on-site search; 3. Use "PHP MYSQL SCWS" to do on-site search search engine.

How to implement site search in php

Recommended: "PHP Video Tutorial"

PHP MYSQL SCWS Make your own on-site search engine

There is such an on-site search engine. It supports Chinese word segmentation and full-text search. It does not require any extensions or any settings on the server. It only needs to support PHP and MYSQL. It is easy to operate. High efficiency and good effect. This is the PHP MYSQL SCWS on-site search engine.

In fact, no matter how big or small, from Sina to Aika Automotive Network, from a website with over 100 million PV per day to my own personal blog, An on-site search engine is needed. The role of an on-site search engine on a website is self-evident. The most direct role is to allow users to find what they want in your website in the most direct way and as quickly as possible. And Instead of going to Baidu and Google, search for other people’s websites.

(1) The most basic on-site search uses SQL’s LIKE

for example

SELECT * FROM bbs_threads WHERE subject LIKE '%搜索引擎%' LIMIT 10

Advantages: too easy Yes, everyone knows it

Disadvantages: 1. Each time the LIKE statement is executed, a table traversal is required, and string comparison is used, which is too inefficient. 2. Word segmentation is not possible, and only the whole sentence can be searched. If The search terms are long and almost no results can be found. If the word segmentation function is added, it is equivalent to using several LIKEs in one sentence, the same as 1.

(2) Google Custom Search

Use Google search engine API and Google's powerful search function to build your own on-site search. Demo: http://www.lusongsong.com/search.html?cx=014724041144905348996:pf5fnahnzuw&cof=FORID:11&ie=UTF-8&q=crisis&sa =Search&siteurl=lusongsong.com/

Advantages: Worry-free, there is nothing more worry-free than this. You don’t have to worry about anything. Google owns its own search algorithm, so how can it be ours? It can be compared.

Disadvantages: 1. You need to use IFRAME, or directly open the Google page, or use a more complex API to achieve it. 2. You can only search according to text, and you can only search the whole site. If I want to search for specific types of articles under a certain channel, Google cannot implement it. In a word, it cannot be customized. 3. If your website is small, Google will not include you, and you will not be able to search anything. What do you do when you come out?

(3) Lucene is the best open source search engine in the world

Uh...nothing to introduce, just look at the pros and cons

Advantages: Completely open source code, completely customizable, completely...

Disadvantages: Written in java, I don’t know how. Do you know java? Try another one

(4) PHP MYSQL SCWS Make your own on-site search engine

It has been waiting for you for a long time!

MySQL supports full-text indexing and search. The full-text index in MySQL is a FULLTEXT type index. FULLTEXT indexes are used on MyISAM tables and can be created on CHAR, VARCHAR, or TEXT columns at the time of CREATE TABLE or after using ALTER TABLE or CREATE INDEX.

Function MATCH() Performs a natural language search for a string against a text set (a column set containing one or more columns in a FULLTEXT index). The search string is given as an argument to AGAINST(). The search is performed ignoring the case of letters. For each record row in the table, MATCH() returns a correlation value. That is, the measure of similarity between the search string and the record row's text in the column specified in the MATCH() list.

When MATCH() is used in a WHERE clause, the returned rows are automatically sorted from high to low relevance. Correlation values ​​are non-negative floating point numbers. Zero correlation means no similarity.

In an English environment, it is completely possible to establish a full-text search environment using PHP and MYSQL. The steps are:

1. Create a content table, fill in the data, and create a full-text search environment on the fields that require full-text search. FULLTEXT index

2. Use the MATCH function to execute the search conditions

3. Process the returned data and display the results

Simple. . .

The problem is that Chinese is not a self-segmented language. In MYSQL, a large section of Chinese is just one word, and full-text search will be invalid. The way to solve this problem is to use Other tools are used for word segmentation to separate large sections of Chinese into words one by one, similar to English words.

The simplest tool is SCWS. Official site: http://www.ftphp.com /scws/

SCWS is the abbreviation of Simple Chinese Words Segmentation, which is a simple Chinese word segmentation system.

This is a mechanical Chinese word segmentation engine based on word frequency dictionary. It can basically correctly segment a whole paragraph of Chinese characters into words. Words are the basic morpheme units of Chinese. When writing, unlike English, words are separated by spaces. Therefore, how to segment words accurately and quickly has always been a difficult problem in Chinese word segmentation.

SCWS 在概念上并无创新成分,采用的是自行采集的词频词典,并辅以一定程度上的专有名称、人名、地名、数字年代等规则集,经小范围测试大概准确率在 90% ~ 95% 之间,已能基本满足一些中小型搜索引擎、关键字提取等场合运用。 SCWS 采用纯 C 代码开发,以 Unix-Like OS 为主要平台环境,提供共享函数库,方便植入各种现有软件系统。此外它支持 GBK,UTF-8,BIG5 等汉字编码,切词效率高。

SCWS提供了纯PHP代码编写的中文分词类,使它不需要做任何额外的扩展就能在机会所有的服务器上使用.

支持中文的基于PHP+MYSQL的全文检索的步骤为:

1. 建立内容表(,将数据分词),灌入数据,在需要全文检索的字段上建立FULLTEXT索引

2. (将要搜索的内容先分词再) 利用MATCH函数执行搜索条件

3. 处理返回的数据,显示结果

仍然是SO EASY!

一个演示的例子: http://www.bnet.com.cn/files/search.php?word=%CA%D5%B9%BA&page=3

执行搜索的语句是:

SELECT SQL_CALC_FOUND_ROWS *, MATCH (titlewords, keywords, author, contentwords) AGAINST ('$words') AS matchscore
FROM search
WHERE MATCH (titlewords, keywords, author, contentwords) AGAINST ('$words') > 0.5

The above is the detailed content of How to implement site search in php. 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