~jcsackett/charmworld/bac-tag-constraints

« back to all changes in this revision

Viewing changes to charmworld/search.py

  • Committer: Tarmac
  • Author(s): Brad Crittenden
  • Date: 2013-08-02 18:23:12 UTC
  • mfrom: (332.1.2 apisearch)
  • Revision ID: tarmac-20130802182312-ty9mt8k6h39pmr4h
[r=abentley][bug=][author=bac] Change api_search and related charms to ignore bundles for now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
204
204
                    }
205
205
                })
206
206
 
207
 
    def get_mapping(self, kind=CHARM):
 
207
    def get_mapping(self, doctype=CHARM):
208
208
        """Return the current mapping for 'charm'."""
209
 
        return self._client.get_mapping(self.index_name, kind)
 
209
        return self._client.get_mapping(self.index_name, doctype)
210
210
 
211
 
    def _index_item(self, item, kind):
 
211
    def _index_item(self, item, doctype):
212
212
        id_ = item['_id']
213
213
        with translate_error():
214
214
            self._client.index(
215
 
                self.index_name, kind, item, id_, refresh=True)
 
215
                self.index_name, doctype, item, id_, refresh=True)
216
216
 
217
217
    def index_charm(self, charm):
218
218
        self._index_item(charm, CHARM)
220
220
    def index_bundle(self, bundle):
221
221
        self._index_item(bundle, BUNDLE)
222
222
 
223
 
    def _delete_item(self, id_, kind):
 
223
    def _delete_item(self, id_, doctype):
224
224
        with translate_error():
225
 
            self._client.delete(self.index_name, kind, id_)
 
225
            self._client.delete(self.index_name, doctype, id_)
226
226
 
227
227
    def delete_charm(self, charm_id):
228
228
        self._delete_item(charm_id, CHARM)
230
230
    def delete_bundle(self, bundle_id):
231
231
        self._delete_item(bundle_id, BUNDLE)
232
232
 
233
 
    def _index_items(self, items, kind):
 
233
    def _index_items(self, items, doctype):
234
234
        if len(items) == 0:
235
235
            return
236
236
        self._client.bulk_index(
237
 
            self.index_name, kind, items, '_id', refresh=True)
 
237
            self.index_name, doctype, items, '_id', refresh=True)
238
238
 
239
239
    def index_charms(self, charms):
240
240
        self._index_items(charms, CHARM)
284
284
        return dsl
285
285
 
286
286
    @classmethod
287
 
    def _get_filtered(cls, dsl, filters, type_, valid_only):
 
287
    def _get_filtered(cls, dsl, filters, type_, valid_only, doctype=CHARM):
288
288
        and_clause = {
289
289
            'and': [{'terms': {key: value}} for key, value in filters.items()],
290
290
        }
 
291
 
291
292
        if valid_only:
292
293
            and_clause['and'].extend(cls.valid_charms_clauses)
293
294
 
308
309
            if len(typeclauses) > 0:
309
310
                and_clause['and'].append({'or': typeclauses})
310
311
 
 
312
        if doctype is not None:
 
313
            and_clause['and'].append({'type': {'value': doctype}})
 
314
 
311
315
        if len(and_clause['and']) == 0:
312
316
            and_clause = {'match_all': {}}
313
317
        dsl = {
320
324
        }
321
325
        return dsl
322
326
 
323
 
    def _get_item(self, id_, kind):
324
 
        result = self._client.get(self.index_name, kind, id_)
 
327
    def _get_item(self, id_, doctype):
 
328
        result = self._client.get(self.index_name, doctype, id_)
325
329
        return result['_source']
326
330
 
327
331
    def get(self, charm_id):
346
350
 
347
351
    def api_search(self, text='', filters=None, type_=None,
348
352
                   limit=None, valid_only=True, sort=None,
349
 
                   autocomplete=False):
 
353
                   autocomplete=False, doctype=CHARM):
350
354
        """Search charms (as used by the API).
351
355
 
352
356
        :param text: A string to search for-- will be analyzed (i.e. split
367
371
            charms are in order of most to least downloaded.  If 'new', charms
368
372
            are in order of most-recently to least-recently created.
369
373
        :param autocomplete: If true use a prefix search for autocompletion.
 
374
        :param doctype: Only return entities of the given type.  May be
 
375
            'charm', 'bundle', or None.  If None, do not filter.
370
376
        """
371
377
        if filters is None:
372
378
            filters = {}
373
379
        dsl = self._get_text_query(text, autocomplete)
374
380
        dsl = self._get_official_boost(dsl)
375
 
        dsl = self._get_filtered(dsl, filters, type_, valid_only)
 
381
        dsl = self._get_filtered(dsl, filters, type_, valid_only, doctype)
376
382
        if sort == 'downloaded':
377
383
            dsl['sort'] = [{
378
384
                'downloads': {'order': 'desc'},
405
411
 
406
412
        dsl = self._get_text_query(terms)
407
413
        dsl = self._get_official_boost(dsl)
408
 
        dsl = self._get_filtered(dsl, {}, None, valid_only)
 
414
        dsl = self._get_filtered(dsl, {}, None, valid_only, doctype=None)
409
415
 
410
416
        with Timer() as timer:
411
417
            status = self.get_status()
441
447
        }
442
448
 
443
449
    def related_charms(self, requires, provides, series=None,
444
 
                       exclude_name=None):
 
450
                       exclude_name=None, doctype=CHARM):
445
451
        """Determine the charms related to the specified interfaces.
446
452
 
447
453
        Results are of the form:
452
458
        :param series: If provided, limit results to the specified series.
453
459
        :param exclude_name: If not None, exclude results with the specified
454
460
            name.
 
461
        :param doctype: Only return entities of the given type.  May be
 
462
            'charm', 'bundle', or None.  If None, do not filter.
455
463
        :return: requires, provides, each of which is a dict mapping
456
464
            interfaces to lists of results.
457
465
        """
464
472
        if exclude_name is not None:
465
473
            and_clauses.append({'not': {'term': {'name': exclude_name}}})
466
474
        and_clauses.extend(self.valid_charms_clauses)
 
475
        if doctype is not None:
 
476
            and_clauses.append({'type': {'value': doctype}})
467
477
        dsl = {
468
478
            'filtered': {
469
479
                'filter': {