0

我有一个 Rails 应用程序,其中一个主题可以被各种用户关注。我希望能够找到与名称匹配的主题,然后是特定用户。仅使用 Tirequery语句,没有filter.

为此,我打算使用带有嵌套查询的布尔查询

这是我的主题模型:

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

跟随模型包含

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吗?

4

1 回答 1

0

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

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

于 2013-05-21T20:00:31.673 回答