gpt4 book ai didi

ruby-on-rails - 用Tire重写SQL查询

转载 作者:行者123 更新时间:2023-12-02 23:41:52 25 4
gpt4 key购买 nike

我正在尝试用Tire重写查询。

这是我拥有的模型:

class User < ActiveRecord::Base
has_many :bookmarks, :dependent => :destroy
has_many :followed_venues, :through => :bookmarks, :source => :bookmarkable, :source_type => 'Venue'
end

用户可以跟随 field 。我需要搜索某些用户遵循的场所。

到目前为止,我一直在使用ActiveRecord进行操作:
@user.followed_venues.where(["venues.title LIKE ?", "%"+params[:q]+"%"])

这显然是不理想的,因此我在轮胎应用中添加了elasticsearch。

我将如何搜索带有轮胎的 field ,并由关注它们的用户进行过滤?

最佳答案

我将发布我自己问题的答案。

因此,仅搜索 field 是很容易的。标准Venue.tire.search {...}问题是如何按照遵循 field 的用户进行过滤。我决定不使用Venue模型进行搜索,而是决定为书签建立索引。

这是我的书签模型

class Bookmark < ActiveRecord::Base
belongs_to :bookmarkable, :polymorphic => true

def to_indexed_json
{
:title => bookmarkable.display_name,
:user_id => user_id
}.to_json
end
end

之后,我在索引中有user_id和场所名称。现在搜索变得如此简单:
Bookmark.tire.search :load => {:include => 'bookmarkable'}, :page => page, :per_page => per_page do
query do
fuzzy :title => { :value => query.downcase, :min_similarity => 0.6, :prefix_length => 2}
end
filter :terms, {:bookmarkable_type => ["Venue"], :user_id => [user.id]}
end

现在这不是一个完整的解决方案。我希望我什至正确使用 filter :terms。我现在回来的结果实际上是一个书签数组。但是为它们加载实际的 field 很容易,并且可能将其包装在WillPaginate集合中,以便在前端更好地分页。

这个解决方案有问题吗?将用户ID放入场所索引时,它与 phoet建议的结果相比如何?

关于ruby-on-rails - 用Tire重写SQL查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9018291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com