gpt4 book ai didi

php - 订购关键字结果相关性

转载 作者:行者123 更新时间:2023-11-30 23:23:50 25 4
gpt4 key购买 nike

我创建了一个查询,它根据用户创建的关键字从数据库中获取相当短的字符串(归功于 zhikharev 用于他的 answer):

$searchArray = explode(' ', $search);
$searchNumber = count($searchArray);

$query = "SELECT * FROM tags WHERE tags.tag LIKE CONCAT('%',?,'%')" .
str_repeat(" OR tags.tag LIKE CONCAT('%',?,'%')", $searchNumber - 1);

$stmt = $mysqli -> prepare($query);
$bind_names[] = str_repeat('s', $searchNumber);

for ($i = 0; $i < count($searchArray); $i++){
$bind_name = 'bind'.$i; /*generate a name for variable bind1, bind2, bind3...*/
$$bind_name = $searchArray[$i]; /*create a variable with this name and put value in it*/
$bind_names[] = & $$bind_name; /*put a link to this variable in array*/
}

call_user_func_array(array($stmt, 'bind_param'), &$bind_names);
$stmt -> execute();

我的问题是我不知道如何按相关性对结果进行排序。请注意,数据库中的一个标签可以包含多个词。

假设用户搜索“纽约市”。然后在数据库中我有:

    New York
York
New York City
New Zealand
Kansas City

我希望结果看起来像这样:

    New York City
New York
New Zealand
York
Kansas City

它应该寻找与用户输入中的单词最匹配的标签,以及与搜索中的单词顺序大致相同的标签。

最佳答案

这是 Sphinx 等独立全文搜索引擎的完美用例。我最近开始在一个项目中使用 Sphinx,到目前为止我对它非常满意。它的索引速度非常快,我发现速度比直接 MySQL 查询提高了 10 倍。

启用星形模式将允许部分匹配: http://sphinxsearch.com/docs/2.0.6/conf-enable-star.html

它还允许各种排序方法: http://sphinxsearch.com/docs/2.0.6/sorting-modes.html

更多链接: http://sphinxsearch.com/docs/2.0.6/http://php.net/manual/en/sphinx.examples.php

关于php - 订购关键字结果相关性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14287096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com