def test_query(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {'hits': 3}
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)
    def test_query(self):
        # Query:
        # {
        #     "size": 0,
        #     "query": {
        #         "match_all": {}
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "skipped": 0,
                "successful": 1,
                "total": 1
            },
            "hits": {
                "hits": [],
                "max_score": None,
                "total": {
                    "relation": "eq",
                    "value": 3
                }
            },
            "timed_out": False,
            "took": 3
        }

        expected = {'hits': 3, 'took_milliseconds': 3}
        result = convert_result(parse_response(response))
        self.assertEqual(expected, result)
    def test_query(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3
        }
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)
    def test_query_old(self):
        # Query:
        # {
        #     "size": 0,
        #     "query": {
        #         "match_all": {}
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {'hits': 3, 'took_milliseconds': 1}
        result = convert_result(parse_response(response))
        self.assertEqual(expected, result)
    def test_extended_stats(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     },
        #     'aggs': {
        #         'val_extended_stats': {
        #             'extended_stats': {'field': 'val'}
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "val_extended_stats": {
                    "avg": 2.0,
                    "count": 3,
                    "max": 3.0,
                    "min": 1.0,
                    "std_deviation": 0.816496580927726,
                    "std_deviation_bounds": {
                        "lower": 0.36700683814454793,
                        "upper": 3.632993161855452
                    },
                    "sum": 6.0,
                    "sum_of_squares": 14.0,
                    "variance": 0.6666666666666666
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3,
            'val_extended_stats_avg': 2.0,
            'val_extended_stats_count': 3,
            'val_extended_stats_max': 3.0,
            'val_extended_stats_min': 1.0,
            'val_extended_stats_sum': 6.0,
            'val_extended_stats_std_deviation': 0.816496580927726,
            'val_extended_stats_std_deviation_bounds_lower':
            0.36700683814454793,
            'val_extended_stats_std_deviation_bounds_upper': 3.632993161855452,
            'val_extended_stats_sum_of_squares': 14.0,
            'val_extended_stats_variance': 0.6666666666666666
        }
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)
    def test_extended_stats(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     },
        #     'aggs': {
        #         'val_extended_stats': {
        #             'extended_stats': {'field': 'val'}
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "val_extended_stats": {
                    "avg": 2.0,
                    "count": 3,
                    "max": 3.0,
                    "min": 1.0,
                    "std_deviation": 0.816496580927726,
                    "std_deviation_bounds": {
                        "lower": 0.36700683814454793,
                        "upper": 3.632993161855452
                    },
                    "sum": 6.0,
                    "sum_of_squares": 14.0,
                    "variance": 0.6666666666666666
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3,
            'val_extended_stats_avg': 2.0,
            'val_extended_stats_count': 3,
            'val_extended_stats_max': 3.0,
            'val_extended_stats_min': 1.0,
            'val_extended_stats_sum': 6.0,
            'val_extended_stats_std_deviation': 0.816496580927726,
            'val_extended_stats_std_deviation_bounds_lower': 0.36700683814454793,
            'val_extended_stats_std_deviation_bounds_upper': 3.632993161855452,
            'val_extended_stats_sum_of_squares': 14.0,
            'val_extended_stats_variance': 0.6666666666666666

        }
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)
    def test_percentiles(self):
        # Query:
        # {
        #     "size": 0,
        #     "query": {
        #         "match_all": {}
        #     },
        #     "aggs": {
        #         "val_percentiles": {
        #             "percentiles": {"field": "val"}
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "val_percentiles": {
                    "values": {
                        "1.0": 1.02,
                        "25.0": 1.5,
                        "5.0": 1.1,
                        "50.0": 2.0,
                        "75.0": 2.5,
                        "95.0": 2.9,
                        "99.0": 2.98
                    }
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": {
                    "relation": "eq",
                    "value": 3
                }
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3,
            'took_milliseconds': 1,
            'val_percentiles_values_1_0': 1.02,
            'val_percentiles_values_5_0': 1.1,
            'val_percentiles_values_25_0': 1.5,
            'val_percentiles_values_50_0': 2.0,
            'val_percentiles_values_75_0': 2.5,
            'val_percentiles_values_95_0': 2.9,
            'val_percentiles_values_99_0': 2.98
        }
        result = convert_result(parse_response(response))
        self.assertEqual(expected, result)
