Exemple #1
0
    def test_query_aggregate_and_suggester(self):
        q = ElasticQuery()
        q.query(Query.match('field', 'query'))
        q.aggregate(Aggregate.max('agg_name', 'field'))
        q.suggest(Suggester.term('sugg_name', 'term text', 'term_field'))

        assert_equal(
            self, q.dict(), {
                'query': {
                    'match': {
                        'field': {
                            'query': 'query'
                        }
                    }
                },
                'aggregations': {
                    'agg_name': {
                        'max': {
                            'field': 'field'
                        }
                    }
                },
                'suggest': {
                    'sugg_name': {
                        'text': 'term text',
                        'term': {
                            'field': 'term_field'
                        }
                    }
                }
            })
Exemple #2
0
    def test_sort(self):
        q = ElasticQuery()
        q.sort('sort_field')

        assert_equal(self, q.dict(), {
            'sort': ['sort_field']
        })
    def query_ec(self, str_query, q_fields, start_date=0, end_date=0, index='logs-*', doc_type='logs',
                 hours=24, debug=False):
        if start_date > end_date:
            raise Exception('The start_date can\'t be greater than the end_date')

        if start_date == 0 or end_date == 0:
            dt_end_date = datetime.now().timestamp()
            dt_start_date = (datetime.now() - timedelta(hours=hours)).timestamp()
            start_date = int(dt_start_date) * 1000
            end_date = int(dt_end_date) * 1000

        # print(str(start_date) + ' -- ' + str(end_date))

        elastic_qry = ElasticQuery(es=self.elastic_client, index=index, doc_type=doc_type)
        elastic_qry.query(
            Query.bool(
                must=[Query.query_string(str_query),
                      Query.range('normalDate', gte=start_date, lte=end_date)]
            )
        )

        elastic_qry.aggregate(
            Aggregate.date_histogram('2', 'normalDate', '12h')
        )

        my_qry = elastic_qry.dict()
        my_qry['stored_fields'] = q_fields

        search_arr = list()
        header_qry = {"index": ["logs-*"], "ignore_unavailable": True}
        search_arr.append(header_qry)
        search_arr.append(my_qry)

        print('Elastic Query: ' + str(search_arr))
        print('------------------------------------------------------------------------------------')
        print('Lucene Query: ' + str_query)

        request = ''
        for each in search_arr:
            request += '%s \n' % json.dumps(each)

        # print(request)

        resp = self.elastic_client.msearch(body=request)

        if resp is None and len(resp['responses']) <= 0:
            return None
        else:
            response = resp['responses'][0]
            hits_data = list()
            if response['hits']['total'] > 0:
                for hit in response['hits']['hits']:
                    hits_data.append(hit)

        # print(str(hits_data))

        return search_arr, hits_data
Exemple #4
0
    def test_sort_order(self):
        q = ElasticQuery()
        q.sort('sort_field', order='desc')

        assert_equal(self, q.dict(),
                     {'sort': [{
                         'sort_field': {
                             'order': 'desc'
                         }
                     }]})
Exemple #5
0
    def test_just_query(self):
        q = ElasticQuery()
        q.query(Query.query_string('this is a querystring'))

        assert_equal(
            self, q.dict(),
            {'query': {
                'query_string': {
                    'query': 'this is a querystring'
                }
            }})
Exemple #6
0
    def test_aggregate(self):
        q = ElasticQuery()
        q.aggregate(Aggregate.terms('agg_name', 'field'))

        assert_equal(
            self, q.dict(),
            {'aggregations': {
                'agg_name': {
                    'terms': {
                        'field': 'field'
                    }
                }
            }})
Exemple #7
0
    def test_suggester(self):
        q = ElasticQuery()
        q.suggest(Suggester.term('sugg_name', 'term text', 'term_field'))

        assert_equal(self, q.dict(), {
            'suggest': {
                'sugg_name': {
                    'text': 'term text',
                    'term': {
                        'field': 'term_field'
                    }
                }
            }
        })
Exemple #8
0
    def test_misc(self):
        q = ElasticQuery()
        q.size(10)
        q.from_(50)
        q.timeout('60s')
        q.set('key', 'value')
        q.fields(('one_field', 'two_field'))

        assert_equal(self, q.dict(), {
            'size': 10,
            'from': 50,
            'timeout': '60s',
            'key': 'value',
            '_source': ['one_field', 'two_field']
        })
Exemple #9
0
    def test_nested_aggregate(self):
        q = ElasticQuery()
        q.aggregate(Aggregate.terms('agg_name', 'field').aggregate(
            Aggregate.sum('sub_agg_name', 'sub_field')
        ))

        assert_equal(self, q.dict(), {
            'aggregations': {
                'agg_name': {
                    'terms': {
                        'field': 'field'
                    },
                    'aggregations': {
                        'sub_agg_name': {
                            'sum': {
                                'field': 'sub_field'
                            }
                        }
                    }
                }
            }
        })