在数据库中使用拆分文本进行搜索并获得重击值 [英] Search with split text in DB and get re-hitting value

查看:90
本文介绍了在数据库中使用拆分文本进行搜索并获得重击值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过搜索查询中的拆分数组从数据库中获取最匹配的数据.
假设我的查询是:如何购买摩托车" &在数据库中,我已经征募了一些数据,例如

I'd like to get the most matched data from DB by split array in search query.
Imagine my query is: "How to buy a motorbike" & in DB I've enlisted some data like

  1. 购买直升机

  1. buy a helicopter

买摩托车

买车

因此,拆分查询后,它将以如何" 至" 购买" 摩托车" .使用这些子字符串,查询仅在输出中生成"购买直升机"字符串.但是我想购买摩托车数据.我生成了一个方法,但仅在这些子字符串中的任何一个与数据库中的相同数据匹配时才对数据进行编码. 这是我的代码,该代码仅获取第一个匹配的数据,但我所需的数据为否. 2.

So, after split the query, it'll search into DB as "how", "to","buy", "motorbike" respectfully. Using these sub-strings, the query only generate "buy a helicopter" string in output. But I want to get buy a motorbike data. I generated a method but it only encode data when any of these sub-strings get matched with same data in DB. Here's my code that fetch only first matched data but my desired data is in no. 2.

$str=$_POST['search']; 
$str=preg_split("/[\s]+/",  $str);
foreach ($str as $search ) {
$sql = "SELECT answer FROM query_tbl WHERE q1 like  '%".$search."%' ";
$record = mysqli_query($link, $sql);
$rows=mysqli_fetch_assoc($record);
  if ($rows == true) {
     echo  json_encode(array('ans'=>$rows['answer']));
    } 
}

那么,我应该遵循哪种算法以及在哪里可以使用它来实现我的目标?

So, which algorithm should i follow and where to use it to achieve my goal?

推荐答案

我认为最好按照

默认情况下,或者使用IN NATURAL LANGUAGE MODE修饰符,MATCH()函数针对文本集合对字符串进行自然语言搜索.集合是FULLTEXT索引中包含的一组一个或多个列.搜索字符串作为AGAINST()的参数给出.对于表中的每一行,MATCH()返回一个相关性值;也就是说,在搜索字符串和MATCH()列表中命名的列中该行中的文本之间的相似性度量.

By default or with the IN NATURAL LANGUAGE MODE modifier, the MATCH() function performs a natural language search for a string against a text collection. A collection is a set of one or more columns included in a FULLTEXT index. The search string is given as the argument to AGAINST(). For each row in the table, MATCH() returns a relevance value; that is, a similarity measure between the search string and the text in that row in the columns named in the MATCH() list.

这篇关于在数据库中使用拆分文本进行搜索并获得重击值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