Exemple #8
0
def run_query(es_client, name, indices, query, timeout):
    try:
        response = es_client.search(index=indices, body=query, request_timeout=timeout)

        metrics = parse_response(response, [name])
    except Exception:
        logging.exception('Error while querying indices [%s], query [%s].', indices, query)
    else:
        update_gauges(metrics)
    def test_percentiles(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     },
        #     'aggs': {
        #         'val_percentiles': {
        #             'percentiles': {'field': 'val'}
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "val_percentiles": {
                    "values": {
                        "1.0": 1.02,
                        "25.0": 1.5,
                        "5.0": 1.1,
                        "50.0": 2.0,
                        "75.0": 2.5,
                        "95.0": 2.9,
                        "99.0": 2.98
                    }
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3,
            'val_percentiles_values_1.0': 1.02,
            'val_percentiles_values_5.0': 1.1,
            'val_percentiles_values_25.0': 1.5,
            'val_percentiles_values_50.0': 2.0,
            'val_percentiles_values_75.0': 2.5,
            'val_percentiles_values_95.0': 2.9,
            'val_percentiles_values_99.0': 2.98
        }
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)
    def test_percentiles(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     },
        #     'aggs': {
        #         'val_percentiles': {
        #             'percentiles': {'field': 'val'}
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "val_percentiles": {
                    "values": {
                        "1.0": 1.02,
                        "25.0": 1.5,
                        "5.0": 1.1,
                        "50.0": 2.0,
                        "75.0": 2.5,
                        "95.0": 2.9,
                        "99.0": 2.98
                    }
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3,
            'val_percentiles_values_1.0': 1.02,
            'val_percentiles_values_5.0': 1.1,
            'val_percentiles_values_25.0': 1.5,
            'val_percentiles_values_50.0': 2.0,
            'val_percentiles_values_75.0': 2.5,
            'val_percentiles_values_95.0': 2.9,
            'val_percentiles_values_99.0': 2.98
        }
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)
    def test_filter(self):
        # Query:
        # {
        #     "size": 0,
        #     "query": {
        #         "match_all": {}
        #     },
        #     "aggs": {
        #         "group1_filter": {
        #             "filter": {"term": {"group1": "a"}},
        #             "aggs": {
        #                 "val_sum": {
        #                     "sum": {"field": "val"}
        #                 }
        #             }
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "group1_filter": {
                    "doc_count": 2,
                    "val_sum": {
                        "value": 3.0
                    }
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": {
                    "relation": "eq",
                    "value": 3
                }
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3,
            'took_milliseconds': 1,
            'group1_filter_doc_count': 2,
            'group1_filter_val_sum_value': 3.0
        }
        result = convert_result(parse_response(response))
        self.assertEqual(expected, result)
    def test_stats(self):
        # Query:
        # {
        #     "size": 0,
        #     "query": {
        #         "match_all": {}
        #     },
        #     "aggs": {
        #         "val_stats": {
        #             "stats": {"field": "val"}
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "val_stats": {
                    "avg": 2.0,
                    "count": 3,
                    "max": 3.0,
                    "min": 1.0,
                    "sum": 6.0
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": {
                    "relation": "eq",
                    "value": 3
                }
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3,
            'took_milliseconds': 1,
            'val_stats_avg': 2.0,
            'val_stats_count': 3,
            'val_stats_max': 3.0,
            'val_stats_min': 1.0,
            'val_stats_sum': 6.0
        }
        result = convert_result(parse_response(response))
        self.assertEqual(expected, result)
