elasticsearch,tire,Activerecord,elasticsearch,Tire" /> elasticsearch,tire,Activerecord,elasticsearch,Tire" />

使用ActiveRecord的Elasticsearch、Tire和嵌套字段

使用ActiveRecord的Elasticsearch、Tire和嵌套字段,activerecord,elasticsearch,tire,Activerecord,elasticsearch,Tire,请阅读下面我对类似问题的回答 假设我在上述问题中提供的图书模型中添加了以下字段: field:account\u标记,类型:Array#[{:account\u id=>1,:tags=>[“tag1”,“tag2”],:account\u id=>2,:tags=>[“tag3”,“tag4”]}] 我现在想建立一个索引来搜索这些帐户ID或标签 我该怎么做呢?我已经解决了这个问题,而且是直截了当的。我的映射看起来像 mapping do ... indexes :account_

请阅读下面我对类似问题的回答

假设我在上述问题中提供的图书模型中添加了以下字段:

field:account\u标记,类型:Array#[{:account\u id=>1,:tags=>[“tag1”,“tag2”],:account\u id=>2,:tags=>[“tag3”,“tag4”]}]

我现在想建立一个索引来搜索这些帐户ID或标签


我该怎么做呢?

我已经解决了这个问题,而且是直截了当的。我的映射看起来像

mapping do 
  ...  
  indexes :account_tags do
    indexes :account_id, :index => "not_analyzed"
    indexes :tags, :type => 'string', :analyzer => 'keyword'
  end
end
def to_indexed_json
  {
    ...
    account_tags: account_tags
  }.to_json
end
我的to_indexed_json看起来像

mapping do 
  ...  
  indexes :account_tags do
    indexes :account_id, :index => "not_analyzed"
    indexes :tags, :type => 'string', :analyzer => 'keyword'
  end
end
def to_indexed_json
  {
    ...
    account_tags: account_tags
  }.to_json
end
现在我可以通过

@query = "account_id:#{account_id}"
@tags = tags

Book.tire.search :load => true do |search|
  search.query do |query|
    query.string @query
  end

  search.filter :terms, :tags => @tags
end
这是一个赤裸裸的解决方案,我用来制作一个系统的原型