【问题标题】:Texticle and ActsAsTaggableOnTexticle 和 ActsAsTaggableOn
【发布时间】:2012-04-26 20:02:53
【问题描述】:

我正在尝试将标签搜索作为 Texticle 搜索的一部分。由于 texticle 不会从同一个模型中搜索多个表,因此我最终创建了一个名为 PostSearch 的新模型,遵循 Texticle 关于 System-Wide Searching 的建议

class PostSearch < ActiveRecord::Base

  # We want to reference various models
  belongs_to :searchable, :polymorphic => true
  # Wish we could eliminate n + 1 query problems,
  # but we can't include polymorphic models when
  # using scopes to search in Rails 3
  # default_scope :include => :searchable

  # Search.new('query') to search for 'query'
  # across searchable models
  def self.new(query)
    debugger
    query = query.to_s
    return [] if query.empty?
    self.search(query).map!(&:searchable)
    #self.search(query) <-- this works, not sure why I shouldn't use it.
  end

  # Search records are never modified
  def readonly?; true; end

  # Our view doesn't have primary keys, so we need
  # to be explicit about how to tell different search
  # results apart; without this, we can't use :include
  # to avoid n + 1 query problems
  def hash
   id.hash
  end

  def eql?(result)
    id == result.id
  end

end

在我的 Postgres DB 中,我创建了一个这样的视图:

  CREATE VIEW post_searches AS
  SELECT posts.id, posts.name, string_agg(tags.name, ', ') AS tags
    FROM posts
      LEFT JOIN taggings ON taggings.taggable_id = posts.id 
        LEFT JOIN tags ON taggings.tag_id = tags.id 
  GROUP BY posts.id;

这让我可以得到这样的帖子:

SELECT * FROM post_searches
id | name | tags
1    Intro  introduction, funny, nice

所以看起来应该没问题。不幸打电话 PostSearch.new("funny") 返回 [nil] (NOT [])。翻看Texticle源代码,好像PostSearch.new中的这一行

self.search(query).map!(&:searchable)

使用某种 searchable_columns 方法映射字段,它是否正确?结果为零。

另一方面,除非我将其从文本类型转换为 varchar 类型,否则不会在 texticle SQL 查询中搜索标签字段。

所以,总而言之: 为什么找到对象时会映射为nil?

为什么 texticle 会忽略我的标签字段,除非它是 varchar?

【问题讨论】:

    标签: ruby-on-rails acts-as-taggable-on texticle


    【解决方案1】:

    Texticle 将对象映射到 nil 而不是什么都没有,这样您就可以检查 nil? - 这是防止检查不存在的项目时出错的保护措施。可能值得向tenderlove 本人询问他这样做的确切原因。

    我并不完全肯定 Texticle 为什么会忽略非 varchars,但 it looks like 这是一种性能保障,因此 Postgres 不会进行全表扫描(在 Creating Indexes for Super Speed部分下>):

    您需要为查询的每个文本/字符串列添加索引,否则 Postgresql 将恢复为全表扫描而不是使用索引。

    【讨论】:

    • 嗯...但是我不能在视图上创建索引,可以吗?此外,我认为将其设为 nil 的原因是有道理的,但您知道为什么它实际上被映射为 nil 吗?
    • 视图是临时表,因此您不能直接索引它们,但您可以索引您从中绘制视图的基础表。我不知道为什么它被映射到nil,所以我建议你联系 texticle 的创建者来调查“为什么”。
    猜你喜欢
    • 1970-01-01
    • 2011-03-23
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多