使用 AJAX 进行实时搜索:如何实现服务器部分

发布于 2024-12-02 09:04:52 字数 637 浏览 0 评论 0原文

我正在为我公司的代理构建一个简单的案例管理系统,我认为实现某种形式的搜索建议功能会很好,就像谷歌或(最近的)维基百科那样。

所以,情况是这样,还有一个问题:

我有一个包含以下列的表(或者更确切地说,视图):

1. Firstname,
2. Lastname,
3. Phone,
4. Category
5. Owner,
6. Status,
7. Creator

(For our sanities sake lets assume every column is of type text or (n)varchar or 
any other representation of a 'string')

我当然可以简单地搜索每一列,然后呈现结果,但脚本无法知道哪个结果(或“建议”)与用户最相关。

那么,从服务器的角度来看,如何实际实现实时搜索

当然,我更喜欢通过 SQL 完成搜索,但我不能使用存储过程,因此尽管可能可行,但它相当有限。

编辑:(为了澄清):我想搜索我的列并返回与用户搜索内容最接近且最相关的结果(类似于 Google 的做法) )。最好通过 SQL,但如果速度相当快,也可以使用 PHP。

I'm building a simple case management systen for the agents on my company and I thought it would be nice to implement some form of search-suggestion-thingie, much like Google or (more recently) wikipedia has.

So, here´s the situation, and a question:

I have a table (or, rather, a view) with the following columns:

1. Firstname,
2. Lastname,
3. Phone,
4. Category
5. Owner,
6. Status,
7. Creator

(For our sanities sake lets assume every column is of type text or (n)varchar or 
any other representation of a 'string')

I could of course simply search each column and then present the result, but the script would have no way to know which result (or 'suggestion') would be the most relevant to the user.

So, how do you actually implement a live search from the servers point of view?

I would, of course, prefer to have the search done via SQL, but I can´t use stored procedures, so albeit probably possible, it´s rather limiting.

EDIT: (for clarification): I want to search my columns and return the result that is closest, and most relevant, to what the user searches (similar to what Google does). Preferably via SQL, but PHP is possible to use if it is reasonably fast.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟凡古楼 2024-12-09 09:04:52

首先,您需要一个可以向其传递请求的网址。尽可能少地加载其他资源非常重要。由于您只想执行查询并返回响应,因此最好避免引导框架的很大一部分(如果您正在使用框架)。这很重要,因为您希望搜索提示请求快速。

因此,网址将为 example.com/search/searchtermexample.com/search?searchterm=searchterm

该 url 应路由到一个脚本,您可以在其中读出搜索词并对其执行搜索。无论您使用的是 MySQL 数据库、SOLR 服务器还是其他东西,都并不重要(从代码角度来看,MySQL 并未针对全文搜索进行优化,但那是另一个主题)

searchterm 的结果可以返回为JSON 编码字符串。这很容易在 javascript 中处理(我想你的问题的客户端实现将使用 javascript)

MySQL 方式可能是这样的:

search.php

$searchTerm = real_escape_string($_GET['searchterm']);

$sql = "SELECT `lastname` FROM `tablename` WHERE `lastname` LIKE '%" . $searchTerm . "%'";

//Use the result of the query to build a piece of HTML OR return a JSON encoded array and build the HTML client side.
$output = ....

header('Content-type: application/json');
echo PBJSON::encode($output);

编辑:

在了解 Hannes 的实际问题后,我建议使用 levenshtein 检查找到的哪一列是最佳匹配。该函数检查两个字符串之间的差异并返回它。使用它,您可以看到哪一列是最接近的匹配,并使用此信息来决定您将返回什么。

First you need an url you can pass a request to. It is important that you load as little other resources there as possible. Since you're only going to perform a query and return a response it would be good to avoid bootstrapping a large part of your framework (if you're using one). This is important because you want the search hint request to be speedy.

So the url would be example.com/search/searchterm or example.com/search?searchterm=searchterm.

That url should route to a script where you read out the searchterm and perform a search for it. Whether you're using a MySQL database, a SOLR server or something else doesn't really matter (Code wise that is. MySQL is not optimized for full-text search but thats a whole other topic)

The result of searchterm could be returned as JSON Encoded string. This is easily handled in javascript (I suppose the client side implementation of your problem will be using javascript)

The MySQL way could be something like this:

search.php

$searchTerm = real_escape_string($_GET['searchterm']);

$sql = "SELECT `lastname` FROM `tablename` WHERE `lastname` LIKE '%" . $searchTerm . "%'";

//Use the result of the query to build a piece of HTML OR return a JSON encoded array and build the HTML client side.
$output = ....

header('Content-type: application/json');
echo PBJSON::encode($output);

EDIT:

After understanding the actual question from Hannes, I suggested to use the levenshtein to check which of the columns found was the best match. This function checks the difference between two strings and returns it. Using that you could see which of the columns was the closest match and use this information to decide what you will be returning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文