Show More
@@ -1,143 +1,143 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | """ |
|
3 | 3 | rhodecode.controllers.search |
|
4 | 4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 | 5 | |
|
6 |
Search controller for |
|
|
6 | Search controller for RhodeCode | |
|
7 | 7 | |
|
8 | 8 | :created_on: Aug 7, 2010 |
|
9 | 9 | :author: marcink |
|
10 | 10 | :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> |
|
11 | 11 | :license: GPLv3, see COPYING for more details. |
|
12 | 12 | """ |
|
13 | 13 | # This program is free software: you can redistribute it and/or modify |
|
14 | 14 | # it under the terms of the GNU General Public License as published by |
|
15 | 15 | # the Free Software Foundation, either version 3 of the License, or |
|
16 | 16 | # (at your option) any later version. |
|
17 | 17 | # |
|
18 | 18 | # This program is distributed in the hope that it will be useful, |
|
19 | 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20 | 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21 | 21 | # GNU General Public License for more details. |
|
22 | 22 | # |
|
23 | 23 | # You should have received a copy of the GNU General Public License |
|
24 | 24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25 | 25 | import logging |
|
26 | 26 | import traceback |
|
27 | 27 | |
|
28 | 28 | from pylons.i18n.translation import _ |
|
29 | 29 | from pylons import request, config, tmpl_context as c |
|
30 | 30 | |
|
31 | 31 | from rhodecode.lib.auth import LoginRequired |
|
32 | 32 | from rhodecode.lib.base import BaseController, render |
|
33 |
from rhodecode.lib.indexers import CHGSETS_SCHEMA, SCHEMA, CHGSET_IDX_NAME, |
|
|
33 | from rhodecode.lib.indexers import CHGSETS_SCHEMA, SCHEMA, CHGSET_IDX_NAME, \ | |
|
34 | IDX_NAME, WhooshResultWrapper | |
|
34 | 35 | |
|
35 | 36 | from webhelpers.paginate import Page |
|
36 | 37 | from webhelpers.util import update_params |
|
37 | 38 | |
|
38 | 39 | from whoosh.index import open_dir, EmptyIndexError |
|
39 | 40 | from whoosh.qparser import QueryParser, QueryParserError |
|
40 | 41 | from whoosh.query import Phrase, Wildcard, Term, Prefix |
|
41 | 42 | from rhodecode.model.repo import RepoModel |
|
42 | 43 | |
|
43 | 44 | log = logging.getLogger(__name__) |
|
44 | 45 | |
|
45 | 46 | |
|
46 | 47 | class SearchController(BaseController): |
|
47 | 48 | |
|
48 | 49 | @LoginRequired() |
|
49 | 50 | def __before__(self): |
|
50 | 51 | super(SearchController, self).__before__() |
|
51 | 52 | |
|
52 | 53 | def index(self, search_repo=None): |
|
53 | 54 | c.repo_name = search_repo |
|
54 | 55 | c.formated_results = [] |
|
55 | 56 | c.runtime = '' |
|
56 | 57 | c.cur_query = request.GET.get('q', None) |
|
57 | 58 | c.cur_type = request.GET.get('type', 'content') |
|
58 | 59 | c.cur_search = search_type = {'content': 'content', |
|
59 | 60 | 'commit': 'message', |
|
60 | 61 | 'path': 'path', |
|
61 |
'repository': 'repository' |
|
|
62 | .get(c.cur_type, 'content') | |
|
62 | 'repository': 'repository' | |
|
63 | }.get(c.cur_type, 'content') | |
|
63 | 64 | |
|
64 | 65 | index_name = { |
|
65 | 66 | 'content': IDX_NAME, |
|
66 | 67 | 'commit': CHGSET_IDX_NAME, |
|
67 |
'path': IDX_NAME |
|
|
68 |
|
|
|
68 | 'path': IDX_NAME | |
|
69 | }.get(c.cur_type, IDX_NAME) | |
|
69 | 70 | |
|
70 | 71 | schema_defn = { |
|
71 | 72 | 'content': SCHEMA, |
|
72 | 73 | 'commit': CHGSETS_SCHEMA, |
|
73 |
'path': SCHEMA |
|
|
74 |
|
|
|
74 | 'path': SCHEMA | |
|
75 | }.get(c.cur_type, SCHEMA) | |
|
75 | 76 | |
|
76 | 77 | log.debug('IDX: %s' % index_name) |
|
77 | 78 | log.debug('SCHEMA: %s' % schema_defn) |
|
78 | 79 | |
|
79 | 80 | if c.cur_query: |
|
80 | 81 | cur_query = c.cur_query.lower() |
|
81 | 82 | log.debug(cur_query) |
|
82 | 83 | |
|
83 | 84 | if c.cur_query: |
|
84 | 85 | p = int(request.params.get('page', 1)) |
|
85 | 86 | highlight_items = set() |
|
86 | 87 | try: |
|
87 | 88 | idx = open_dir(config['app_conf']['index_dir'], |
|
88 | 89 | indexname=index_name) |
|
89 | 90 | searcher = idx.searcher() |
|
90 | 91 | |
|
91 | 92 | qp = QueryParser(search_type, schema=schema_defn) |
|
92 | 93 | if c.repo_name: |
|
93 | 94 | cur_query = u'repository:%s %s' % (c.repo_name, cur_query) |
|
94 | 95 | try: |
|
95 | 96 | query = qp.parse(unicode(cur_query)) |
|
96 | 97 | # extract words for highlight |
|
97 | 98 | if isinstance(query, Phrase): |
|
98 | 99 | highlight_items.update(query.words) |
|
99 | 100 | elif isinstance(query, Prefix): |
|
100 | 101 | highlight_items.add(query.text) |
|
101 | 102 | else: |
|
102 | 103 | for i in query.all_terms(): |
|
103 | 104 | if i[0] in ['content', 'message']: |
|
104 | 105 | highlight_items.add(i[1]) |
|
105 | 106 | |
|
106 | 107 | matcher = query.matcher(searcher) |
|
107 | 108 | |
|
108 | 109 | log.debug('query: %s' % query) |
|
109 | 110 | log.debug('hl terms: %s' % highlight_items) |
|
110 | 111 | results = searcher.search(query) |
|
111 | 112 | res_ln = len(results) |
|
112 | 113 | c.runtime = '%s results (%.3f seconds)' % ( |
|
113 | 114 | res_ln, results.runtime |
|
114 | 115 | ) |
|
115 | 116 | |
|
116 | 117 | def url_generator(**kw): |
|
117 | 118 | return update_params("?q=%s&type=%s" \ |
|
118 | 119 | % (c.cur_query, c.cur_type), **kw) |
|
119 | 120 | repo_location = RepoModel().repos_path |
|
120 | 121 | c.formated_results = Page( |
|
121 | 122 | WhooshResultWrapper(search_type, searcher, matcher, |
|
122 | 123 | highlight_items, repo_location), |
|
123 | 124 | page=p, |
|
124 | 125 | item_count=res_ln, |
|
125 | 126 | items_per_page=10, |
|
126 | 127 | url=url_generator |
|
127 | 128 | ) |
|
128 | 129 | |
|
129 | 130 | except QueryParserError: |
|
130 | 131 | c.runtime = _('Invalid search query. Try quoting it.') |
|
131 | 132 | searcher.close() |
|
132 | 133 | except (EmptyIndexError, IOError): |
|
133 | 134 | log.error(traceback.format_exc()) |
|
134 | 135 | log.error('Empty Index data') |
|
135 | 136 | c.runtime = _('There is no index to search in. ' |
|
136 | 137 | 'Please run whoosh indexer') |
|
137 | 138 | except (Exception): |
|
138 | 139 | log.error(traceback.format_exc()) |
|
139 | 140 | c.runtime = _('An error occurred during this search operation') |
|
140 | 141 | |
|
141 | ||
|
142 | 142 | # Return a rendered template |
|
143 | 143 | return render('/search/search.html') |
General Comments 0
You need to be logged in to leave comments.
Login now