Table of Contents
回复内容:
Home Backend Development PHP Tutorial MYSQL中的中文模糊搜索除了使用全文索引外还有什么办法呢?

MYSQL中的中文模糊搜索除了使用全文索引外还有什么办法呢?

Jun 06, 2016 pm 08:14 PM
mysql php fuzzy search index

环 境:winxp
语 言:php5.49
数据库:musql5.6
LEVER:一个初学PHP和MYSQL的小粉
数据量:30万左右,不会超过50万。

就是这样的一个网站,就是这样的一个水平的我,却在这几天被一个难题难死了。

因为我之前的这个站是用asp+mssql搭建的,一直在托管的服务器上跑的还行,某一天想把数据库放到阿里云上,查了一下报价,发现mysql的价格比mssql的价格便宜近10倍多,这才认识到为什么开源软件这么受欢迎了,好吧,我也改一下数据库,顺便学习一下早就想学的PHP,说干就干,装PHP装mysql,中间碰到的这个苦难就不说了,改写起来还算顺利,有一些弱智的问题也得到了网友的热情回答,还算顺利,但是直到昨天,我在改写原来MSsql里面的全文索引语句为PHP的全文索引语句时,无意中看到,MSSQL居然不支持中文的全文索引,于是我感到无比的挫败感,这该怎么办呢?这么好的数据库却有这么一个难题,于是想对于一个如此成熟的数据库,同时全文索引又是一个常用功能,那么网上的解决方案一定是现成的,好吧,我开始了百度,这下才发现,很难找到像样的资料可以轻松解决此问题,很多解决方案是那么那么的晦涩难懂。对于我这样的水平的人,看完都不容易更不要说执行了,尝试了其中一种Sphinx+CoreSeek4.1的方案,连安装调试都过不去,可以找到的资料也是很少很少,唉。。。愁人的

所以,我就想问问大家,为什么你们都不需要解决中文模糊搜索过程中全文索引的建立和使用问题吗?这不是一个很普遍的应用吗?

再或者,请你们帮我分析一下,对于这样一个30万左右的数据库,一共10个字段,想对其中5个字段实现模糊搜索,是通过spnix/coreseek建立中文的全文索引的搜索速度快,还是直接使用like 汉字关键词%的速度快呢?或者慢的话又能慢多少呢?

我现在没有这方面的经验,所以一直在纠结,要是二者没有显著性差别,我觉得为这个搭建一个spnix/coreseek环境是不是有些太那个了呢?而且还那么那么那么那么难搭建的,真心懊恼,不知道咋办了,请帮我一下好吗?谢谢大家。

回复内容:

环 境:winxp
语 言:php5.49
数据库:musql5.6
LEVER:一个初学PHP和MYSQL的小粉
数据量:30万左右,不会超过50万。

就是这样的一个网站,就是这样的一个水平的我,却在这几天被一个难题难死了。

因为我之前的这个站是用asp+mssql搭建的,一直在托管的服务器上跑的还行,某一天想把数据库放到阿里云上,查了一下报价,发现mysql的价格比mssql的价格便宜近10倍多,这才认识到为什么开源软件这么受欢迎了,好吧,我也改一下数据库,顺便学习一下早就想学的PHP,说干就干,装PHP装mysql,中间碰到的这个苦难就不说了,改写起来还算顺利,有一些弱智的问题也得到了网友的热情回答,还算顺利,但是直到昨天,我在改写原来MSsql里面的全文索引语句为PHP的全文索引语句时,无意中看到,MSSQL居然不支持中文的全文索引,于是我感到无比的挫败感,这该怎么办呢?这么好的数据库却有这么一个难题,于是想对于一个如此成熟的数据库,同时全文索引又是一个常用功能,那么网上的解决方案一定是现成的,好吧,我开始了百度,这下才发现,很难找到像样的资料可以轻松解决此问题,很多解决方案是那么那么的晦涩难懂。对于我这样的水平的人,看完都不容易更不要说执行了,尝试了其中一种Sphinx+CoreSeek4.1的方案,连安装调试都过不去,可以找到的资料也是很少很少,唉。。。愁人的

所以,我就想问问大家,为什么你们都不需要解决中文模糊搜索过程中全文索引的建立和使用问题吗?这不是一个很普遍的应用吗?

再或者,请你们帮我分析一下,对于这样一个30万左右的数据库,一共10个字段,想对其中5个字段实现模糊搜索,是通过spnix/coreseek建立中文的全文索引的搜索速度快,还是直接使用like 汉字关键词%的速度快呢?或者慢的话又能慢多少呢?

我现在没有这方面的经验,所以一直在纠结,要是二者没有显著性差别,我觉得为这个搭建一个spnix/coreseek环境是不是有些太那个了呢?而且还那么那么那么那么难搭建的,真心懊恼,不知道咋办了,请帮我一下好吗?谢谢大家。

mysql5.7 支持中文分词 全文索引

你既然用阿里云了,那就尝试他们的【开放搜索】服务看看
或者你觉得拥抱开源,也可以尝试在linux下配置segmentfault.com在用或曾经用过的http://www.xunsearch.com/,个人感觉相对还是比较简单的,而且性能比mysql全文索引要强

coreseek,sphinx拿来用研究试试

表引擎建议使用MySQL5.6.4后的InnoDB,是支持中文索引的.简易中文分词SCWS提供有PECL扩展以及中文词典,PHP使用起来很方便:
http://www.xunsearch.com/scws/docs.php#instscws
比如要实现对自己博客的所有文章进行全文搜索:
show variables like '%ft_min%' 可见默认值:
MyISAM: ft_min_word_len=4
InnoDB: innodb_ft_min_token_size=3
把单词最小单词长度设为1,这样就能索引和查找只有一个字符的关键词了:
执行 set global innodb_ft_min_token_size=1;
返回 Variable 'innodb_ft_min_token_size' is a read only variable
所以只能在my.cnf的[mysqld]下配置:

<code>ft_min_word_len=1
innodb_ft_min_token_size=1
</code>
Copy after login
<code>建表:
CREATE TABLE `articles` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `title` varchar(200) NOT NULL DEFAULT '',
    `content` text,
    `article_fc` text,
    PRIMARY KEY (`id`),
    FULLTEXT (`article_fc`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
或者后期添加FULLTEXT: ALTER TABLE `articles` ADD FULLTEXT (`article_fc`);

INSERT INTO `articles`(`title`, `content`, `article_fc`) 
VALUES ('标题1', '中国人民银行', '中国 人民 银行 中国人民银行');

INSERT INTO `articles`(`title`, `content`, `article_fc`) 
VALUES ('标题2', '中华民族伟大复兴', '中华 民族 伟大 复兴 中华民族');

SELECT * FROM articles WHERE MATCH(article_fc) AGAINST('中国 复兴');</code>
Copy after login

如果只是查询表单自动完成呢
有没有树型存储结构来存放拼音对应的单词

建议选用第三方专用搜索引擎

ElasticSearch

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? Apr 01, 2025 pm 03:03 PM

The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...

Top 10 PHP CMS Platforms For Developers in 2024 Top 10 PHP CMS Platforms For Developers in 2024 Dec 05, 2024 am 10:29 AM

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

See all articles