Php 如何在全文搜索中处理多个搜索条件和优先级

Php 如何在全文搜索中处理多个搜索条件和优先级,php,mysql,full-text-search,Php,Mysql,Full Text Search,是否有可能以任何方式减少执行的查询? 因为我现在这样做还可以,但是以后我会有30个查询,这在我看来并不合适 我的剧本 $string = 'new movie stars'; $words = preg_split('/(\/|\s+)/', $string); print_r($words); 阵列([0]=>新[1]=>电影[2]=>明星) $sql=“从电影中选择*匹配(名称)(在布尔模式下为“+$words[0]+$words[1]+$words[2]”); $query\u nam

是否有可能以任何方式减少执行的查询? 因为我现在这样做还可以,但是以后我会有30个查询,这在我看来并不合适

我的剧本

$string = 'new movie stars';
$words =  preg_split('/(\/|\s+)/', $string);
print_r($words);
阵列([0]=>新[1]=>电影[2]=>明星)

$sql=“从电影中选择*匹配(名称)(在布尔模式下为“+$words[0]+$words[1]+$words[2]”);
$query\u name=$this->db->query($sql);
如果($query\u name->num\u rows<20){
$sql=“从电影中选择*,其中匹配(名称)与(“+$words[0]+($words[1]$words[2])在布尔模式下)”;
$query\u name\u two=$this->db->query($sql);
}
如果(计数($query\u name->num\u rows+$query\u name\u two->num\u rows)<20){
$sql=“从匹配(名称)的电影中选择*(布尔模式下的“$words[0]$words[1]$words[2]”);
$query\u name\u three=$this->db->query($sql);
}
您的代码容易受到相关攻击。甚至不能完全保护它。请学会使用

现在,除了上述建议外,还有两种可能的修复方法:

修复#1用于将输入字符串标记为FTS单词的php代码不足。不久前,我确实创建了一个函数,以更健壮的方式处理此需求。您可以使用以下选项:

/**
 * Method to take an input string and tokenize it into an array of words for Full Text Searching (FTS).
 * This method is used when an input string can be made up of multiple words (let's say, separated by space characters),
 * and we need to use different Boolean operators on each of the words. The tokenizing process is similar to extraction
 * of words by FTS parser in MySQL. The operators used for matching in Boolean condition are removed from the input $phrase.
 * These characters as of latest version of MySQL (8+) are: +-><()~*:""&|
 * We can also execute the following query to get updated list: show variables like 'ft_boolean_syntax';
 * Afterwards, the modified string is split into individual words considering either space, comma, and, period (.) characters.
 * Details at: https://dev.mysql.com/doc/refman/8.0/en/fulltext-natural-language.html
 * @param string $phrase Input statement/phrase consisting of words
 * @return array Tokenized words
 * @author Madhur, 2019
 */
function tokenizeStringIntoFTSWords(string $phrase) : array {
    $phrase_mod = trim(preg_replace('/[><()~*:"&|+-]/', '', trim($phrase)));
    return preg_split('/[\s,.]/', $phrase_mod, null, PREG_SPLIT_NO_EMPTY);
}

现在是阅读使用的好时机。最好在@Tim Biegeleisen上问这个问题。你能举个例子吗?如果你不介意的话,自然模式不是我想要的,因为我知道你的查询会做:使用更高优先级的$words[0]或$words[1]或$words[2]进行搜索@GameClub流我的查询不是使用
自然模式
,而是只使用布尔模式。尝试一下这个查询,我相信结果应该按照您的要求进行排名。如果没有,请提供一些样本数据证明你的案例你能加入吗?我使用正确的atm设置查询结果,如果您尝试,您的查询将打印所有结果results@GameClubStreaming检查此项:@GameClubStreaming是的,您不需要更多的IFs
BOOLEAN
模式提供了许多运算符,这些运算符应该能够在单个查询中满足您的要求。
/**
 * Method to take an input string and tokenize it into an array of words for Full Text Searching (FTS).
 * This method is used when an input string can be made up of multiple words (let's say, separated by space characters),
 * and we need to use different Boolean operators on each of the words. The tokenizing process is similar to extraction
 * of words by FTS parser in MySQL. The operators used for matching in Boolean condition are removed from the input $phrase.
 * These characters as of latest version of MySQL (8+) are: +-><()~*:""&|
 * We can also execute the following query to get updated list: show variables like 'ft_boolean_syntax';
 * Afterwards, the modified string is split into individual words considering either space, comma, and, period (.) characters.
 * Details at: https://dev.mysql.com/doc/refman/8.0/en/fulltext-natural-language.html
 * @param string $phrase Input statement/phrase consisting of words
 * @return array Tokenized words
 * @author Madhur, 2019
 */
function tokenizeStringIntoFTSWords(string $phrase) : array {
    $phrase_mod = trim(preg_replace('/[><()~*:"&|+-]/', '', trim($phrase)));
    return preg_split('/[\s,.]/', $phrase_mod, null, PREG_SPLIT_NO_EMPTY);
}
SELECT * FROM movie 
WHERE 
  MATCH(name) 
  AGAINST('>:first_word :second_word :third_word ..and so on)' IN BOOLEAN MODE)
ORDER BY 
  MATCH(name) 
  AGAINST('>:first_word :second_word :third_word ..and so on)' IN BOOLEAN MODE) 
  DESC
LIMIT 20