如何定义嵌套在ActiveRecord映射块中的类型的Tire属性?

时间:2013-04-16 20:10:15

标签: ruby-on-rails elasticsearch tire

我有一个Rails应用程序,其中各个用户可以遵循主题。我希望能够找到与名称匹配的主题,然后是特定用户。仅使用轮胎query语句,而不使用filter

为此,我打算使用a boolean query with a nested query

这是我的主题模型:

class Topic < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks

  mapping do
    indexes :name
    indexes :follower_ids, type: 'nested', as: "indexed_follower_ids", id: 'integer'
  end
  def indexed_follower_ids
    followers.pluck(:id).map { |id| {id: id} }.to_json
  end

  attr_accessible :name

end

Follow模型包含

after_save :update_topic_index
def update_topic_index
  topic.tire.update_index
end

不幸的是,在使用几条记录为我的数据库播种后,没有任何内容被编入索引。例如,如果我创建以下记录:

User.create! name: "user0"
Topic.create! name: "topic0"
Follow.create! user: User.first, topic: Topic.first

我在ElasticSearch服务器日志中看到以下错误:

[2013-04-16 15:07:28,564][INFO ][cluster.metadata         ] [Sligguth] [topics] creating index, cause [api], shards [5]/[1], mappings [topic]
[2013-04-16 15:07:28,690][DEBUG][action.index             ] [Sligguth] [topics][2], node[UdT6jTecRNuzdRkxkX2uUA], [P], s[STARTED]: Failed to execute [index {[topics][topic][1], source[{"name":"topic0","follower_ids":"[]"}]}]
org.elasticsearch.index.mapper.MapperParsingException: object mapping for [topic] tried to parse as object, but got EOF, has a concrete value been provided to it?
at org.elasticsearch.index.mapper.object.ObjectMapper.parse(ObjectMapper.java:457)
at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:494)
at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:438)
at org.elasticsearch.index.shard.service.InternalIndexShard.prepareIndex(InternalIndexShard.java:308)
at org.elasticsearch.action.index.TransportIndexAction.shardOperationOnPrimary(TransportIndexAction.java:202)
at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.performOnPrimary(TransportShardReplicationOperationAction.java:532)
at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$1.run(TransportShardReplicationOperationAction.java:430)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
[2013-04-16 15:07:28,709][DEBUG][action.index             ] [Sligguth] [topics][2], node[UdT6jTecRNuzdRkxkX2uUA], [P], s[STARTED]: Failed to execute [index {[topics][topic][1], source[{"name":"topic0","follower_ids":"[{\"key_id\":1}]"}]}]
org.elasticsearch.index.mapper.MapperParsingException: object mapping for [topic] tried to parse as object, but got EOF, has a concrete value been provided to it?
at org.elasticsearch.index.mapper.object.ObjectMapper.parse(ObjectMapper.java:457)
at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:494)
at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:438)
at org.elasticsearch.index.shard.service.InternalIndexShard.prepareIndex(InternalIndexShard.java:308)
at org.elasticsearch.action.index.TransportIndexAction.shardOperationOnPrimary(TransportIndexAction.java:202)
at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.performOnPrimary(TransportShardReplicationOperationAction.java:532)
at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$1.run(TransportShardReplicationOperationAction.java:430)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)

知道为什么ElasticSearch在索引我的嵌套follower_ids时失败了吗?

1 个答案:

答案 0 :(得分:0)

我想象一下,因为你没有在那里存储一个实际的嵌套对象,你需要存储一个数组。只需将follower_ids的类型更改为字符串,您就可以了。

编辑:刚看到上面的线程,在查询follower_ids时,使用过滤器而不是在布尔值中指定术语查询,看看它是如何工作的