Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(index_from_mongo) index collections with string id's #2397

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions superdesk/commands/index_from_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ class IndexFromMongo(superdesk.Command):
superdesk.Option("--all", action="store_true", dest="all_collections"),
superdesk.Option("--page-size", "-p"),
superdesk.Option("--last-id"),
superdesk.Option("--string-id", dest="string_id", action="store_true", help="Treat the id's as strings"),
]
default_page_size = 500

def run(self, collection_name, all_collections, page_size, last_id):
def run(self, collection_name, all_collections, page_size, last_id, string_id):
if not collection_name and not all_collections:
raise SystemExit("Specify --all to index from all collections")
elif all_collections:
Expand All @@ -50,11 +51,11 @@ def run(self, collection_name, all_collections, page_size, last_id):
for resource in resources:
self.copy_resource(resource, page_size)
else:
self.copy_resource(collection_name, page_size, last_id)
self.copy_resource(collection_name, page_size, last_id, string_id)

@classmethod
def copy_resource(cls, resource, page_size, last_id=None):
for items in cls.get_mongo_items(resource, page_size, last_id):
def copy_resource(cls, resource, page_size, last_id=None, string_id=False):
for items in cls.get_mongo_items(resource, page_size, last_id, string_id):
print("{} Inserting {} items".format(time.strftime("%X %x %Z"), len(items)))
s = time.time()
success, failed = 0, 0
Expand All @@ -78,7 +79,7 @@ def copy_resource(cls, resource, page_size, last_id=None):
return "Finished indexing collection {}".format(resource)

@classmethod
def get_mongo_items(cls, mongo_collection_name, page_size, last_id):
def get_mongo_items(cls, mongo_collection_name, page_size, last_id, string_id):
"""Generate list of items from given mongo collection per page size.

:param mongo_collection_name: Name of the collection to get the items
Expand All @@ -93,10 +94,11 @@ def get_mongo_items(cls, mongo_collection_name, page_size, last_id):

while True:
if last_id:
try:
last_id = ObjectId(last_id)
except Exception:
pass
if not string_id:
try:
last_id = ObjectId(last_id)
except Exception:
pass
args.update({"filter": {config.ID_FIELD: {"$gt": last_id}}})

cursor = db.find(**args)
Expand Down