(size, remove=False)
Builds search index
Builds search index
Builds search index
def build(size, remove=False):
    """
    Builds search index
    """

    # Get top level posts that have not been indexed.
    posts = Post.objects.valid_posts(indexed=False, is_toplevel=True).exclude(root=None)[:size]
    target_count = len(posts)

    # The list of posts to update
    ids = [post.id for post in posts]

    # Add post to search index.
    search.index_posts(posts=posts, overwrite=remove)

    # Set the indexed field to true.
    Post.objects.filter(id__in=ids).update(indexed=True)

    count = Post.objects.valid_posts(indexed=False, is_toplevel=True).exclude(root=None).count()

    logger.info(f"Indexed {target_count} posts, {count} unindexed posts remaining")

    # Take spam posts that have been indexed and remove.
    spam_posts = Post.objects.filter(spam=Post.SPAM, indexed=False)[:size]
    sids = [post.id for post in spam_posts]

    for post in spam_posts:
        # Remove spam from search index.
        search.remove_post(post=post)

    # Update the spam indexed flag.
    Post.objects.filter(id__in=sids).update(indexed=True)

    # Add to spam index
    logger.info(f"Removed {len(sids)} spam posts from index")
def build ( size , remove = False ) : # Get top level posts that have not been indexed. posts = Post . objects . valid_posts ( indexed = False , is_toplevel = True ) . exclude ( root = None ) [ : size ] target_count = len ( posts ) # The list of posts to update ids = [ post . id for post in posts ] # Add post to search index. search . index_posts ( posts = posts , overwrite = remove ) # Set the indexed field to true. Post . objects . filter ( id__in = ids ) . update ( indexed = True ) count = Post . objects . valid_posts ( indexed = False , is_toplevel = True ) . exclude ( root = None ) . count ( ) logger . info ( f"Indexed {target_count} posts, {count} unindexed posts remaining" ) # Take spam posts that have been indexed and remove. spam_posts = Post . objects . filter ( spam = Post . SPAM , indexed = False ) [ : size ] sids = [ post . id for post in spam_posts ] for post in spam_posts : # Remove spam from search index. search . remove_post ( post = post ) # Update the spam indexed flag. Post . objects . filter ( id__in = sids ) . update ( indexed = True ) # Add to spam index logger . info ( f"Removed {len(sids)} spam posts from index" )