##// END OF EJS Templates
search: updated api and switched to desc:date as default
marcink -
r3965:27fda7c0 default
parent child Browse files
Show More
@@ -1,112 +1,118 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2011-2019 RhodeCode GmbH
3 # Copyright (C) 2011-2019 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21
21
22 import logging
22 import logging
23
23
24 from rhodecode.api import jsonrpc_method
24 from rhodecode.api import jsonrpc_method
25 from rhodecode.api.exc import JSONRPCValidationError
25 from rhodecode.api.exc import JSONRPCValidationError
26 from rhodecode.api.utils import Optional
26 from rhodecode.api.utils import Optional
27 from rhodecode.lib.index import searcher_from_config
27 from rhodecode.lib.index import searcher_from_config
28 from rhodecode.model import validation_schema
28 from rhodecode.model import validation_schema
29 from rhodecode.model.validation_schema.schemas import search_schema
29 from rhodecode.model.validation_schema.schemas import search_schema
30
30
31 log = logging.getLogger(__name__)
31 log = logging.getLogger(__name__)
32
32
33
33
34 @jsonrpc_method()
34 @jsonrpc_method()
35 def search(request, apiuser, search_query, search_type, page_limit=Optional(10),
35 def search(request, apiuser, search_query, search_type, page_limit=Optional(10),
36 page=Optional(1), search_sort=Optional('newfirst'),
36 page=Optional(1), search_sort=Optional('desc:date'),
37 repo_name=Optional(None), repo_group_name=Optional(None)):
37 repo_name=Optional(None), repo_group_name=Optional(None)):
38 """
38 """
39 Fetch Full Text Search results using API.
39 Fetch Full Text Search results using API.
40
40
41 :param apiuser: This is filled automatically from the |authtoken|.
41 :param apiuser: This is filled automatically from the |authtoken|.
42 :type apiuser: AuthUser
42 :type apiuser: AuthUser
43 :param search_query: Search query.
43 :param search_query: Search query.
44 :type search_query: str
44 :type search_query: str
45 :param search_type: Search type. The following are valid options:
45 :param search_type: Search type. The following are valid options:
46 * commit
46 * commit
47 * content
47 * content
48 * path
48 * path
49 :type search_type: str
49 :type search_type: str
50 :param page_limit: Page item limit, from 1 to 500. Default 10 items.
50 :param page_limit: Page item limit, from 1 to 500. Default 10 items.
51 :type page_limit: Optional(int)
51 :type page_limit: Optional(int)
52 :param page: Page number. Default first page.
52 :param page: Page number. Default first page.
53 :type page: Optional(int)
53 :type page: Optional(int)
54 :param search_sort: Search sort order. Default newfirst. The following are valid options:
54 :param search_sort: Search sort order.Must start with asc: or desc: Default desc:date.
55 * newfirst
55 The following are valid options:
56 * oldfirst
56 * asc|desc:message.raw
57 * asc|desc:date
58 * asc|desc:author.email.raw
59 * asc|desc:message.raw
60 * newfirst (old legacy equal to desc:date)
61 * oldfirst (old legacy equal to asc:date)
62
57 :type search_sort: Optional(str)
63 :type search_sort: Optional(str)
58 :param repo_name: Filter by one repo. Default is all.
64 :param repo_name: Filter by one repo. Default is all.
59 :type repo_name: Optional(str)
65 :type repo_name: Optional(str)
60 :param repo_group_name: Filter by one repo group. Default is all.
66 :param repo_group_name: Filter by one repo group. Default is all.
61 :type repo_group_name: Optional(str)
67 :type repo_group_name: Optional(str)
62 """
68 """
63
69
64 data = {'execution_time': ''}
70 data = {'execution_time': ''}
65 repo_name = Optional.extract(repo_name)
71 repo_name = Optional.extract(repo_name)
66 repo_group_name = Optional.extract(repo_group_name)
72 repo_group_name = Optional.extract(repo_group_name)
67
73
68 schema = search_schema.SearchParamsSchema()
74 schema = search_schema.SearchParamsSchema()
69
75
70 try:
76 try:
71 search_params = schema.deserialize(
77 search_params = schema.deserialize(
72 dict(search_query=search_query,
78 dict(search_query=search_query,
73 search_type=search_type,
79 search_type=search_type,
74 search_sort=Optional.extract(search_sort),
80 search_sort=Optional.extract(search_sort),
75 page_limit=Optional.extract(page_limit),
81 page_limit=Optional.extract(page_limit),
76 requested_page=Optional.extract(page))
82 requested_page=Optional.extract(page))
77 )
83 )
78 except validation_schema.Invalid as err:
84 except validation_schema.Invalid as err:
79 raise JSONRPCValidationError(colander_exc=err)
85 raise JSONRPCValidationError(colander_exc=err)
80
86
81 search_query = search_params.get('search_query')
87 search_query = search_params.get('search_query')
82 search_type = search_params.get('search_type')
88 search_type = search_params.get('search_type')
83 search_sort = search_params.get('search_sort')
89 search_sort = search_params.get('search_sort')
84
90
85 if search_params.get('search_query'):
91 if search_params.get('search_query'):
86 page_limit = search_params['page_limit']
92 page_limit = search_params['page_limit']
87 requested_page = search_params['requested_page']
93 requested_page = search_params['requested_page']
88
94
89 searcher = searcher_from_config(request.registry.settings)
95 searcher = searcher_from_config(request.registry.settings)
90
96
91 try:
97 try:
92 search_result = searcher.search(
98 search_result = searcher.search(
93 search_query, search_type, apiuser, repo_name, repo_group_name,
99 search_query, search_type, apiuser, repo_name, repo_group_name,
94 requested_page=requested_page, page_limit=page_limit, sort=search_sort)
100 requested_page=requested_page, page_limit=page_limit, sort=search_sort)
95
101
96 data.update(dict(
102 data.update(dict(
97 results=list(search_result['results']), page=requested_page,
103 results=list(search_result['results']), page=requested_page,
98 item_count=search_result['count'],
104 item_count=search_result['count'],
99 items_per_page=page_limit))
105 items_per_page=page_limit))
100 finally:
106 finally:
101 searcher.cleanup()
107 searcher.cleanup()
102
108
103 if not search_result['error']:
109 if not search_result['error']:
104 data['execution_time'] = '%s results (%.4f seconds)' % (
110 data['execution_time'] = '%s results (%.4f seconds)' % (
105 search_result['count'],
111 search_result['count'],
106 search_result['runtime'])
112 search_result['runtime'])
107 else:
113 else:
108 node = schema['search_query']
114 node = schema['search_query']
109 raise JSONRPCValidationError(
115 raise JSONRPCValidationError(
110 colander_exc=validation_schema.Invalid(node, search_result['error']))
116 colander_exc=validation_schema.Invalid(node, search_result['error']))
111
117
112 return data
118 return data
@@ -1,58 +1,58 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2016-2019 RhodeCode GmbH
3 # Copyright (C) 2016-2019 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21
21
22 import colander
22 import colander
23
23
24
24
25 def sort_validator(node, value):
25 def sort_validator(node, value):
26 if value in ['oldfirst', 'newfirst']:
26 if value in ['oldfirst', 'newfirst']:
27 return value
27 return value
28 if value.startswith('asc:'):
28 if value.startswith('asc:'):
29 return value
29 return value
30 if value.startswith('desc:'):
30 if value.startswith('desc:'):
31 return value
31 return value
32
32
33 msg = u'Invalid search sort, must be `oldfirst`, `newfirst`, or start with asc: or desc:'
33 msg = u'Invalid search sort, must be `oldfirst`, `newfirst`, or start with asc: or desc:'
34 raise colander.Invalid(node, msg)
34 raise colander.Invalid(node, msg)
35
35
36
36
37 class SearchParamsSchema(colander.MappingSchema):
37 class SearchParamsSchema(colander.MappingSchema):
38 search_query = colander.SchemaNode(
38 search_query = colander.SchemaNode(
39 colander.String(),
39 colander.String(),
40 missing='')
40 missing='')
41 search_type = colander.SchemaNode(
41 search_type = colander.SchemaNode(
42 colander.String(),
42 colander.String(),
43 missing='content',
43 missing='content',
44 validator=colander.OneOf(['content', 'path', 'commit', 'repository']))
44 validator=colander.OneOf(['content', 'path', 'commit', 'repository']))
45 search_sort = colander.SchemaNode(
45 search_sort = colander.SchemaNode(
46 colander.String(),
46 colander.String(),
47 missing='newfirst',
47 missing='desc:date',
48 validator=sort_validator)
48 validator=sort_validator)
49 search_max_lines = colander.SchemaNode(
49 search_max_lines = colander.SchemaNode(
50 colander.Integer(),
50 colander.Integer(),
51 missing=10)
51 missing=10)
52 page_limit = colander.SchemaNode(
52 page_limit = colander.SchemaNode(
53 colander.Integer(),
53 colander.Integer(),
54 missing=10,
54 missing=10,
55 validator=colander.Range(1, 500))
55 validator=colander.Range(1, 500))
56 requested_page = colander.SchemaNode(
56 requested_page = colander.SchemaNode(
57 colander.Integer(),
57 colander.Integer(),
58 missing=1)
58 missing=1)
General Comments 0
You need to be logged in to leave comments. Login now