使用有序 Sphinx 搜索输出获取 MySQL 条目时对冗余进行排序

发布于 2024-12-16 15:11:30 字数 1202 浏览 0 评论 0原文

我有一个使用 Sphinx 建立索引的 MySQL 表,其中有一堆列作为属性,我想让我的用户对其搜索结果进行排序(例如名称、评级等)。

所以我告诉 Sphinx 这样做(例如,在 PHP 中):

$sphinx = new SphinxClient();

// Retrieve $query, $sort_attr, and $order from $_GET    

$sphinx->SetMatchMode(SPH_MATCH_ANY);
$sphinx->SetArrayResult(true);

$sphinx->SetSortMode($order, $sort_attr);
$sphinx->SetLimits( /* something reasonable, <1000 */ );
$results_sphinx = $sphinx->Query($query, 'table');

这有效,我得到了我的有序结果。

我还想将所有属性(以及其他一些应保持未索引的列)显示为搜索结果的一部分。这意味着我必须从数据库中获取搜索结果的每一项。

因此,我进行以下 MySQL 调用:

SELECT id, colA, colB, [...] FROM table WHERE table.id IN ([IDs returned from Sphinx, in some sorted order])

但是,即使从 Sphinx 返回的 ID 列表按照属性列(例如字母顺序)按某种排序顺序,WHERE IN 也会按照表索引列的顺序返回结果,这在本例中是 ID 本身。

我想到的唯一选择是使用 ORDER BY:

SELECT id, colA, colB, [...] FROM table WHERE table.id IN ([IDs returned from Sphinx, in some sorted order]) ORDER BY [attribute] [DESC|ASC]

这可行,但我只是让 Sphinx 和 MySQL 对每个搜索实例的同一组数据进行排序。这感觉不太理想。我也不认为我可以将排序留给后一个 MySQL 调用,因为我打算在结果中进行分页,因此从 Sphinx 返回的 ID 必须按某种顺序开始。

StackOverflow 能为我找到避免这种冗余的方法吗?请拆开我上面所做的任何事情。

谢谢!

I have a MySQL table that I indexed using Sphinx, with a bunch of columns as attributes that I want to let my users sort their search results by (e.g. name, ratings, etc.).

So I tell Sphinx to do this (for example, in PHP):

$sphinx = new SphinxClient();

// Retrieve $query, $sort_attr, and $order from $_GET    

$sphinx->SetMatchMode(SPH_MATCH_ANY);
$sphinx->SetArrayResult(true);

$sphinx->SetSortMode($order, $sort_attr);
$sphinx->SetLimits( /* something reasonable, <1000 */ );
$results_sphinx = $sphinx->Query($query, 'table');

This works and I get my ordered results.

I also want to display all the attributes (and some other columns that should remain unindexed) as part of the search results. This means that I have to fetch each item of the search results from the DB.

So I make the following MySQL call:

SELECT id, colA, colB, [...] FROM table WHERE table.id IN ([IDs returned from Sphinx, in some sorted order])

However, even if my list of IDs returned from Sphinx are in some sorted order according to the attribute columns (e.g. alphabetical order), WHERE IN will return results in the order of the table's index column, which in this case is the IDs themselves.

The only option I have in mind is to use ORDER BY:

SELECT id, colA, colB, [...] FROM table WHERE table.id IN ([IDs returned from Sphinx, in some sorted order]) ORDER BY [attribute] [DESC|ASC]

This works, but I just made both Sphinx and MySQL sort the same set of data for each search instance. This feels sub-optimal. I don't think I can leave the sorting to the latter MySQL call either, as I intend to have pagination in my results, so the IDs returned from Sphinx have to be in some order to begin with.

Can StackOverflow find me a way to avoid this redundancy? Please pick apart anything that I did above.

Thanks!

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

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

发布评论

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

评论(1

琉璃梦幻 2024-12-23 15:11:30

您一次返回多少个 ID?如果不是很多,我建议使用 MySQL ORDER BY FIELD,这样

SELECT id, colA, colB, ... FROM table WHERE table.id IN (id1,id2,id3,...) ORDER BY FIELD (table.id,id1,id2,id3,....)

我对 Sphinx/MySQL 搜索和检索做了完全相同的事情,效果很好,从未有过缓慢的查询(尽管我只在 6 和 6 之间获取)一次 12 个 ID)。

How many IDs are you returning at a time? If it isn't many I would suggest using the MySQL ORDER BY FIELD as such

SELECT id, colA, colB, ... FROM table WHERE table.id IN (id1,id2,id3,...) ORDER BY FIELD (table.id,id1,id2,id3,....)

I do the exact same thing for my Sphinx/MySQL searches and retrievals, works great, never had a slow query (although I'm only fetching between 6 and 12 IDs at a time).

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