GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ElasticSearch.add()   B
last analyzed

Complexity

Conditions 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
1
from __future__ import absolute_import
2
3
from elasticsearch import Elasticsearch
4
5
from simple_forums import models
6
from simple_forums.backends.search import BaseSearch, SearchResultSet
7
8
9
class ElasticSearch(BaseSearch):
10
    """ Search backend using elasticsearch service """
11
    REQUIRED_SETTINGS = ['host', 'port']
12
13
    def __init__(self, *args, **kwargs):
14
        super(ElasticSearch, self).__init__(*args, **kwargs)
15
16
        host = self.connection_info['host']
17
        port = self.connection_info['port']
18
19
        self.es = Elasticsearch([{'host': host, 'port': port}])
20
21
        # get index name or use default of 'forums'
22
        self.index = self.connection_settings.get('index', 'forums')
23
24
    def add(self, thread):
25
        """ Add the given object to the search index """
26
        assert isinstance(thread, models.Thread), "Can only index threads"
27
28
        data = {
29
            'title': thread.title,
30
        }
31
32
        self.es.index(
33
            index=self.index,
34
            doc_type='thread',
35
            id=thread.pk,
36
            body=data)
37
38
        for message in thread.message_set.all():
39
            data = {
40
                'body': message.body,
41
            }
42
43
            self.es.index(
44
                index=self.index,
45
                doc_type='message',
46
                id=message.pk,
47
                body=data)
48
49
    def remove(self, thread):
50
        """ Remove the given object from the search index """
51
        assert isinstance(thread, models.Thread), \
52
            "'thread' is not a thread instance"
53
54
        self.es.delete(
55
            index=self.index,
56
            doc_type='thread',
57
            id=thread.pk)
58
59
        for message in thread.message_set.all():
60
            self.es.delete(
61
                index=self.index,
62
                doc_type='message',
63
                id=thread.pk)
64
65
    def search(self, query_string):
66
        """ Search for the given query string """
67
        body = {
68
            'query': {
69
                'bool': {
70
                    'should': [
71
                        {
72
                            'match': {
73
                                'title': query_string,
74
                            },
75
                        },
76
                        {
77
                            'match': {
78
                                'body': query_string,
79
                            },
80
                        },
81
                    ]
82
                }
83
            }
84
        }
85
86
        search_results = self.es.search(
87
            index=self.index,
88
            doc_type='message,thread',
89
            body=body)
90
91
        hits = search_results.get('hits').get('hits')
92
93
        result_set = SearchResultSet()
94
95
        for hit in hits:
96
            id = hit.get('_id')
97
            doc_type = hit.get('_type')
98
99
            if doc_type == 'thread':
100
                obj = models.Thread.objects.get(id=id)
101
            elif doc_type == 'message':
102
                obj = models.Message.objects.get(id=id)
103
104
            score = hit.get('_score')
105
            result_set.add(obj, score)
106
107
        return result_set
108
109
    def wipe(self):
110
        """ Wipe the search index of all data """
111
        if self.es.indices.exists(self.index):
112
            self.es.indices.delete(self.index)
113