~jcsackett/charmworld/bac-tag-constraints

« back to all changes in this revision

Viewing changes to charmworld/search.py

[r=benji][bug=][author=sinzui] Update IndexClient.api_search to include the doctype of the found document.

Show diffs side-by-side

added added

removed removed

Lines of Context:
356
356
 
357
357
        if text != '':
358
358
            def make_query(fields):
359
 
                return  {
 
359
                return {
360
360
                    'query_string': {
361
361
                    'query': text,
362
362
                    'fields': ['data.' + field for field in fields],
473
473
    def api_search(self, text='', filters=None, type_=None,
474
474
                   limit=None, valid_only=True, sort=None,
475
475
                   autocomplete=False, doctype=CHARM):
476
 
        """Search charms (as used by the API).
 
476
        """Search charms and bundles (as used by the API).
477
477
 
478
478
        :param text: A string to search for-- will be analyzed (i.e. split
479
479
            into stems).
495
495
        :param autocomplete: If true use a prefix search for autocompletion.
496
496
        :param doctype: Only return entities of the given type.  May be
497
497
            'charm', 'bundle', or None.  If None, do not filter.
 
498
        return: A list of {doctype=<charm|bundle>, data=entity}.
498
499
        """
499
500
        if filters is None:
500
501
            filters = {}
515
516
            if limit < 0:
516
517
                raise NegativeLimit()
517
518
        hits = self._unlimited_search(dsl, limit)
518
 
 
519
 
        return [hit['_source']['data'] for hit in hits['hits']['hits']]
 
519
        return [
 
520
            dict(doctype=hit['_type'], data=hit['_source']['data'])
 
521
            for hit in hits['hits']['hits']]
520
522
 
521
523
    def get_status(self):
522
524
        with translate_error():
658
660
        with self.replacement(name) as copy:
659
661
            if charms is None:
660
662
                try:
661
 
                    charms = self.api_search(valid_only=False, doctype=CHARM)
 
663
                    result = self.api_search(valid_only=False, doctype=CHARM)
 
664
                    charms = [r['data'] for r in result]
662
665
                except IndexMissing:
663
666
                    charms = []
664
667
            copy.index_charms(charms)
665
668
 
666
669
            if bundles is None:
667
670
                try:
668
 
                    bundles = self.api_search(valid_only=False, doctype=BUNDLE)
 
671
                    result = self.api_search(valid_only=False, doctype=BUNDLE)
 
672
                    bundles = [r['data'] for r in result]
669
673
                except IndexMissing:
670
674
                    bundles = []
671
675
            copy.index_bundles(bundles)