在数据库中使用拆分文本进行搜索并获得重击值 [英] Search with split text in DB and get re-hitting value
问题描述
我想通过搜索查询中的拆分数组从数据库中获取最匹配的数据.
假设我的查询是:如何购买摩托车" &在数据库中,我已经征募了一些数据,例如
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
-
购买直升机
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?