<commit_before>
import superdesk
from flask import current_app as app, json
from apps.archive.common import aggregations


class SearchService(superdesk.Service):
    """Federated search service.

    It can search ingest/content/archive/spike at the same time.
    """

    available_repos = ('ingest', 'archive')
    default_repos = ['ingest', 'archive']

    def _get_query(self, req):
        """Get elastic query."""
        args = getattr(req, 'args', {})
        return json.loads(args.get('source')) if args.get('source') else {}

    def _get_types(self, req):
        """Get document types for the given query."""
        args = getattr(req, 'args', {})
        repos = args.get('repo', ','.join(self.default_repos)).split(',')
        return [repo for repo in repos if repo in self.available_repos]

    def get(self, req, lookup):
        """Run elastic search agains on multiple doc types."""
        elastic = app.data.elastic
        query = self._get_query(req)
        types = self._get_types(req)
        query['aggs'] = aggregations
        hits = elastic.es.search(body=query, index=elastic.index, doc_type=types)
        docs = elastic._parse_hits(hits, 'ingest')  # any resource here will do
        for resource in types:
            response = {app.config['ITEMS']: [doc for doc in docs if doc['_type'] == resource]}
            getattr(app, 'on_fetched_resource')(resource, response)
            getattr(app, 'on_fetched_resource_%s' % resource)(response)<commit_msg>[SD-1063] Global search returns facets<commit_after>
import superdesk
from flask import current_app as app, json
from apps.archive.common import aggregations
from flask import current_app as app, json
from apps.archive.common import aggregations


class SearchService(superdesk.Service):
    """Federated search service.

    It can search ingest/content/archive/spike at the same time.
    """

    available_repos = ('ingest', 'archive')
    default_repos = ['ingest', 'archive']

    def _get_query(self, req):
        """Get elastic query."""
        args = getattr(req, 'args', {})
        query = json.loads(args.get('source')) if args.get('source') else {}
        query['aggs'] = aggregations
        return query

    def _get_types(self, req):
        """Get document types for the given query."""
        args = getattr(req, 'args', {})
        repos = args.get('repo', ','.join(self.default_repos)).split(',')
        return [repo for repo in repos if repo in self.available_repos]

    def get(self, req, lookup):
        """Run elastic search agains on multiple doc types."""
        elastic = app.data.elastic
        query = self._get_query(req)
        types = self._get_types(req)
        query['aggs'] = aggregations
        hits = elastic.es.search(body=query, index=elastic.index, doc_type=types)
        docs = elastic._parse_hits(hits, 'ingest')  # any resource here will do
        for resource in types:
            response = {app.config['ITEMS']: [doc for doc in docs if doc['_type'] == resource]}
            getattr(app, 'on_fetched_resource')(resource, response)
            getattr(app, 'on_fetched_resource_%s' % resource)(response)
{'fraction_non_alphanumeric': 0.12864385297845374, 'fraction_numerical': 0.0012674271229404308, 'mean_word_length': 3.0735483870967744, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4787256', 'n_tokens_mistral': 962, 'n_tokens_neox': 913, 'n_words': 268}