我有一个带有基于 Elasticsearch 和 Tire 的全文搜索的 rails 应用程序,它已经在一个名为 Category 的 MongoDB 模型上工作,但现在我想添加一个基于MongoID Embedded 1-n model User 的更复杂的搜索,其中 embeds_many :关注列表
现在我必须批量导入和索引监视列表中的所有字段,我想知道:
- 我怎样才能做到这一点 ?
- 可以只索引监视列表子字段,而没有用户父字段吗?
Embedded 1-N MongoDB/MongoID 模型如下所示:
app/models/user.rb (父级):
class User
include Mongoid::Document
include Tire::Model::Search
include Tire::Model::Callbacks
index_name 'users'
field :nickname
field ... many others
embeds_many :watchlists
end
app/models/watchlist.rb (嵌入的“许多”儿童):
class Watchlist
include Mongoid::Document
include Tire::Model::Search
include Tire::Model::Callbacks
index_name 'watchlists'
field :html_url
embedded_in :user
end
关于如何完成任务的任何建议?
更新: 这里是用 mongo shell 看到的模型的一部分
> user = db.users.findOne({'nickname': 'lgs'})
{
"_id" : ObjectId("4f76a16cf2a6a12f88cbca43"),
"encrypted_password" : "",
"sign_in_count" : 0,
"provider" : "github",
"uid" : "1573",
"name" : "Luca G. Soave",
"email" : "luca.soave@gmail.com",
"nickname" : "lgs",
"watchlists" : [
{
"_id" : ObjectId("4f76997f1d41c81173000002"),
"tags_array" : [ git, peristence ],
"html_url" : "https://github.com/mojombo/grit",
"description" : "Grit gives you object oriented read/write access to Git repositories via Ruby.",
"fork_" : false,
"forks" : 207,
"watchers" : 1258,
"created_at" : ISODate("2007-10-29T14:37:16Z"),
"pushed_at" : ISODate("2012-01-27T01:05:45Z"),
"avatar_url" : "https://secure.gravatar.com/avatar/25c7c18223fb42a4c6ae1c8db6f50f9b?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"
},
...
...
}
我想索引和查询嵌入式子监视列表文档拥有的任何字段:
... "tags_array", "html_url", "description", "forks"
但我不希望 elasticsearch 包含父用户字段:
... "uid", "name", "email", "nickname"
这样当我查询“git 持久性”时,它会查看原始 MongoDB 的每个“用户”的每个“关注列表”索引字段。
(对不起,这里的单数和复数不匹配,我只是指出文档对象名称)