mongodb / rails"例外:无法找到特殊索引:2d用于:"

时间:2012-11-05 17:21:05

标签: ruby-on-rails mongodb indexing 2d

我有一个rails应用程序,我有索引的一些问题。我按名称搜索位置。 首先我认为这是地址问题,但我不确定。

搜索控制器的相关部分:

  @practices = Practice.published
  @practices = @practices.where(:"addresses.country" => params[:country].upcase) if  params[:country].present?

  if params[:location].present? && latlng = get_coordinates
    @practices = @practices.near_sphere(:"addresses.coords" => latlng).max_distance(:"addresses.coords" => get_distance )
  end

  # now find doctors based on resulting practices
  @doctors = Doctor.published.in("organization_relations.practice_id" => @practices.distinct(:_id))

完整的崩溃日志:

Moped::Errors::OperationFailure (The operation: #<Moped::Protocol::Command
  @length=255
  @request_id=646
  @response_to=0
  @op_code=2004
  @flags=[]
  @full_collection_name="um-contacts.$cmd"
  @skip=0
  @limit=-1
  @selector={:distinct=>"practices", :key=>"_id", :query=>{"deleted_at"=>nil,     "published_at"=>{"$lte"=>2012-11-05 15:17:14 UTC}, "addresses.country"=>"DE", "addresses.coords"=>{"$nearSphere"=>[13.4060912, 52.519171], "$maxDistance"=>0.01569612305760477}}}
  @fields=nil>
failed with error 13038: "exception: can't find special index: 2d for: { deleted_at: null, published_at: { $lte: new Date(1352128634313) }, addresses.country: \"DE\", addresses.coords: { $nearSphere: [ 13.4060912, 52.519171 ], $maxDistance: 0.01569612305760477 } }"

See https://github.com/mongodb/mongo/blob/master/docs/errors.md
for details about this error.):
  app/controllers/search_controller.rb:16:in `index'

这是索引的结果,不知道如何从通过has_many嵌入的地址中查询它们。

> db.practices.getIndexes()

[

    {
    "v" : 1,
    "key" : {
        "_id" : 1
    },
    "ns" : "um-contacts.practices",
    "name" : "_id_"
}

]

非常感谢帮助!

编辑:看起来没有创建adresses.coords的索引,

db.system.indexes.find()
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "um-contacts.users", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "um-contacts.doctors", "name" : "_id_" }
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "um-contacts.collaborations", "name" : "_i
{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "um-contacts.practices", "name" : "_id_" }

但应该在练习课中创建:

class Practice
 ...
 embeds_many :addresses, cascade_callbacks: true, as: :addressable
 ...
 field :name, type: String
 field :kind, type: String
 field :slug, type: String

 index({"addresses.coords" => '2d'}, { min: -180, max: 180, background: true })
 index({name: 1})
 index({slug: 1}, { unique: true })
 ...

任何人都知道为什么会失败?

1 个答案:

答案 0 :(得分:0)

尝试重新创建索引。对于mongoid:

rake db:mongoid:create_indexes