Switch to unified view

a/Allura/allura/lib/search.py b/Allura/allura/lib/search.py
1
import re
1
import re
2
import socket
2
import cPickle as pickle
3
import cPickle as pickle
3
from logging import getLogger
4
from logging import getLogger
4
from pprint import pformat
5
from pprint import pformat
5
from itertools import islice, chain
6
from itertools import islice, chain
6
7
7
import markdown
8
import markdown
8
from pylons import c,g
9
from pylons import c,g
9
import pysolr
10
from pysolr import SolrError
10
11
11
from . import helpers as h
12
from . import helpers as h
12
from .markdown_extensions import ForgeExtension
13
from .markdown_extensions import ForgeExtension
13
14
14
log = getLogger(__name__)
15
log = getLogger(__name__)
...
...
24
        # log.exception('Indexing empty text: %s', doc)
25
        # log.exception('Indexing empty text: %s', doc)
25
        text = pformat(doc.values())
26
        text = pformat(doc.values())
26
    doc['text'] = text
27
    doc['text'] = text
27
    return doc
28
    return doc
28
29
30
class SearchError(SolrError):
31
    pass
32
29
def search(q,short_timeout=False,**kw):
33
def search(q,short_timeout=False,ignore_errors=True,**kw):
30
    try:
34
    try:
31
        if short_timeout:
35
        if short_timeout:
32
            return g.solr_short_timeout.search(q, **kw)
36
            return g.solr_short_timeout.search(q, **kw)
33
        else:
37
        else:
34
            return g.solr.search(q, **kw)
38
            return g.solr.search(q, **kw)
35
    except pysolr.SolrError as e:
39
    except (SolrError, socket.error) as e:
36
        log.exception('Error in solr indexing')
40
        log.exception('Error in solr indexing')
41
        if not ignore_errors:
42
            match = re.search(r'<pre>(.*)</pre>', str(e))
43
            raise SearchError('Error running search query: %s' % (match.group(1) if match else e))
37
44
38
def search_artifact(atype, q, history=False, rows=10, short_timeout=False, **kw):
45
def search_artifact(atype, q, history=False, rows=10, short_timeout=False, **kw):
39
    """Performs SOLR search.
46
    """Performs SOLR search.
40
47
41
    Raises ValueError if SOLR returns an error.
48
    Raises ValueError if SOLR returns an error.
...
...
50
        'type_s:%s' % fields['type_s'],
57
        'type_s:%s' % fields['type_s'],
51
        'project_id_s:%s' % c.project._id,
58
        'project_id_s:%s' % c.project._id,
52
        'mount_point_s:%s' % c.app.config.options.mount_point ]
59
        'mount_point_s:%s' % c.app.config.options.mount_point ]
53
    if not history:
60
    if not history:
54
        fq.append('is_history_b:False')
61
        fq.append('is_history_b:False')
55
    try:
62
    return search(q, fq=fq, rows=rows, short_timeout=short_timeout, ignore_errors=False, **kw)
56
        if short_timeout:
57
            return g.solr_short_timeout.search(q, fq=fq, rows=rows, **kw)
58
        else:
59
            return g.solr.search(q, fq=fq, rows=rows, **kw)
60
    except pysolr.SolrError, e:
61
        raise ValueError('Error running search query: %s' % e.message)
62
63
63
def find_shortlinks(text):
64
def find_shortlinks(text):
64
    md = markdown.Markdown(
65
    md = markdown.Markdown(
65
        extensions=['codehilite', ForgeExtension(), 'tables'],
66
        extensions=['codehilite', ForgeExtension(), 'tables'],
66
        output_format='html4')
67
        output_format='html4')