Search code examples
google-app-enginepython-2.7webapp2

Delete entity from index?


Am I doing this right? I'm trying to delete an entity from the index in this method, it's the first time I try so I don't know if it's working:

def get(self):
    timeline = datetime.now () - timedelta (days = 59)
    edge = datetime.now () - timedelta (days = 60)
    ads = Ad.all().filter("published =", True).filter("modified <", timeline).filter("url IN", ['www.koolbusiness.com']).filter("modified >", edge)
    for ad in ads:
        if ad.title:
            subject= ad.title
        else:
            subject = 'Reminder'
        message = mail.EmailMessage(sender='admin@koolbusiness.com', subject=subject)
        reminder = 'You can renew your advertisement'
        message.body = ('%s \nhttp://www.koolbusiness.com/vi/%d %s') % (reminder, ad.key().id(), '\nIf you like, you can also add us on Facebook \nhttp://apps.facebook.com/koolbusiness')
        message.to=ad.email
        message.bcc='fridge@koolbusiness.com'
        message.send()
        index = search.Index(name='ad',
                 consistency=Index.PER_DOCUMENT_CONSISTENT)
        index.remove(str(ad.key())

The above code is supposed to send a reminder for timed out advertisements and then remove then from the index. If the advertisement is renewed it can be added to the index again. Will it work?


Solution

  • You code should work, but IMHO it is best to mark the ad as expired instead of removing it from the index. This will save you the need to reindex renewed ads and will give you better audit on your ads.