Merge lp:~rharding/charmworld/apply-cowboys into lp:charmworld

Proposed by Richard Harding
Status: Merged
Approved by: Richard Harding
Approved revision: 490
Merged at revision: 494
Proposed branch: lp:~rharding/charmworld/apply-cowboys
Merge into: lp:charmworld
Diff against target: 114 lines (+11/-24)
4 files modified
charmworld/search.py (+8/-11)
charmworld/tests/test_search.py (+0/-9)
charmworld/views/api/__init__.py (+1/-3)
charmworld/views/search.py (+2/-1)
To merge this branch: bzr merge lp:~rharding/charmworld/apply-cowboys
Reviewer Review Type Date Requested Status
Juju Gui Bot continuous-integration Approve
Richard Harding Approve
Benji York (community) code Approve
Review via email: mp+211517@code.launchpad.net

Commit message

Update scoring to be limited on ES side.

- Drive-by to make sure we log the exception from ES for debugging purposes.

Description of the change

Update scoring to be limited on ES side.

- Drive-by to make sure we log the exception from ES for debugging purposes.

To post a comment you must log in.
Revision history for this message
Benji York (benji) wrote :

There is a nice alternative to log.error(str(exc)) in the logging
package: log.exception()

If the logging is important, we should have tests to ensure it keeps
working.

If the default value for min_score were set to MIN_ES_SCORE then we
wouldn't need the inline-if expression to set if no value was provided.

review: Approve (code)
Revision history for this message
Richard Harding (rharding) wrote :

> There is a nice alternative to log.error(str(exc)) in the logging
> package: log.exception()

Thanks, will update.

> If the default value for min_score were set to MIN_ES_SCORE then we
> wouldn't need the inline-if expression to set if no value was provided.

I originally did this, but the caller would end up passing in a None value which would reset the default. So I did this instead to make sure you can adjust the min score, but you can't leave it None if you're not fetching all explicitly.

490. By Richard Harding

Update per review

Revision history for this message
Richard Harding (rharding) :
review: Approve
Revision history for this message
Juju Gui Bot (juju-gui-bot) :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmworld/search.py'
2--- charmworld/search.py 2014-03-12 19:22:56 +0000
3+++ charmworld/search.py 2014-03-18 15:21:03 +0000
4@@ -20,6 +20,8 @@
5 Timer,
6 )
7
8+MIN_ES_SCORE = 0.5
9+
10 # Where wildcards are used, the final var name must be used, to avoid
11 # selecting any fields that may be integers, such as config.options.*.default,
12 # provides.*.interface.limit, or changes.revno. Selecting an integer field
13@@ -553,7 +555,7 @@
14 def api_search(self, text='', filters=None, type_=None,
15 limit=None, valid_only=True, sort=None,
16 autocomplete=False, doctype=CHARM,
17- min_score=1.0):
18+ min_score=None):
19 """Search charms and bundles (as used by the API).
20
21 :param text: A string to search for -- will be analyzed (i.e. split
22@@ -606,7 +608,12 @@
23 elif sort is not None:
24 raise ValueError('Invalid sort: %s.' % sort)
25
26+ # Make sure we always get scores back from ES.
27+ dsl['track_scores'] = True
28+
29 if not all_requested:
30+ min_score = min_score if min_score is not None else MIN_ES_SCORE
31+ dsl['min_score'] = min_score
32 if limit is None:
33 limit = self.default_search_limit
34 else:
35@@ -617,20 +624,10 @@
36 except ElasticHttpError:
37 hits = []
38
39- # Sorted queries do not return scores, so don't filter if we have a
40- # sort.
41- if not all_requested and sort is None:
42- hits = self._filter_by_score(hits, min_score)
43-
44 return [
45 dict(doctype=hit['_type'], data=hit['_source']['data'])
46 for hit in hits]
47
48- @staticmethod
49- def _filter_by_score(results, min_score):
50- """Filter the results by a minimum score."""
51- return [res for res in results if res['_score'] >= min_score]
52-
53 def get_status(self):
54 with translate_error():
55 return self._client.status(index=self.index_name)['indices']
56
57=== modified file 'charmworld/tests/test_search.py'
58--- charmworld/tests/test_search.py 2014-03-12 19:22:56 +0000
59+++ charmworld/tests/test_search.py 2014-03-18 15:21:03 +0000
60@@ -5,7 +5,6 @@
61
62
63 from datetime import datetime
64-import random
65 from textwrap import dedent
66 from time import sleep
67
68@@ -684,14 +683,6 @@
69 result = self.index_client.api_search('charms', {}, None)
70 self.assertEqual(22, len(result))
71
72- def test_filter_by_score(self):
73- hits = [{'_score': i} for i in xrange(10)]
74- random.shuffle(hits)
75- cutoff = 4
76- filtered = self.index_client._filter_by_score(hits, cutoff)
77- for item in filtered:
78- self.assertTrue(item['_score'] >= cutoff)
79-
80 def test_api_search_with_store_error(self):
81 """api_search() does not return charms withs errors by default."""
82 self.makeCharm(name='evil-charm', charm_error=True)
83
84=== modified file 'charmworld/views/api/__init__.py'
85--- charmworld/views/api/__init__.py 2014-03-12 19:22:56 +0000
86+++ charmworld/views/api/__init__.py 2014-03-18 15:21:03 +0000
87@@ -720,9 +720,7 @@
88 if key in ('series', 'owner', 'name', 'categories'))
89 if text is None:
90 text = ['']
91- if min_score is None:
92- min_score = 1
93- else:
94+ if min_score is not None:
95 min_score = float(min_score[0])
96 params['i_provides'] = provides
97 params['i_requires'] = requires
98
99=== modified file 'charmworld/views/search.py'
100--- charmworld/views/search.py 2013-11-15 20:02:03 +0000
101+++ charmworld/views/search.py 2014-03-18 15:21:03 +0000
102@@ -31,10 +31,11 @@
103 request=request)
104 response.status_code = 503
105 return response
106- except ElasticHttpError:
107+ except ElasticHttpError as exc:
108 text = request.params["search_text"]
109 log.warning(
110 "User search error with search text='{}'".format(text))
111+ log.exception(exc)
112 response = render_to_response(
113 '../templates/search-terms-bad.pt',
114 {'search_text': text},

Subscribers

People subscribed via source and target branches

to all changes: