我正在使用this库来实现具有全局化模型的弹性搜索。
我有一个有很多文章的作者类。现在我在作者类中有一个名为type_string的属性,我想用它搜索所有文章。这是我的模型目前的样子。
Articles.rb
require 'elasticsearch/model'
class Article < ActiveRecord::Base
translates :title, :text
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
include Elasticsearch::Model::Globalize::MultipleFields
index_name 'relation_test'
belongs_to :author
after_commit on: [:create] do
__elasticsearch__.index_document
end
after_commit on: [:update] do
__elasticsearch__.index_document
end
after_commit on: [:destroy] do
__elasticsearch__.delete_document
end
mapping _parent: { type: 'author' } do
indexes :id, type: 'integer'
indexes :title_de, analyzer: 'german'
indexes :title_en, analyzer: 'english'
indexes :text_de, analyzer: 'german'
indexes :text_en, analyzer: 'english'
end
def self.create_index!(options={})
client = Author.__elasticsearch__.client
client.indices.delete index: index_name rescue nil if options[:force]
settings = Author.settings.to_hash.merge Article.settings.to_hash
mappings = Author.mappings.to_hash.merge Article.mappings.to_hash
client.indices.create index: index_name,
body:
{
settings: settings.to_hash,
mappings: mappings.to_hash
}
end
after_commit lambda { __elasticsearch__.index_document(parent: author_id) }, on: :create
after_commit lambda { __elasticsearch__.update_document(parent: author_id) }, on: :update
after_commit lambda { __elasticsearch__.delete_document(parent: author_id) }, on: :destroy
def as_indexed_json(options={})
self.as_json({
only: [:title, :text],
include: {
author: { only: [:type_string] },
}
})
end
end
Article.import # for auto sync model with elastic search
Authors.rb
class Author < ActiveRecord::Base
translates :name, :type_string
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
include Elasticsearch::Model::Globalize::MultipleFields
mapping do
indexes :id, type: 'integer'
indexes :type_string_de, analyzer: 'german'
indexes :type_string_en, analyzer: 'english'
end
after_commit on: [:create] do
__elasticsearch__.index_document
end
after_commit on: [:update] do
__elasticsearch__.index_document
end
after_commit on: [:destroy] do
__elasticsearch__.delete_document
end
def as_indexed_json(options={})
{
author_id: author_id,
type_string_de: type_string_de,
type_string_en: type_string_en
}
end
end
这是我的种子.rb
I18n.locale = :de
auth1 = Author.create!({ name: "Author1" , type_string: "Tanzer"})
auth2 = Author.create!({ name: "Author2" , type_string: "Laufer"})
Article.create!({ title: "Coffee", text: "killer thing to drink", author: auth1 })
Article.create!({ title: "Tea", text: "don't drink this is shit", author: auth2 })
我得到以下基于文章对象的索引映射。
虽然它向我展示了作者对象的id,但我似乎无法弄清楚如何检索作者。
所以在最后我想要的是,如果我搜索文本“Tanzer”,它会给作者带来type_String = "Tanzer"
及其文章。
我也按照定义的here尝试了以下查询,但它给了我0个结果。
a = Article.search({query:{has_parent:{parent_type:"author",query:{ match:{ type_string:"Tanzer"}}}}})
a.results.total # <== this gives 0.
更新
当我删除行include Elasticsearch::Model::Globalize::MultipleFields
并使用所有属性作为其原始名称,不包括前面的_de and _en
。比它完美无缺。