【问题标题】:How to define a Tire property of type nested in a ActiveRecord mapping block?如何定义嵌套在 ActiveRecord 映射块中的类型的轮胎属性?
【发布时间】:2013-05-22 04:07:20
【问题描述】:

我有一个 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 吗?

【问题讨论】:

  • 查看 Follow.create! user: User.first, topic: Topic.first 的代码 sn-p ,您似乎在引用 User 类,而不是对象实例。你应该说u0 = User.create! name: "user0"; t0 = Topic.create! name: "topic0"; Follow.create! user: u0, topic: t0;——还是类似的东西?
  • 不,User.first 返回一个 User 对象实例,所以没关系
  • 猜猜我不太了解泰尔,无法真正提供帮助。您是否设法让同一台服务器为非嵌套文档或通过 _parent 属性相关的单个文档建立索引?
  • 你可以把它存储为一个数组,因为你只想把 id 放在那里,对吧?
  • Yeggeps:这就是我最初所做的,但我无法查询数组。我曾尝试使用indexes :follower_ids, type: 'string', as: "indexed_follower_ids"def indexed_follower_ids; followers.pluck :id; end,但must { terms :indexed_follower_ids, [follower_id]} 将只返回具有单个关注者的匹配文档。例如,使用follower_id = 1 查询时,未找到在indexed_follower_ids 中具有[1, 2] 的文档,而在 indexed_follower_ids 中找到具有[1] 的文档。也许我在尝试对数组进行索引和查询时做错了什么?

标签: ruby-on-rails elasticsearch tire


【解决方案1】:

我想这是因为您没有在其中存储实际的嵌套对象,而是在存储一个数组。只需将 follower_ids 的类型更改为 string 就可以了。

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

【讨论】:

    猜你喜欢
    • 2013-07-21
    • 2021-09-16
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多