Completed
Push — master ( ddc962...ab2a47 )
by
unknown
49s queued 25s
created

QueryBuilder._build_initial_query_string()   A

Complexity

Conditions 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 10
rs 9.2
1
class QueryBuilder:
2
    def __init__(self, query_params=None):
3
        """
4
        Creates a query builder
5
6
        :param query_params: dict with query parameters
7
        """
8
        self.query_params = query_params
9
        self.query = self._build_initial_query_string()
10
        self.filters = []
11
12
    def _build_initial_query_string(self):
13
        if self.query_params is None or 'query' not in self.query_params or self.query_params['query'] == '':
14
            return {
15
                'match_all': {}
16
            }
17
        else:
18
            return {
19
                'query_string': {
20
                    'default_field': '_all',
21
                    'query': self.query_params['query']
22
                }
23
            }
24
25
    def add_named_concept_filters(self, named_filter_concepts):
26
        """
27
        Adds named concept filters
28
29
        :param named_filter_concepts: dict with named filter concepts which will be mapped as the key as query param and the value as search string
30
        """
31
        for concept_key, concept_name in named_filter_concepts.items():
32
            self.add_concept_filter(concept_key, concept_name=concept_name)
33
34
    def add_concept_filters(self, filter_concepts):
35
        """
36
        Adds concept filters
37
38
        :param filter_concepts: list with filter concepts
39
        """
40
        for concept in filter_concepts:
41
            self.add_concept_filter(concept)
42
43
    def add_concept_filter(self, concept, concept_name=None):
44
        """
45
        Add a concept filter
46
47
        :param concept: concept which will be used as lowercase string in a search term
48
        :param concept_name: name of the place where there will be searched for
49
        """
50
        if concept in self.query_params.keys():
51
            if not concept_name:
52
                concept_name = concept
53
            if isinstance(self.query_params[concept], list):
54
                es_filter = {'or': []}
55
                for or_filter in self.query_params[concept]:
56
                    es_filter['or'].append(self._build_concept_term(concept_name, or_filter))
57
            else:
58
                es_filter = self._build_concept_term(concept_name, self.query_params[concept])
59
            self.filters.append(es_filter)
60
61
    @staticmethod
62
    def _build_concept_term(concept_name, concept):
63
        return {
64
            'term': {concept_name: str(concept).lower()}
65
        }
66
67
    def build(self):
68
        """
69
        Builds the query string, which can be used for a search query
70
71
        :return: the query string
72
        """
73
        if len(self.filters) > 0:
74
            return {
75
                'filtered': {
76
                    'query': self.query,
77
                    'filter': {
78
                        'and': self.filters
79
                    }
80
                }
81
            }
82
        else:
83
            return self.query
84