索引字段 +使用 Thinking Sphinx 自定义文本

发布于 2024-12-01 15:58:42 字数 813 浏览 8 评论 0原文

我有几个不同模型的索引,有时用户可能会搜索多个模型中存在的值。现在,如果用户确实只对其中一个模型的数据感兴趣,我希望用户能够在查询前/后添加一些内容来限制范围。

例如,如果我只想在我的市政模型中查找匹配项,我会在该模型中设置一个索引,以便用户现在可以查询 "xyz市政" (在引号中)

  define_index do
    indexes :name, :sortable => true
    indexes "name || ' municipality' name", :as => :extended_name, :type => :string
  end

:工作得很好。现在我还有一个与市政府相关的人员模型。我希望,当仅在 Person 模型上搜索时,具有相同的可用功能,以便我可以说 Person.search("xyz市政当局") 并使所有人员连接到该市政当局。这是我当前在 Person 模型中的定义:

  has_many :municipalities,  :through => :people_municipalities

  define_index do
    indexes [lastname, firstname], :as => :name, :sortable => true
    indexes municipalities.name, :as => :municipality_name, :sortable => true
  end

但是有什么方法可以在此模型上创建索引,引用市政当局,就像我在市政模型本身上的索引一样?

I've got indexes on a few different models, and sometimes the user might search for a value which exists in multiple models. Now, if the user is really only interested in data from one of the models I'd like the user to be able to pre/postfix the query with something to limit the scope.

For instance, if I only want to find a match in my Municipality model, I've set up an index in that model so that the user now can query "xyz municipality" (in quotes):

  define_index do
    indexes :name, :sortable => true
    indexes "name || ' municipality' name", :as => :extended_name, :type => :string
  end

This works just fine. Now I also have a Person model, with a relation to Municipality. I'd like, when searching only on the Person model, to have the same functionality available, so that I can say Person.search("xyz municipality") and get all people connected to that municipality. This is my current definition in the Person model:

  has_many :municipalities,  :through => :people_municipalities

  define_index do
    indexes [lastname, firstname], :as => :name, :sortable => true
    indexes municipalities.name, :as => :municipality_name, :sortable => true
  end

But is there any way I can create an index on this model, referencing municipalities, like the one I have on the Municipality model itself?

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

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

发布评论

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

评论(1

贵在坚持 2024-12-08 15:58:42

如果您查看源 person_core_0config/development.sphinx.conf 的 sql_query 设置中生成的 SQL,您将看到市政当局.name 是如何连接在一起的(我会发布一个示例,但这取决于您的数据库 - MySQL 和 PostgreSQL 处理此问题的方式完全不同)。

我建议复制该字段,并插入类似这样的内容(SQL 是伪代码):

indexes "GROUP_CONCAT(' municipality ' + municipalities.name)",
  :as => :extended_municipality_names

另外:添加 :sortable true 到此字段或关联中的原始字段都没有多大意义 - 您要按所有字段进行排序吗?市镇名称连接在一起?我猜不是:)

If you look at the generated SQL in the sql_query setting of config/development.sphinx.conf for source person_core_0, you'll see how municipalities.name is being concatenated together (I'd post an example, but it depends on your database - MySQL and PostgreSQL handle this completely differently).

I would recommend duplicating the field, and insert something like this (SQL is pseudo-code):

indexes "GROUP_CONCAT(' municipality ' + municipalities.name)",
  :as => :extended_municipality_names

Also: there's not much point adding :sortable true to either this nor the original field from the association - are you going to sort by all of the municipality names concat'd together? I'm guessing not :)

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