def __init__(self, connection_alias, **connection_options):
     super(
         ElasticSearchBackend,
         self).__init__(
         connection_alias,
         **connection_options)
     self.manager = ArticleDocumentManager()
Exemplo n.º 2
0
 def __init__(self, connection_alias, **connection_options):
     super(ElasticSearchBackend, self).__init__(connection_alias,
                                                **connection_options)
     self.manager = ArticleDocumentManager()
     try:
         self._rebuild(None)
     except:
         pass
Exemplo n.º 3
0
    def handle(self, *args, **options):
        manager = ArticleDocumentManager()
        manager.delete_index()
        manager.rebuild()

        manager = ElapsedTimeDocument()
        manager.init()
Exemplo n.º 4
0
 def handle(self, *args, **options):
     if ELASTICSEARCH_ENABLED:
         ElaspedTimeDocumentManager.build_index()
         manager = ElapsedTimeDocument()
         manager.init()
         manager = ArticleDocumentManager()
         manager.delete_index()
         manager.rebuild()
Exemplo n.º 5
0
 def handle(self, *args, **options):
     manager = ArticleDocumentManager()
     manager.deleate_index()
     manager.rebuild()
Exemplo n.º 6
0
class ElasticSearchBackend(BaseSearchBackend):
    def __init__(self, connection_alias, **connection_options):
        super(ElasticSearchBackend, self).__init__(connection_alias,
                                                   **connection_options)
        self.manager = ArticleDocumentManager()
        self._rebuild(None)

    def _get_models(self, iterable):
        models = iterable if iterable else Article.objects.all()
        docs = self.manager.convert_to_doc(models)
        return docs

    def _create(self, models):
        self.manager.create_index()
        docs = self._get_models(models)
        self.manager.rebuild(docs)

    def _delete(self, models):
        for m in models:
            m.delete()
        return True

    def _rebuild(self, models):
        models = models if models else Article.objects.all()
        docs = self.manager.convert_to_doc(models)
        self.manager.update_docs(docs)

    def update(self, index, iterable, commit=True):
        models = self._get_models(iterable)
        self.manager.update_docs(models)

    def remove(self, obj_or_string):
        models = self._get_models([obj_or_string])
        self._delete(models)

    def clear(self, models=None, commit=True):
        self.remove(None)

    @log_query
    def search(self, query_string, **kwargs):
        logger.info('search query_string:' + query_string)

        start_offset = kwargs.get('start_offset')
        end_offset = kwargs.get('end_offset')

        q = Q('bool',
              must=[Q('match', body=query_string)],
              minimum_should_match="70%")

        search = ArticleDocument.search() \
                     .query('bool', filter=[q]) \
                     .filter('term', status='p') \
                     .filter('term', type='a') \
                     .source(False)[start_offset: end_offset]

        results = search.execute()
        hits = results['hits'].total
        raw_results = []
        for raw_result in results['hits']['hits']:
            app_label = 'blog'
            model_name = 'Article'
            additional_fields = {}

            # if 'highlight' in raw_result:
            #     additional_fields['highlighted'] = raw_result['highlight'].get(content_field, '')

            result_class = SearchResult

            result = result_class(app_label, model_name, raw_result['_id'],
                                  raw_result['_score'], **additional_fields)
            raw_results.append(result)
        facets = {}
        spelling_suggestion = None

        return {
            'results': raw_results,
            'hits': hits,
            'facets': facets,
            'spelling_suggestion': spelling_suggestion,
        }