Increase timeout for member stop
[integration/test.git] / csit / scripts / generate_searchSourceJSON.py
1 from copy import deepcopy as dc
2
3 # Template for search source format
4 SEARCH_SOURCE_FORMAT = {"index": None, "filter": [],
5                         "query": {"language": "lucene", "query": ""}}
6
7 # Template for filter format
8 FILTER_FORMAT = {
9     "query": {
10         "match": {
11             "placeholder_field": {
12                 "query": "query_phrase",
13                 "type": "phrase"
14             }
15         }
16     }
17 }
18
19
20 def generate(dash_config, viz_config, index_pattern):
21
22     search_source = dc(SEARCH_SOURCE_FORMAT)
23
24     # Search for 'match-with' and 'field' for each keys in 'filter' either
25     # in viz_config or dash_config
26     #
27     # ex:- filter:
28     #        1:
29     #           field: my_field
30     #           match-with: pattern
31
32     try:
33         filters = dash_config['filter']
34         for _, value in filters.items():
35             try:
36                 temp = dc(FILTER_FORMAT)
37                 temp['query']['match'][value['field']
38                                        ] = \
39                     temp['query']['match']['placeholder_field']
40                 temp['query']['match'][value['field']
41                                        ]['query'] = value['match-with']
42                 del temp['query']['match']['placeholder_field']
43                 search_source['filter'].append(temp)
44             except KeyError:
45                 continue
46     except KeyError:
47         pass
48
49     try:
50         filters = viz_config['filter']
51         for _, value in filters.items():
52             try:
53                 temp = dc(FILTER_FORMAT)
54                 temp['query']['match'][value['field']
55                                        ] = \
56                     temp['query']['match']['placeholder_field']
57                 temp['query']['match'][value['field']
58                                        ]['query'] = value['match-with']
59                 del temp['query']['match']['placeholder_field']
60                 search_source['filter'].append(temp)
61             except KeyError:
62                 continue
63     except KeyError:
64         pass
65
66     search_source['index'] = index_pattern
67
68     return search_source