<commit_before>from django.core.urlresolvers import reverse

from podaci.search import search_files_raw, authorize_filter
from search.searchers.base import DocumentSearcher, ResultSet
from search.searchers.base import DocumentSearchResult


class DocumentSearchPodaci(DocumentSearcher):
    PROVIDER = "ID Files"

    def search(self, q, offset=0, limit=100, **kwargs):
        query = {
            'query': {
                'query_string': {
                    'query': q,
                    'default_operator': 'AND',
                }
            },
            'from': offset,
            'size': limit,
            'fields': [
                'url', 'title', 'date_added', 'filename',
                'created_by', 'mimetype'
            ],
            'highlight': {
                'fields': {
                    'text': {}
                }
            }
        }
        user = self.request.requester if self.request else None
        query['filter'] = authorize_filter(user)
        results = search_files_raw(query)

        resultset = ResultSet(total=results['hits']['total'])
        for r in results['hits']['hits']:
            text = '<br/>'.join(r.get('highlight', {}).get('text', []))
            field = lambda n: ''.join(r["fields"].get(n, []))

            url = field('url')
            if url is None or not len(url):
                url = reverse('podaci_file_detail', kwargs={'pk': r['_id']})

            title = field('title')
            if title is None or not len(title):<commit_msg>Return 0 results if no hits found<commit_after>from django.core.urlresolvers import reverse

from podaci.search import search_files_raw, authorize_filter
from search.searchers.base import DocumentSearcher, ResultSet
from search.searchers.base import DocumentSearchResult


class DocumentSearchPodaci(DocumentSearcher):
    PROVIDER = "ID Files"

    def search(self, q, offset=0, limit=100, **kwargs):
        query = {
            'query': {
                'query_string': {
                    'query': q,
                    'default_operator': 'AND',
                }
            },
            'from': offset,
            'size': limit,
            'fields': [
                'url', 'title', 'date_added', 'filename',
                'created_by', 'mimetype'
            ],
            'highlight': {
                'fields': {
                    'text': {}
                }
            }
        }
        user = self.request.requester if self.request else None
        query['filter'] = authorize_filter(user)
        results = search_files_raw(query)
        if not results.has_key('hits'):
            return ResultSet(total=0)

        resultset = ResultSet(total=results['hits']['total'])
        for r in results['hits']['hits']:
            text = '<br/>'.join(r.get('highlight', {}).get('text', []))
            field = lambda n: ''.join(r["fields"].get(n, []))

            url = field('url')
            if url is None or not len(url):
                url = reverse('podaci_file_detail', kwargs={'pk': r['_id']})

            title = field('title')
            if title is None or not len(title):
{'fraction_non_alphanumeric': 0.1354365838113448, 'fraction_numerical': 0.0031867431485022306, 'mean_word_length': 1.785270629991127, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6419385', 'n_tokens_mistral': 881, 'n_tokens_neox': 853, 'n_words': 200}