- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我在使用 Sunspot 时按组搜索时遇到了一些问题。
这是一个例子:
# == Schema Information
#
# Table name: movies
#
# id :integer(4) not null, primary key
# title :string(255)
class Movie < ActiveRecord::Base
has_and_belongs_to_many :actors
searchable do
text :title
integer :ages, multiple: true do
actors.map(&:age)
end
text :names, multiple: true do
actors.map(&:name)
end
end
end
# == Schema Information
#
# Table name: actors
#
# id :integer(4) not null, primary key
# name :string(255)
# age :integer(30)
class Actor < ActiveRecord::Base
has_and_belongs_to_many :movies
searchable do
integer :age
text :name
end
end
我想找到每一部电影都有一个叫约翰的 Actor ,年龄是 30 岁。
Movie.search do
with(:names).equal_to("John")
with(:ages).equal_to(30)
with(:title).equal_to("...")
# ...
end
问题就在这里,它可能会找到一部有两个 Actor 的电影;一个叫约翰,一个 30 岁。有没有办法以某种方式将其组合在一起,以便找到的电影中有一个 30 岁的 Actor 叫约翰?
最佳答案
解决方案,就像 Maurício Linhares 在他的评论中所写的那样,是通过 Actor 模型并按电影分组。
问题是 Sunspot 不支持 Solr 3.3 或 4.0,这是唯一支持分组的 Solr 版本。
这是我使用 Sunspot 1.2.1 和 Solr 3.3 的解决方案。
在我的示例中,movie_id
被放置在类型转换中,这在我的实际应用程序中并未完成。
# == Schema Information
#
# Table name: actors
#
# id :integer(4) not null, primary key
# name :string(255)
# created_at :datetime
# updated_at :datetime
# movie_id :integer(4)
#
class Actor < ActiveRecord::Base
searchable do
# We need to store the movie_id as an string
# So it can be sorted. We also need to pass the
# stored: true params
string :movie_id, stored: true do
movie_id.to_s
end
end
def search_using_solr
scoped = Sunspot.new_search(Actor)
scoped.build do
adjust_solr_params do |params|
params[:group] = true
params[:"group.field"] = "movie_id_s"
params[:"group.format"] = "simple"
end
end
# Sunspot 1.2.1 doesn't support grouping, so we need to do some hacking.
def scoped.hits
@hits ||= @solr_result["grouped"].values.first["doclist"]["docs"].map do |doc|
Sunspot::Search::Hit.new(doc, nil, self)
end
end
def scoped.total
@total ||= @solr_result["grouped"]["movie_id_s"]["matches"] || 0
end
# Here we'll only fetch the stored data from Solr it self,
# and then pass it manualy to ActiveRecord.
Movie.where({
id: scoped.execute.hits.map{ |h| h.stored(:movie_id) }
})
end
end
alindeman 因他的 example gist 而受到赞誉。
关于ruby-on-rails - 组使用 Sunspot/Solr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7031836/
我支持 Rails 项目,其中包含 Rails 应用程序和 Solr 的附加实例。 我的环境:rails 3.2.1、ruby 2.1.2、sunspot 2.1.0、Solr 4.1.6。 问题:
在 Solr 中添加和提交之间的根本区别是什么?我们已经阅读了几个文档,但现在仍然非常清楚它到底做了什么,以及何时使用 Add 和何时使用 Commit? 据我了解,Add 将数据添加到 solr 数
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 5年前关闭。 Improve t
我们可以在 solr suggester 响应中添加上下文而不是上下文过滤吗? 我有 5 个不同的类别。每个类别都有不同的名称。我的建议者在名字上工作。我可以得到如下输出吗? { "term" :
在 solrconfig.xml 中,filterCache(或 queryResultCache 等)的 'autowarmCount' 表示当新的搜索者到来时将复制多少缓存实体。但是,如果我在 s
我们计划部署 Solr 来搜索从通用 CMS 平台发布的多个站点。 每种语言都有单独的网站,其他语言的内容主要是从英语翻译过来的。 搜索要求包括 – 关键字突出显示、建议(“你是什么意思?”)、停用词
我们有一个系统,使用户能够创建应用程序并在其应用程序上存储数据。我们希望将每个应用程序的索引分开。我们为每个应用程序创建一个核心,并在用户进行查询时搜索给定的应用程序。由于应用程序之间没有任何关系,因
我写了一个小型搜索引擎作为我的每周项目。它基于查询向量和文档向量之间的余弦相似度。向量是使用 tf-idf 标记的疮计算的。 我开始了解 Apache Solr,它是一个全文搜索引擎。我的问题是 so
为了索引我的网站,我有一个 Ruby 脚本,它反过来生成一个 shell 脚本,将我的文档根目录中的每个文件上传到 Solr。 shell 脚本有很多行,如下所示: curl -s \ "htt
是否可以分享Solr fieldType s 定义于 schema.xml多核之间? 我在 Solr 中有许多核心,发现自己正在重新定义 fieldType s 仅基于内置过滤器和分词器。例如
我想通过命令停止 solr 所以如果找到这篇文章 http://rc98.net/solrinit echo "Stopping Solr" cd $SOLR_DIR
我想用守护进程运行 solr。我在另一篇文章中看到有一个可以运行的 init.d 脚本,但它在我的 ubuntu 环境中似乎有问题。每当我尝试使用/etc/init.d/solr start 运行脚本
我有一个 solr 搜索返回上下文突出显示结果,显示网址和电子邮件,句点后带有空格 - 例如“www.google.com”或“email@google.com”无论如何要关闭它,以便它们正常显示?谢
我遇到了一个问题,其中一个列是多值的。例如:值可以是 (11,22) (11,33) (11,55) , (22,44) , (22,99) 我想执行一个分组操作,它将产生: 11 : 计数 3 22
这个问题在这里已经有了答案: How to select distinct field values using Solr? (6 个回答) 6年前关闭。 我有如下 solr 索引数据 7920
背景 使用 Solr 4.0.0。我已经索引了一组示例文档的文本并启用了术语向量,因此我可以使用快速向量突出显示 为了突出显示,我正在使用带有句子边界的 Break Iterator Boundar
题 我在哪里可以找到一个完整的示例,该示例展示了从索引文档到检索搜索结果的分层分面搜索是如何工作的? 我的研究到目前为止 Stackoverflow 有一些帖子,但它们都只针对分层分面搜索的某些方面;
我正在尝试开始使用 Apache Solr,但有些事情我不清楚。通读tutorial ,我已经设置了一个正在运行的 Solr 实例。我感到困惑的是 Solr 的所有配置(架构等)都是 XML 格式的。
我将以下文档存储在 Solr 中: doc { id: string; // this is a unique string that looks like an md5 result
我有一个关于在 solr 中创建嵌套字段的可能性的问题。 谷歌搜索告诉我一些关于组的信息,但我认为它只是为了结果? 我想要的是这样的结构: 类别1 项目 1 (9) 项目 2 (8) 类别2 项目 3
我是一名优秀的程序员,十分优秀!