def run_query(es_client, name, indices, query, timeout):
    try:
        response = es_client.search(index=indices, body=query, request_timeout=timeout)

        metrics = parse_response(response, [name])
    except ElasticsearchException as e:
        if isinstance(e, NotFoundError):
            logging.warn('Not found indices [%s]. Zeroing metrics for query [%s].', indices, name)
            zero_gauges(name)
        else:
            logging.exception('Error while querying indices [%s], query [%s]. Dropping related metrics.', indices, query)
            drop_gauges(name)
        return
    update_gauges(metrics)
    def test_filter(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     },
        #     'aggs': {
        #         'group1_filter': {
        #             'filter': {'term': {'group1': 'a'}},
        #             'aggs': {
        #                 'val_sum': {
        #                     'sum': {'field': 'val'}
        #                 }
        #             }
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "group1_filter": {
                    "doc_count": 2,
                    "val_sum": {
                        "value": 3.0
                    }
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3,
            'took_milliseconds': 1,
            'group1_filter_doc_count': 2,
            'group1_filter_val_sum_value': 3.0
        }
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)
    def test_stats(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     },
        #     'aggs': {
        #         'val_stats': {
        #             'stats': {'field': 'val'}
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "val_stats": {
                    "avg": 2.0,
                    "count": 3,
                    "max": 3.0,
                    "min": 1.0,
                    "sum": 6.0
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3,
            'took_milliseconds': 1,
            'val_stats_avg': 2.0,
            'val_stats_count': 3,
            'val_stats_max': 3.0,
            'val_stats_min': 1.0,
            'val_stats_sum': 6.0
        }
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)
    def test_filter(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     },
        #     'aggs': {
        #         'group1_filter': {
        #             'filter': {'term': {'group1': 'a'}},
        #             'aggs': {
        #                 'val_sum': {
        #                     'sum': {'field': 'val'}
        #                 }
        #             }
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "group1_filter": {
                    "doc_count": 2,
                    "val_sum": {
                        "value": 3.0
                    }
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3,
            'group1_filter_doc_count': 2,
            'group1_filter_val_sum_value': 3.0
        }
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)
    def test_stats(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     },
        #     'aggs': {
        #         'val_stats': {
        #             'stats': {'field': 'val'}
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "val_stats": {
                    "avg": 2.0,
                    "count": 3,
                    "max": 3.0,
                    "min": 1.0,
                    "sum": 6.0
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3,
            'val_stats_avg': 2.0,
            'val_stats_count': 3,
            'val_stats_max': 3.0,
            'val_stats_min': 1.0,
            'val_stats_sum': 6.0
        }
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)
Exemple #18
0
    def scheduled_run(scheduled_time, ):
        try:
            response = es_client.search(index=indices, body=query)

            metrics = parse_response(response, [name])
        except Exception:
            logging.exception('Error while querying indices [%s], query [%s].',
                              indices, query)
        else:
            update_gauges(metrics)

        current_time = time.monotonic()
        next_scheduled_time = scheduled_time + interval
        while next_scheduled_time < current_time:
            next_scheduled_time += interval

        scheduler.enterabs(next_scheduled_time, 1, scheduled_run,
                           (next_scheduled_time, ))
    def scheduled_run(scheduled_time,):
        try:
            response = es_client.search(index=indices, body=query)

            metrics = parse_response(response, [name])
        except Exception:
            logging.exception('Error while querying indices [%s], query [%s].', indices, query)
        else:
            update_gauges(metrics)

        current_time = time.monotonic()
        next_scheduled_time = scheduled_time + interval
        while next_scheduled_time < current_time:
            next_scheduled_time += interval

        scheduler.enterabs(
            next_scheduled_time,
            1,
            scheduled_run,
            (next_scheduled_time,)
        )
    def test_avg(self):
        # Query:
        # {
        #     "size": 0,
        #     "query": {
        #         "match_all": {}
        #     },
        #     "aggs": {
        #         "val_avg": {
        #             "avg": {"field": "val"}
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "val_avg": {
                    "value": 2.0
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3,
            'took_milliseconds': 1,
            'val_avg_value': 2
        }
        result = convert_result(parse_response(response))
        self.assertEqual(expected, result)
    def test_avg(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     },
        #     'aggs': {
        #         'val_avg': {
        #             'avg': {'field': 'val'}
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "val_avg": {
                    "value": 2.0
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3,
            'val_avg_value': 2
        }
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)
    def test_avg(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     },
        #     'aggs': {
        #         'val_avg': {
        #             'avg': {'field': 'val'}
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "val_avg": {
                    "value": 2.0
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {'hits': 3, 'val_avg_value': 2}
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)
    def test_composite(self):
        # Query:
        # {
        #     "size": 0,
        #     "query": {
        #         "match_all": {}
        #     },
        #     "aggs": {
        #         "group_comp": {
        #             "composite": {
        #                 "sources": [
        #                     {"group1": {"terms": {"field": "group1.keyword"}}},
        #                     {"val": {"terms": {"field": "val"}}}
        #                 ]
        #             },
        #             "aggs": {
        #                 "val_sum": {
        #                     "sum": {"field": "val"}
        #                 }
        #             }
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "total": 1,
                "successful": 1,
                "skipped": 0,
                "failed": 0
            },
            "aggregations": {
                "group_comp": {
                    "after_key": {
                        "group1": "b",
                        "val": 3
                    },
                    "buckets": [{
                        "doc_count": 1,
                        "key": {
                            "group1": "a",
                            "val": 1
                        },
                        "val_sum": {
                            "value": 1
                        }
                    }, {
                        "doc_count": 1,
                        "key": {
                            "group1": "a",
                            "val": 2
                        },
                        "val_sum": {
                            "value": 2
                        }
                    }, {
                        "doc_count": 1,
                        "key": {
                            "group1": "b",
                            "val": 3
                        },
                        "val_sum": {
                            "value": 3
                        }
                    }]
                }
            },
            "hits": {
                "hits": [],
                "max_score": None,
                "total": {
                    "relation": "eq",
                    "value": 3
                }
            },
            "timed_out": False,
            "took": 3
        }

        expected = {
            'hits':
            3,
            'took_milliseconds':
            3,
            'group_comp_doc_count{group_comp_group1="a",group_comp_val="1"}':
            1,
            'group_comp_val_sum_value{group_comp_group1="a",group_comp_val="1"}':
            1,
            'group_comp_doc_count{group_comp_group1="a",group_comp_val="2"}':
            1,
            'group_comp_val_sum_value{group_comp_group1="a",group_comp_val="2"}':
            2,
            'group_comp_doc_count{group_comp_group1="b",group_comp_val="3"}':
            1,
            'group_comp_val_sum_value{group_comp_group1="b",group_comp_val="3"}':
            3,
        }
        result = convert_result(parse_response(response))
        self.assertEqual(expected, result)
    def test_nested_terms(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     },
        #     'aggs': {
        #         'group1_terms': {
        #             'terms': {'field': 'group1'},
        #             'aggs': {
        #                 'val_sum': {
        #                     'sum': {'field': 'val'}
        #                 },
        #                 'group2_terms': {
        #                     'terms': {'field': 'group2'},
        #                     'aggs': {
        #                         'val_sum': {
        #                             'sum': {'field': 'val'}
        #                         }
        #                     }
        #                 }
        #             }
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "group1_terms": {
                    "buckets": [
                        {
                            "doc_count": 2,
                            "group2_terms": {
                                "buckets": [
                                    {
                                        "doc_count": 1,
                                        "key": "a",
                                        "val_sum": {
                                            "value": 1.0
                                        }
                                    },
                                    {
                                        "doc_count": 1,
                                        "key": "b",
                                        "val_sum": {
                                            "value": 2.0
                                        }
                                    }
                                ],
                                "doc_count_error_upper_bound": 0,
                                "sum_other_doc_count": 0
                            },
                            "key": "a",
                            "val_sum": {
                                "value": 3.0
                            }
                        },
                        {
                            "doc_count": 1,
                            "group2_terms": {
                                "buckets": [
                                    {
                                        "doc_count": 1,
                                        "key": "b",
                                        "val_sum": {
                                            "value": 3.0
                                        }
                                    }
                                ],
                                "doc_count_error_upper_bound": 0,
                                "sum_other_doc_count": 0
                            },
                            "key": "b",
                            "val_sum": {
                                "value": 3.0
                            }
                        }
                    ],
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 2
        }

        expected = {
            'hits': 3,
            'group1_terms_doc_count_error_upper_bound': 0,
            'group1_terms_sum_other_doc_count': 0,
            'group1_terms_doc_count{bucket="a"}': 2,
            'group1_terms_val_sum_value{bucket="a"}': 3.0,
            'group1_terms_group2_terms_doc_count_error_upper_bound{bucket="a"}': 0,
            'group1_terms_group2_terms_sum_other_doc_count{bucket="a"}': 0,
            'group1_terms_group2_terms_doc_count{bucket="a_a"}': 1,
            'group1_terms_group2_terms_val_sum_value{bucket="a_a"}': 1.0,
            'group1_terms_group2_terms_doc_count{bucket="a_b"}': 1,
            'group1_terms_group2_terms_val_sum_value{bucket="a_b"}': 2.0,
            'group1_terms_doc_count{bucket="b"}': 1,
            'group1_terms_val_sum_value{bucket="b"}': 3.0,
            'group1_terms_group2_terms_doc_count_error_upper_bound{bucket="b"}': 0,
            'group1_terms_group2_terms_sum_other_doc_count{bucket="b"}': 0,
            'group1_terms_group2_terms_doc_count{bucket="b_b"}': 1,
            'group1_terms_group2_terms_val_sum_value{bucket="b_b"}': 3.0,
        }
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)
    def test_bad_chars(self):
        # Query:
        # {
        #     "size": 0,
        #     "query": {
        #         "match_all": {}
        #     },
        #     "aggs": {
        #         "1-group-filter-1": {
        #             "filters": {
        #                 "filters": {
        #                     "group_a": {"term": {"group1": "a"}},
        #                     "group_b": {"term": {"group1": "b"}}
        #                 }
        #             },
        #             "aggs": {
        #                 "val_sum": {
        #                     "sum": {"field": "val"}
        #                 }
        #             }
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "1-group-filter-1": {
                    "buckets": {
                        "group_a": {
                            "doc_count": 2,
                            "val_sum": {
                                "value": 3.0
                            }
                        },
                        "group_b": {
                            "doc_count": 1,
                            "val_sum": {
                                "value": 3.0
                            }
                        }
                    }
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3,
            'took_milliseconds': 1,
            '__group_filter_1_doc_count{_group_filter_1="group_a"}': 2,
            '__group_filter_1_doc_count{_group_filter_1="group_b"}': 1,
            '__group_filter_1_val_sum_value{_group_filter_1="group_a"}': 3.0,
            '__group_filter_1_val_sum_value{_group_filter_1="group_b"}': 3.0
        }
        result = convert_result(parse_response(response))
        self.assertEqual(expected, result)
