from django.contrib.search.backends import LuceneIndexer
from pressblog.libs.log import Log
from pressblog.blogs.models import Article
import settings

PROJECT_VAR_PATH=settings.PROJECT_VAR_PATH
def getArticle(i):
    articles=Article.objects.filter(id=i)
    if len(articles)<>1 :
        return None
    article=articles[0]
    return article

from threading import Thread

class testit(Thread):
    def __init__ (self,param):
        Thread.__init__(self)
        self.param = param
        self.status = -1
    def run(self):
        print "start"
        print '1'
        indexer = LuceneIndexer(PROJECT_VAR_PATH+'/indexs/article-index',
                                Article,
                                fields=['Article.description'],
                                attributes={'id': 'Article.id'}
                                )
        print '2'
        indexer.update()
        print '3'
        res=indexer.search(self.param)._hits
        resids=[]
        for hit in res:
            resids.append(int(hit.get('id')))
        print '4'
        articles=[]
        for i in resids:
            a=getArticle(i)
            if a:
                articles.append(a)
        print '5'
        print articles
        print "end"


current = testit("ok")
current.start()
current.join()

