mongoid_fulltext icon indicating copy to clipboard operation
mongoid_fulltext copied to clipboard

Creating indexes from the console with an empty DB crashes with "ns does not exist"

Open Startouf opened this issue 3 years ago • 4 comments

Context : starting a mongoid container/pod on kubernetes and trying to seed the database with

::Mongoid::Tasks::Database.create_indexes
# or rails db:create_indexes

I am not sure whether this is due to a Mongoid update or something else I am missing. Backtrace

rails aborted!
Mongo::Error::OperationFailure: ns does not exist: my_db_production.mongoid_fulltext.index_collection_name_0 (26) (on mongodb.staging.svc.cluster.local, modern retry, attempt 1)
/usr/local/bundle/gems/mongo-2.14.0/lib/mongo/operation/result.rb:343:in `raise_operation_failure'
/usr/local/bundle/gems/mongo-2.14.0/lib/mongo/operation/indexes/result.rb:86:in `validate!'
/usr/local/bundle/gems/mongo-2.14.0/lib/mongo/operation/shared/response_handling.rb:29:in `block (3 levels) in validate_result'
/usr/local/bundle/gems/mongo-2.14.0/lib/mongo/operation/shared/response_handling.rb:96:in `add_server_diagnostics'
/usr/local/bundle/gems/mongo-2.14.0/lib/mongo/operation/shared/response_handling.rb:28:in `block (2 levels) in validate_result'
/usr/local/bundle/gems/mongo-2.14.0/lib/mongo/operation/shared/response_handling.rb:43:in `add_error_labels'
/usr/local/bundle/gems/mongo-2.14.0/lib/mongo/operation/shared/response_handling.rb:27:in `block in validate_result'
/usr/local/bundle/gems/mongo-2.14.0/lib/mongo/operation/shared/response_handling.rb:82:in `unpin_maybe'
/usr/local/bundle/gems/mongo-2.14.0/lib/mongo/operation/shared/response_handling.rb:26:in `validate_result'
/usr/local/bundle/gems/mongo-2.14.0/lib/mongo/operation/shared/executable.rb:46:in `block in execute'
/usr/local/bundle/gems/mongo-2.14.0/lib/mongo/operation/shared/executable.rb:45:in `tap'
/usr/local/bundle/gems/mongo-2.14.0/lib/mongo/operation/shared/executable.rb:45:in `execute'
/usr/local/bundle/gems/mongo-2.14.0/lib/mongo/operation/shared/op_msg_or_list_indexes_command.rb:29:in `block 

The faulty lines of code (School collection is empty)

School.all.each(&:save)
::School.create_indexes

Startouf avatar Aug 02 '21 13:08 Startouf

same here

selivandex avatar Oct 01 '21 10:10 selivandex

@Startouf did you found the solution?

selivandex avatar Nov 22 '21 13:11 selivandex

No we haven't really found a solution yet. We just ensure there is something in the collection before running create_indexes.

For example, we had some mongoid migration code where we had to add a guard clause return if MyCollection.empty? in the self.up method to avoid problems in test apps with an empty database Screenshot 2021-11-22 at 15 01 56

Startouf avatar Nov 22 '21 14:11 Startouf

No we haven't really found a solution yet. We just ensure there is something in the collection before running create_indexes.

For example, we had some mongoid migration code where we had to add a guard clause return if MyCollection.empty? in the self.up method to avoid problems in test apps with an empty database

I just realised that starting from mongoid 4 there is a built-in full text search and you just need to create index:

index searchable_field: 'text'

then search: Model.text_search('query')

selivandex avatar Nov 22 '21 14:11 selivandex