Exemple #26
0
    def test_terms_numeric(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     },
        #     'aggs': {
        #         'val_terms': {
        #             'terms': {'field': 'val'},
        #             'aggs': {
        #                 'val_sum': {
        #                     'sum': {'field': 'val'}
        #                 }
        #             }
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "total": 5,
                "successful": 5,
                "failed": 0
            },
            "aggregations": {
                "val_terms": {
                    "doc_count_error_upper_bound":
                    0,
                    "sum_other_doc_count":
                    0,
                    "buckets": [{
                        "key": 1,
                        "doc_count": 1,
                        "val_sum": {
                            "value": 1.0
                        }
                    }, {
                        "key": 2,
                        "doc_count": 1,
                        "val_sum": {
                            "value": 2.0
                        }
                    }, {
                        "key": 3,
                        "doc_count": 1,
                        "val_sum": {
                            "value": 3.0
                        }
                    }]
                }
            },
            "hits": {
                "total": 3,
                "max_score": 0.0,
                "hits": []
            },
            "timed_out": False,
            "took": 4
        }

        expected = {
            'hits': 3,
            'val_terms_doc_count_error_upper_bound': 0,
            'val_terms_sum_other_doc_count': 0,
            'val_terms_doc_count{val_terms="1"}': 1,
            'val_terms_val_sum_value{val_terms="1"}': 1.0,
            'val_terms_doc_count{val_terms="2"}': 1,
            'val_terms_val_sum_value{val_terms="2"}': 2.0,
            'val_terms_doc_count{val_terms="3"}': 1,
            'val_terms_val_sum_value{val_terms="3"}': 3.0
        }
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)
    def test_datefield_extended_stats(self):
        # Query:
        # {
        #     "size": 0,
        #     "query": {
        #         "match_all": {}
        #     },
        #     "aggs": {
        #         "val_extended_stats": {
        #             "extended_stats": {"field": "dateval"}
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "val_extended_stats": {
                    "avg": 1546300800000,
                    "avg_as_string": "2019-01-01T00:00:00.000Z",
                    "count": 1,
                    "max": 1546300800000,
                    "max_as_string": "2019-01-01T00:00:00.000Z",
                    "min": 1546300800000,
                    "min_as_string": "2019-01-01T00:00:00.000Z",
                    "std_deviation": 0,
                    "std_deviation_as_string": "1970-01-01T00:00:00.000Z",
                    "std_deviation_bounds": {
                        "upper": 1546300800000,
                        "lower": 1546300800000
                    },
                    "std_deviation_bounds_as_string": {
                        "upper": "2019-01-01T00:00:00.000Z",
                        "lower": "2019-01-01T00:00:00.000Z"
                    },
                    "sum": 1546300800000,
                    "sum_as_string": "2019-01-01T00:00:00.000Z",
                    "sum_of_squares": 2.39104616408064e+24,
                    "sum_of_squares_as_string":
                    "292278994-08-17T07:12:55.807Z",
                    "variance": 0,
                    "variance_as_string": "1970-01-01T00:00:00.000Z"
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": {
                    "relation": "eq",
                    "value": 4
                }
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 4,
            'took_milliseconds': 1,
            'val_extended_stats_avg': 1546300800000,
            'val_extended_stats_count': 1,
            'val_extended_stats_max': 1546300800000,
            'val_extended_stats_min': 1546300800000,
            'val_extended_stats_sum': 1546300800000,
            'val_extended_stats_std_deviation': 0,
            'val_extended_stats_std_deviation_bounds_upper': 1546300800000,
            'val_extended_stats_std_deviation_bounds_lower': 1546300800000,
            'val_extended_stats_sum_of_squares': 2.39104616408064e+24,
            'val_extended_stats_variance': 0
        }
        result = convert_result(parse_response(response))
        self.assertEqual(expected, result)
    def test_terms_numeric(self):
        # Query:
        # {
        #     "size": 0,
        #     "query": {
        #         "match_all": {}
        #     },
        #     "aggs": {
        #         "val_terms": {
        #             "terms": {"field": "val"},
        #             "aggs": {
        #                 "val_sum": {
        #                     "sum": {"field": "val"}
        #                 }
        #             }
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "total": 5,
                "successful": 5,
                "failed": 0
            },
            "aggregations": {
                "val_terms": {
                    "doc_count_error_upper_bound":
                    0,
                    "sum_other_doc_count":
                    0,
                    "buckets": [{
                        "key": 1,
                        "doc_count": 1,
                        "val_sum": {
                            "value": 1.0
                        }
                    }, {
                        "key": 2,
                        "doc_count": 1,
                        "val_sum": {
                            "value": 2.0
                        }
                    }, {
                        "key": 3,
                        "doc_count": 1,
                        "val_sum": {
                            "value": 3.0
                        }
                    }]
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": {
                    "relation": "eq",
                    "value": 3
                }
            },
            "timed_out": False,
            "took": 4
        }

        expected = {
            'hits': 3,
            'took_milliseconds': 4,
            'val_terms_doc_count_error_upper_bound': 0,
            'val_terms_sum_other_doc_count': 0,
            'val_terms_doc_count{val_terms="1"}': 1,
            'val_terms_val_sum_value{val_terms="1"}': 1.0,
            'val_terms_doc_count{val_terms="2"}': 1,
            'val_terms_val_sum_value{val_terms="2"}': 2.0,
            'val_terms_doc_count{val_terms="3"}': 1,
            'val_terms_val_sum_value{val_terms="3"}': 3.0
        }
        result = convert_result(parse_response(response))
        self.assertEqual(expected, result)
    def test_nested_terms(self):
        # Query:
        # {
        #     "size": 0,
        #     "query": {
        #         "match_all": {}
        #     },
        #     "aggs": {
        #         "group1_term": {
        #             "terms": {"field": "group1.keyword"},
        #             "aggs": {
        #                 "val_sum": {
        #                     "sum": {"field": "val"}
        #                 },
        #                 "group2_term": {
        #                     "terms": {"field": "group2.keyword"},
        #                     "aggs": {
        #                         "val_sum": {
        #                             "sum": {"field": "val"}
        #                         }
        #                     }
        #                 }
        #             }
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "group1_term": {
                    "buckets": [{
                        "doc_count": 2,
                        "group2_term": {
                            "buckets": [{
                                "doc_count": 1,
                                "key": "a",
                                "val_sum": {
                                    "value": 1.0
                                }
                            }, {
                                "doc_count": 1,
                                "key": "b",
                                "val_sum": {
                                    "value": 2.0
                                }
                            }],
                            "doc_count_error_upper_bound":
                            0,
                            "sum_other_doc_count":
                            0
                        },
                        "key": "a",
                        "val_sum": {
                            "value": 3.0
                        }
                    }, {
                        "doc_count": 1,
                        "group2_term": {
                            "buckets": [{
                                "doc_count": 1,
                                "key": "b",
                                "val_sum": {
                                    "value": 3.0
                                }
                            }],
                            "doc_count_error_upper_bound":
                            0,
                            "sum_other_doc_count":
                            0
                        },
                        "key": "b",
                        "val_sum": {
                            "value": 3.0
                        }
                    }],
                    "doc_count_error_upper_bound":
                    0,
                    "sum_other_doc_count":
                    0
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": {
                    "relation": "eq",
                    "value": 3
                }
            },
            "timed_out": False,
            "took": 2
        }

        expected = {
            'hits':
            3,
            'took_milliseconds':
            2,
            'group1_term_doc_count_error_upper_bound':
            0,
            'group1_term_sum_other_doc_count':
            0,
            'group1_term_doc_count{group1_term="a"}':
            2,
            'group1_term_val_sum_value{group1_term="a"}':
            3.0,
            'group1_term_group2_term_doc_count_error_upper_bound{group1_term="a"}':
            0,
            'group1_term_group2_term_sum_other_doc_count{group1_term="a"}':
            0,
            'group1_term_group2_term_doc_count{group1_term="a",group2_term="a"}':
            1,
            'group1_term_group2_term_val_sum_value{group1_term="a",group2_term="a"}':
            1.0,
            'group1_term_group2_term_doc_count{group1_term="a",group2_term="b"}':
            1,
            'group1_term_group2_term_val_sum_value{group1_term="a",group2_term="b"}':
            2.0,
            'group1_term_doc_count{group1_term="b"}':
            1,
            'group1_term_val_sum_value{group1_term="b"}':
            3.0,
            'group1_term_group2_term_doc_count_error_upper_bound{group1_term="b"}':
            0,
            'group1_term_group2_term_sum_other_doc_count{group1_term="b"}':
            0,
            'group1_term_group2_term_doc_count{group1_term="b",group2_term="b"}':
            1,
            'group1_term_group2_term_val_sum_value{group1_term="b",group2_term="b"}':
            3.0,
        }
        result = convert_result(parse_response(response))
        self.assertEqual(expected, result)
    def test_filters_anonymous(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     },
        #     'aggs': {
        #         'group_filters': {
        #             'filters': {
        #                 'filters': [
        #                     {'term': {'group1': 'a'}},
        #                     {'term': {'group1': 'b'}}
        #                 ]
        #             },
        #             'aggs': {
        #                 'val_sum': {
        #                     'sum': {'field': 'val'}
        #                 }
        #             }
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "group_filters": {
                    "buckets": [{
                        "doc_count": 2,
                        "val_sum": {
                            "value": 3.0
                        }
                    }, {
                        "doc_count": 1,
                        "val_sum": {
                            "value": 3.0
                        }
                    }]
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 1
        }

        expected = {
            'hits': 3,
            'group_filters_doc_count{bucket="filter_0"}': 2,
            'group_filters_doc_count{bucket="filter_1"}': 1,
            'group_filters_val_sum_value{bucket="filter_0"}': 3.0,
            'group_filters_val_sum_value{bucket="filter_1"}': 3.0
        }
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)
    def test_nested_terms(self):
        # Query:
        # {
        #     'size': 0,
        #     'query': {
        #         'match_all': {}
        #     },
        #     'aggs': {
        #         'group1_terms': {
        #             'terms': {'field': 'group1'},
        #             'aggs': {
        #                 'val_sum': {
        #                     'sum': {'field': 'val'}
        #                 },
        #                 'group2_terms': {
        #                     'terms': {'field': 'group2'},
        #                     'aggs': {
        #                         'val_sum': {
        #                             'sum': {'field': 'val'}
        #                         }
        #                     }
        #                 }
        #             }
        #         }
        #     }
        # }
        response = {
            "_shards": {
                "failed": 0,
                "successful": 5,
                "total": 5
            },
            "aggregations": {
                "group1_terms": {
                    "buckets": [{
                        "doc_count": 2,
                        "group2_terms": {
                            "buckets": [{
                                "doc_count": 1,
                                "key": "a",
                                "val_sum": {
                                    "value": 1.0
                                }
                            }, {
                                "doc_count": 1,
                                "key": "b",
                                "val_sum": {
                                    "value": 2.0
                                }
                            }],
                            "doc_count_error_upper_bound":
                            0,
                            "sum_other_doc_count":
                            0
                        },
                        "key": "a",
                        "val_sum": {
                            "value": 3.0
                        }
                    }, {
                        "doc_count": 1,
                        "group2_terms": {
                            "buckets": [{
                                "doc_count": 1,
                                "key": "b",
                                "val_sum": {
                                    "value": 3.0
                                }
                            }],
                            "doc_count_error_upper_bound":
                            0,
                            "sum_other_doc_count":
                            0
                        },
                        "key": "b",
                        "val_sum": {
                            "value": 3.0
                        }
                    }],
                    "doc_count_error_upper_bound":
                    0,
                    "sum_other_doc_count":
                    0
                }
            },
            "hits": {
                "hits": [],
                "max_score": 0.0,
                "total": 3
            },
            "timed_out": False,
            "took": 2
        }

        expected = {
            'hits': 3,
            'group1_terms_doc_count_error_upper_bound': 0,
            'group1_terms_sum_other_doc_count': 0,
            'group1_terms_doc_count{bucket="a"}': 2,
            'group1_terms_val_sum_value{bucket="a"}': 3.0,
            'group1_terms_group2_terms_doc_count_error_upper_bound{bucket="a"}':
            0,
            'group1_terms_group2_terms_sum_other_doc_count{bucket="a"}': 0,
            'group1_terms_group2_terms_doc_count{bucket="a_a"}': 1,
            'group1_terms_group2_terms_val_sum_value{bucket="a_a"}': 1.0,
            'group1_terms_group2_terms_doc_count{bucket="a_b"}': 1,
            'group1_terms_group2_terms_val_sum_value{bucket="a_b"}': 2.0,
            'group1_terms_doc_count{bucket="b"}': 1,
            'group1_terms_val_sum_value{bucket="b"}': 3.0,
            'group1_terms_group2_terms_doc_count_error_upper_bound{bucket="b"}':
            0,
            'group1_terms_group2_terms_sum_other_doc_count{bucket="b"}': 0,
            'group1_terms_group2_terms_doc_count{bucket="b_b"}': 1,
            'group1_terms_group2_terms_val_sum_value{bucket="b_b"}': 3.0,
        }
        result = convert_result(parse_response(response))
        self.assertEqual(result, expected)