import datetime from elasticsearch import Elasticsearch INDEX = 'feed' today = datetime.date.today() es = Elasticsearch() if es.indices.exists(INDEX): es.indices.delete(INDEX) # Create index. es.indices.create(INDEX, { 'settings': { 'index': { 'number_of_replicas': 0 } } }) # Populate some test data. # # Let's make up 3 content types. Let's assume a blog type app that combines # blog posts, flickr photos, and twitter posts. # Blog posts for i in range(1, 31, 4): # 30 days back, a blog post ea 4 days. body = { 'created': today - datetime.timedelta(days=i), 'title': 'Blog %s' % i, 'content': 'foo', } es.index(INDEX, 'blog', body=body) # Photos for i in range(1, 31, 2): # 30 days back, a photo ea 2 days. body = { 'created': today - datetime.timedelta(days=i), 'img': '/some/url/to/image/%s' % i, 'caption': 'foo', } es.index(INDEX, 'photo', body=body) # Tweets for i in range(1, 31): # 30 days back, a tweet erryday. body = { 'created': today - datetime.timedelta(days=i), 'tweet': 'tweet %s' % i, } es.index(INDEX, 'tweet', body=body) # Refresh index. es.indices.refresh(INDEX) # Get the feed, 25 items. res = es.search(INDEX, body={ 'query' : { 'match_all': {} }, 'sort': { 'created': {'order': 'desc'} }, # If things were given regions to appear in, we could do a region filter # in here too. 'size': 25 }) print 'Feed Items:' for obj in res['hits']['hits']: print "Doctype: %s" % obj['_type'] print "Source: %s" % obj['_source'] print '-' * 72