具有多对多关系的 sphinx 索引

发布于 2024-10-12 07:02:00 字数 682 浏览 3 评论 0原文

我正在尝试建立一个在艺术品和流派之间具有基本的多对多关系的 Sphinx 索引:

artworks
---------------
id
title
description

genres
---------------
id
name

artwork_genres
---------------
artworks_id
genres_id

在我的 sphinx 配置文件中,我有类似的内容

source src_artwork {
    ...
    sql_query    = SELECT id, title, description FROM artworks
    sql_attr_multi = uint tag from query; SELECT id,name FROM genres
}

据我所知,这是来自文档 多值属性sql_attr_multi

但显然那里没有提到绑定表,我无法理解它是如何带入配置中的。我只是想搜索“印象派”以找到属于该流派的艺术品(如果该术语在其他领域中出现,则适当加权)

I am trying to set up a Sphinx index with a basic many-to-many relation between artworks and genres:

artworks
---------------
id
title
description

genres
---------------
id
name

artwork_genres
---------------
artworks_id
genres_id

In my sphinx config file I have something like

source src_artwork {
    ...
    sql_query    = SELECT id, title, description FROM artworks
    sql_attr_multi = uint tag from query; SELECT id,name FROM genres
}

This is from the docs, as far as I can understand, on multi-valued attributes and sql_attr_multi

But obviously there is no mention of the tie table in there and I can't understand how that is brought into the config. I'd simply like for a search on "Impressionism" to result in artworks belonging to that genre (weighted as appropriate if the term is seen in the other fields)

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

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

发布评论

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

评论(1

痴意少年 2024-10-19 07:02:00

在这种情况下,我会考虑忽略属性功能。创建用于搜索艺术品的流派字段的最简单方法是将流派表“去规范化”到 sql_query 中。

在 SQL 查询的 FROM 子句中,您可以通过链接表将流派表 JOIN 到艺术品。在 SELECT 子句中,您可以将 GROUP_CONCATgenels.name 放入一列中,该列将成为要搜索的 Sphinx 字段。

您的 sql_query 可能如下所示:

source src_artwork {
        ...
    sql_query    = SELECT a.id, a.title, a.description, GROUP_CONCAT( DISTINCT g.name SEPARATOR ' ') AS genre \
        FROM artworks AS a \
        LEFT JOIN artwork_genres AS ag ON ag.artworks_id = a.id \ 
        LEFT JOIN genres AS g ON g.id = ag.genres_id
        GROUP BY a.id;
}

然后,在 @genre 字段中查找“印象派”的艺术品的 sphinx 搜索将返回“行”。

I would consider ignoring the attributes feature in this case. The simplest way to create a genre field by which to search artworks is to "de-normalise" the genres table into the sql_query.

In the FROM clause of your SQL query, you would JOIN the genres table to the artworks via the linking table. In the SELECT clause, you can then GROUP_CONCAT genres.name into a column, which becomes a Sphinx field to search on.

Your sql_query might look like this:

source src_artwork {
        ...
    sql_query    = SELECT a.id, a.title, a.description, GROUP_CONCAT( DISTINCT g.name SEPARATOR ' ') AS genre \
        FROM artworks AS a \
        LEFT JOIN artwork_genres AS ag ON ag.artworks_id = a.id \ 
        LEFT JOIN genres AS g ON g.id = ag.genres_id
        GROUP BY a.id;
}

Then a sphinx search for artworks looking for "impressionism" in the @genre field will return the "row".

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