[英]Elasticsearch with Tire on Rails bulk import & indexing issue
[英]Elasticsearch Rails indexing issue
我正在使用Elasticsearch和Elasticsearch Rails gem。 我有两个通过ActiveRecord关联的模型,我试图用Elasticsearch对其进行索引和搜索。
这是我的店铺模型
store.rb
has_many :addresses
has_many :jobs
def as_indexed_json
self.as_json(include: {
customer: { include: { addresses: {}, jobs: {} } },
vendors: {}
})
end
settings index: { number_of_shards: 1 } do
mapping dynamic: 'false' do
indexes :id
indexes :store_number
indexes :manager
indexes :customer do
indexes :first_name
indexes :addresses do
indexes :city
indexes :state
end
indexes :jobs do
indexes :job_number
end
end
end
end
这是我的地址模型:
def as_indexed_json
end
settings index: { number_of_shards: 1 } do
mapping dynamic: 'false' do
indexes :city
indexes :state
end
end
我还将Searchkit用作前端UI。 Searchkit能够汇总和显示商店模型中的所有属性(例如商店编号和经理)。 但是,它不能查看嵌套的项目。 换句话说,我无法汇总和检索客户下的作业下的job_numbers。 我可以查看客户名称,等等。我尝试使用类型:作业和所有其他对象旁边的“嵌套”,但这没有什么区别。 我也尝试过调整as_indexed_json,但也没有任何运气。 有人对此有任何想法吗? 我为此感到困惑。
尝试将您的as_indexed_json
方法更改为此:
def as_indexed_json() {
hash = self.as_json()
hash['jobs'] = self.jobs.map(&:job_number)
hash['address_state'] = self.addresses.map(&:state)
hash['address_city'] = self.addresses.map(&:city)
hash
}
像这样在您的store
模型中覆盖search
:
def self.search(query)
@search_definition = {
query: {}
}
unless query.blank?
@search_definition[:query] = {
bool: {
should: [
{
multi_match: {
query: query,
fields: ['store_number', 'manager', 'jobs', 'address_state', 'address_city'],
operator: 'and'
}
}
]
}
}
else
@search_definition[:query] = { match_all: {} }
end
__elasticsearch__.search(@search_definition)
end
您还需要更改映射设置,如下所示:
settings index: { number_of_shards: 1 } do
mapping do
indexes :store_number, analyzer: 'keyword'
indexes :jobs, analyzer: 'keyword'
indexes :address_state, analyzer: 'keyword'
indexes :address_city, analyzer: 'keyword'
indexes :manager, type: 'multi_field' do
indexes :manager, analyzer: 'snowball'
indexes :tokenized, analyzer: 'simple'
end
end
end
这些索引设置只是示例。 您可以使用最适合您的用例的不同types
, analyzers
和tokenizers
。
这样,当您通过商店的索引字段以及job_number
, city
和state
搜索商店时,将获得结果。尽管如果您想将关联用作过滤器,则搜索方法和映射将有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.