Skip to content

Commit

Permalink
improve reindex on upgrade-step to reindex only contents with some bl…
Browse files Browse the repository at this point in the history
…ocks
  • Loading branch information
cekk committed Oct 17, 2024
1 parent 80911b4 commit d4ffdc2
Showing 1 changed file with 57 additions and 3 deletions.
60 changes: 57 additions & 3 deletions src/redturtle/volto/upgrades.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,17 +526,71 @@ def to_4307(context):


def to_4308(context):

def should_reindex(blocks):

reindexable_blocks = [
"accordion",
"alert",
"testo_riquadro_semplice",
"testo_riquadro_immagine",
"callout_block",
"hero",
"cta_block",
"gridBlock",
"slateTable",
"contacts",
"iconBlocks",
"numbersBlock",
"remote-counter",
"count_down",
]

for block in blocks.values():
if block.get("@type", "") in reindexable_blocks:
return True
return False

catalog = api.portal.get_tool(name="portal_catalog")
brains = catalog()
tot = len(brains)
logger.info(f"Reindexing {tot} items.")
logger.info(f"Analyzing {tot} items.")
reindexed = []
i = 0
for brain in brains:
i += 1
obj = brain.getObject()
obj = aq_base(brain.getObject())
reindex = False
if i % 100 == 0:
logger.info(f"Progress: {i}/{tot}")
obj.reindexObject(idxs=["SearchableText"], update_metadata=True)

if should_reindex(blocks=getattr(obj, "blocks", {})):
reindex = True
for schema in iterSchemata(obj):
for name, field in getFields(schema).items():
if name == "blocks":
continue
if not HAS_BLOCKSFIELD:
# blocks are only in blocks field
continue
if not isinstance(field, BlocksField):
continue
value = field.get(obj)
try:
blocks = value.get("blocks", {})
if should_reindex(blocks):
reindex = True
break
except AttributeError:
logger.warning(
f"[RICHTEXT] - {brain.getURL()} (should not reindexed)"
)
if reindex:
obj.reindexObject(idxs=["SearchableText"], update_metadata=True)
reindexed.append(brain.getURL())
if i % 1000 == 0:
transaction.commit()
logger.info(f"{i} items processed. Commit.")
logger.info(f"Reindex complete. Reindexed {len(reindexed)} contents:")
for url in reindexed:
logger.info(f"- {url}")

0 comments on commit d4ffdc2

Please sign in to comment.