<commit_before>import os.path
from flask.ext.sqlalchemy import models_committed
from whoosh import fields, index
from whoosh.qparser import QueryParser
from whoosh.analysis import StemmingAnalyzer

from hasjob import models, app

search_schema = fields.Schema(title=fields.TEXT(stored=True),
                              content=fields.TEXT(analyzer=StemmingAnalyzer()),
                              idref=fields.ID(stored=True, unique=True))

# For search results
def ob_from_idref(idref):
    ref, objid = idref.split('/')
    if ref == 'post':
        return models.JobPost.query.get(int(objid))
    elif ref == 'type':
        return models.JobType.query.get(int(objid))
    elif ref == 'category':
        return models.JobCategory.query.get(int(objid))


def do_search(query, expand=False):
    """
    Returns search results
    """
    ix = index.open_dir(app.config['SEARCH_INDEX_PATH'])
    hits = []

    if query:
        parser = QueryParser("content", schema=ix.schema)
        try:
            qry = parser.parse(query)
        except:
            # query couldn't be parsed
            qry = None
        if qry is not None:
            searcher = ix.searcher()
            hits = searcher.search(qry, limit=1000)

    if expand:
        return (ob_from_idref(hit['idref']) for hit in hits)
    else:
        return hits


def on_models_committed(sender, changes):
    index_required = False
    for model, change in changes:
        if isinstance(model, (models.JobType, models.JobCategory, models.JobPost)):
            index_required = True
            break
    if index_required:
        ix = index.open_dir(app.config['SEARCH_INDEX_PATH'])
        writer = ix.writer()
        for model, change in changes:<commit_msg>Catch the whoosh exception. Fixes #22<commit_after>import os.path
from flask.ext.sqlalchemy import models_committed
import time
from whoosh import fields, index
from whoosh.qparser import QueryParser
from whoosh.analysis import StemmingAnalyzer

from hasjob import models, app

search_schema = fields.Schema(title=fields.TEXT(stored=True),
                              content=fields.TEXT(analyzer=StemmingAnalyzer()),
                              idref=fields.ID(stored=True, unique=True))

# For search results
def ob_from_idref(idref):
    ref, objid = idref.split('/')
    if ref == 'post':
        return models.JobPost.query.get(int(objid))
    elif ref == 'type':
        return models.JobType.query.get(int(objid))
    elif ref == 'category':
        return models.JobCategory.query.get(int(objid))


def do_search(query, expand=False):
    """
    Returns search results
    """
    ix = index.open_dir(app.config['SEARCH_INDEX_PATH'])
    hits = []

    if query:
        parser = QueryParser("content", schema=ix.schema)
        try:
            qry = parser.parse(query)
        except:
            # query couldn't be parsed
            qry = None
        if qry is not None:
            searcher = ix.searcher()
            hits = searcher.search(qry, limit=1000)

    if expand:
        return (ob_from_idref(hit['idref']) for hit in hits)
    else:
        return hits


def on_models_committed(sender, changes):
    index_required = False
    for model, change in changes:
        if isinstance(model, (models.JobType, models.JobCategory, models.JobPost)):
            index_required = True
            break
    if index_required:
        ix = index.open_dir(app.config['SEARCH_INDEX_PATH'])
        try:
            writer = ix.writer()
        except:
            time.sleep(1)
            writer = ix.writer()
        for model, change in changes:
{'fraction_non_alphanumeric': 0.10271317829457365, 'fraction_numerical': 0.003045404208194906, 'mean_word_length': 2.43767840152236, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3550667', 'n_tokens_mistral': 1086, 'n_tokens_neox': 1065, 'n_words': 263}