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' } } } })
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
def test_aggregate(self): q = ElasticQuery() q.aggregate(Aggregate.terms('agg_name', 'field')) assert_equal( self, q.dict(), {'aggregations': { 'agg_name': { 'terms': { 'field': 'field' } } }})
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' } } } } } })
def test_invalid_arg(self): # Test passing not a list with self.assertRaises(ValueError): Query.bool(must=set()) # And now an invalid list with self.assertRaises(ValueError): Query.bool(must=[None]) # And now an invalid list with self.assertRaises(ValueError): Query.bool(must=[Aggregate.terms('test', 'test')]) # And now an invalid list with self.assertRaises(ValueError): Query.range('field', gte=['error']) # Empty list should be OK/ignored Query.bool(must=[])
}, 'aggregations': { 'option_count': { 'sum': { 'field': 'option_count_current' } } } } } } } # Test aggregates print '[ElasticQuery] Testing: aggregates' aggregate = Aggregate.stats('field_name1') test('Aggregate.stats', aggregate, AGGREGATES['STATS']) aggregate = Aggregate.extended_stats('field_name1') test('Aggregate.extended_stats', aggregate, AGGREGATES['EXTENDED_STATS']) aggregate = Aggregate.histogram('field_name1', interval=100) test('Aggregate.histogram', aggregate, AGGREGATES['HISTOGRAM']) aggregate = Aggregate.date_histogram('field_name1') test('Aggregate.date_histogram', aggregate, AGGREGATES['DATE_HISTOGRAM']) aggregate = Aggregate.terms('field_name1') test('Aggregate.terms', aggregate, AGGREGATES['TERMS']) aggregate = Aggregate.sub(Aggregate.terms('field_name1'),
def return_single_field_search(field,search): q = ElasticQuery(es=Elasticsearch(),index=all_indexes,doc_type='') q.aggregate(Aggregate.terms(search,field)) q.query(Query.query_string(search,field,default_operator='OR',analyze_wildcard=True)) q.fields(field) ElasticQuery.sort(q,"_score",order="desc")
def test_no_aggregate(self): with self.assertRaises(NoAggregateError): Aggregate.doesnotexist()