def sources()

in src/plugins/brokers/kibbleES.py [0:0]


    def sources(self, sourceType = None, view = None):
        """ Get all sources or sources of a specific type for an org """
        s = []
        # Search for all sources of this organisation
        mustArray = [{
                        'term': {
                            'organisation': self.id
                        }
                    }
                    ]
        if view:
            res = self.broker.DB.get(
                index=self.broker.config['elasticsearch']['database'],
                doc_type="view",
                id = view
            )
            if res:
                mustArray.append({
                                'terms': {
                                    'sourceID': res['_source']['sourceList']
                                }
                            })
        # If we want a specific source type, amend the search criteria
        if sourceType:
            mustArray.append({
                                'term': {
                                    'type': sourceType
                                }
                            })
        # Run the search, fetch all results, 9999 max. TODO: Scroll???
        res = self.broker.DB.search(
            index=self.broker.config['elasticsearch']['database'],
            doc_type="source",
            size = 9999,
            body = {
                'query': {
                    'bool': {
                        'must': mustArray
                    }
                },
                'sort': {
                    'sourceURL': 'asc'
                }
            }
        )
    
        for hit in res['hits']['hits']:
            if sourceType == None or hit['_source']['type'] == sourceType:
                s.append(hit['_source'])
        return s