简体   繁体   English

如何进行全文搜索

[英]How to make a fulltext search

I want to make a fulltext search with metaphone.我想用元音进行全文搜索。 Everythings works fine.一切正常。 I have 4 fields ie.我有 4 个字段,即。

ID |Category     | Type          |Title                        |Meta
1  |Vehicle      |4 Wheelers     |Farrari Car for Sale         |FHKL WLRS FRR KR FR SL
2  |Real Estate  |Residential Apt|3BHK for sale                |RL ESTT RSTN APT BK FR SL
3  |Music        |Instruments    |Piano for sale               |MSK INST PN FR SL
4  |Stationary   |College        |Bag for $50                  |STXN KLJ BK FR
5  |Services     |Job            |Vacancy for Jr.Web Developer |SRFS JB FKNS FR JRWB TFLP

The above is the sample data.以上是样本数据。 Here I want to use metaphone and fulltext search using match()against().在这里,我想使用 match()against() 使用变音素和全文搜索。 Everything works fine.一切正常。 However Some words like Bag, Job and Car are ignored as the default minimum character 4. The issue is now that I use shared hosting and the hosting provider has told me that he cannot provide me a mysql config file nor can they change this so doing this in config file ft_min_word_len = 2 is not an option.然而,像 Bag、Job 和 Car 这样的一些词被忽略为默认的最小字符 4。现在的问题是我使用共享主机,并且托管服务提供商告诉我他不能向我提供 mysql 配置文件,他们也不能这样做这在配置文件ft_min_word_len = 2不是一个选项。

//Code for generating metaphone

<?php 
    $string = "Vacancy for Jr.Web Developer";
    $a = explode(" ", $string);
    foreach ($a as $value) {
        echo metaphone($value,4)."<br>";
    }
?>

I am using normal我使用正常

SELECT * FROM tbl_sc WHERE MATCH(META) AGAINST('$USER_SEARCH');

All the information in the database are user generated so I cannot supervise.数据库中的所有信息都是用户生成的,所以我无法监督。 Since I use mysql, PHP and on a shared hosting.由于我使用 mysql、PHP 和共享主机。 I cannot use any elastic search library or solr like things.我不能使用任何弹性搜索库或 solr 之类的东西。 I have searched google and stack overflow however I am not able to get anything我已经搜索过谷歌和堆栈溢出,但是我什么也没有得到

One options is using LIKE operator but I want to use MATCH() AGAINST() if possible.一种选择是使用 LIKE 运算符,但如果可能,我想使用 MATCH() AGAINST()。

Kindly help me out with some work around or alternate route.请帮助我解决一些工作或替代路线。

first there are three types of fulltext searches首先有三种类型的全文搜索

Natural language full text search自然语言全文搜索

Boolean fulltext searches布尔全文搜索

Query expansion searches查询扩展搜索

what suits your question here is the natural language full-text search, since your queries are mostly in free language and uses no special characters.the syntax goes like this这里适合您的问题是自然语言全文搜索,因为您的查询主要使用自由语言并且不使用特殊字符。语法如下

SELECT * FROM table_name WHERE MATCH(col1, col2)
AGAINST('search terms' IN NATURAL LANGUAGE MODE)

in your case first, add the fulltext functionality to your table在您的情况下,首先将全文功能添加到您的表格中

$stmt_txt_search = $conn->prepare("ALTER TABLE tbl_sc ADD FULLTEXT (Category, Type, Title, Meta)");
$stmt_txt_search->execute();

your query should be something like this你的查询应该是这样的

$stmt_match = $conn->prepare("SELECT * FROM tbl_sc WHERE MATCH (Meta) AGAINST(? IN NATURAL LANGUAGE MODE)");
$stmt_match->bind_param("s",$USER_SEARCH);
$stmt_match->execute();

to alter the ft_min_word_len you have to go the the my.cnf file, change it to the desired value, restart the server and rebuild your indexes like so要更改ft_min_word_len您必须转到 my.cnf 文件,将其更改为所需的值,重新启动服务器并像这样重建索引

[mysqld]
set-variable = ft_min_word_len=3

then然后

mysql> ALTER TABLE tbl_sc DROP INDEX Title, Category...;
mysql> ALTER TABLE tbl_sc ADD FULLTEXT Title, Category...;

but since you are in shared hosting account, you cannot access the my.cnf file.但由于您使用的是共享主机帐户,因此您无法访问my.cnf文件。 However, you using SHOW VARIABLES and INFORMATION SCHEMA you can see all set variables and even change them using SET in your session such that all db connections will be based on the newly set values但是,您使用SHOW VARIABLESINFORMATION SCHEMA您可以看到所有设置的变量,甚至在会话中使用SET更改它们,以便所有数据库连接都基于新设置的值

for instance to SHOW VARIABLES in sql you can use例如在 sql 中SHOW VARIABLES ,您可以使用

SELECT * FROM information_schema.global_variables; this shows all existing variables in your current session, for a variable like flush time it can be set to 1 using SET flush_time = 1;这都显示现有的变量在当前会话,像一个变量flush time可以设置为1使用SET flush_time = 1; so now the database will have a flushtime of 1 onwards, in your case i suppose the variables ft_max_word_len and ft_min_word_len are dynamically changeable and i would therefore suggest trying所以现在数据库的刷新时间为 1 起,在你的情况下,我认为变量ft_max_word_lenft_min_word_len是动态可变的,因此我建议尝试

SET ft_min_word_len = 2; within your current session, for more information see server system variables在您当前的会话中,有关更多信息,请参阅服务器系统变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM