Completed
Pull Request — master (#358)
by
unknown
02:09
created

SearchRunner   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 72
Duplicated Lines 0 %
Metric Value
dl 0
loc 72
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A full_search() 0 13 3
A iselector() 0 11 2
A run() 0 12 2
A _pp_exit() 0 12 3
A simple_search() 0 14 3
A __init__() 0 3 1
1
# pylint: disable=no-member
2
3
from easydict import EasyDict
4
from lib.items_selector import ItemsSelector
5
from lib.esbase_action import ESBaseAction
6
import logging
7
import sys
8
import elasticsearch
9
import json
10
11
logger = logging.getLogger(__name__)
12
13
14
class SearchRunner(ESBaseAction):
15
16
    def __init__(self, config=None):
17
        super(SearchRunner, self).__init__(config=config)
18
        self._iselector = None
19
20
    @property
21
    def iselector(self):
22
        """
23
        Used to fetch indices/snapshots and apply filter to them.
24
        """
25
        if not self._iselector:
26
            kwargs = self.config.copy()
27
            # Support iselectors parameters
28
            kwargs.update({'dry_run': False})
29
            self._iselector = ItemsSelector(self.client, **kwargs)
30
        return self._iselector
31
32
    def run(self, action=None, log_level='warn', operation_timeout=600, **kwargs):
33
        kwargs.update({
34
            'timeout': int(operation_timeout),
35
            'log_level': log_level
36
        })
37
        self.config = EasyDict(kwargs)
38
        self.set_up_logging()
39
40
        if action.endswith('.q'):
41
            self.simple_search()
42
        else:
43
            self.full_search()
44
45
    def simple_search(self):
46
        """Perform URI-based request search.
47
        """
48
        accepted_params = ('q', 'df', 'default_operator', 'from', 'size')
49
        kwargs = {k: self.config[k] for k in accepted_params if self.config[k]}
50
        indices = ','.join(self.iselector.indices())
51
52
        try:
53
            result = self.client.search(index=indices, **kwargs)
54
        except elasticsearch.ElasticsearchException as e:
55
            logger.error(e.message)
56
            sys.exit(2)
57
58
        self._pp_exit(result)
59
60
    def full_search(self):
61
        """Perform search using Query DSL.
62
        """
63
        accepted_params = ('from', 'size')
64
        kwargs = {k: self.config[k] for k in accepted_params if self.config[k]}
65
        try:
66
            result = self.client.search(index=self.config.index,
67
                                        body=self.config.body, **kwargs)
68
        except elasticsearch.ElasticsearchException as e:
69
            logger.error(e.message)
70
            sys.exit(2)
71
72
        self._pp_exit(result)
73
74
    def _pp_exit(self, data):
75
        """Print Elastcsearch JSON response and exit.
76
        """
77
        kwargs = {}
78
        if self.config.pretty:
79
            kwargs = {'indent': 4}
80
        print json.dumps(data, **kwargs)
81
82
        if data['hits']['total'] > 0:
83
            sys.exit(0)
84
        else:
85
            sys.exit(1)
86