the python elasticsearch , elasticsearch dsl packages are life-saver and 
got me converted to ES. 

Now I am trying to use elasticsearch dsl package to create pivot tables in 
ES  but am having hard time figuring out how to chain the buckets 
programmatically. 
while chaining the buckets / metrics manually works,  to chain them 
programmatically seems impossible 

here is an example 


from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search as dsl_search, A, Q, F 
# create client 
es = Elasticsearch('localhost:9200')
# data : from the definitive guide, slighlty modified 
#data from the definitive guide 
xData = [
{'doc_id' : 1, 'price' : 10000, 'color' : 'red',   'make' : 'honda',  
'sold' : '2014-10-28', 'city': 'ROME',   'insurance': 'y'},
{'doc_id' : 2, 'price' : 20000, 'color' : 'red',   'make' : 'honda',  
'sold' : '2014-11-05', 'city': 'ROME',   'insurance': 'n'},
{'doc_id' : 3, 'price' : 30000, 'color' : 'green', 'make' : 'ford',   
'sold' : '2014-05-18', 'city': 'Berlin', 'insurance': 'y'},
{'doc_id' : 4, 'price' : 15000, 'color' : 'blue',  'make' : 'toyota', 
'sold' : '2014-07-02', 'city': 'Berlin', 'insurance': 'n'},
{'doc_id' : 5, 'price' : 12000, 'color' : 'green', 'make' : 'toyota', 
'sold' : '2014-08-19', 'city': 'Berlin', 'insurance': 'n'},
{'doc_id' : 6, 'price' : 20000, 'color' : 'red',   'make' : 'honda',  
'sold' : '2014-11-05', 'city': 'Paris',  'insurance': 'n'},
{'doc_id' : 7, 'price' : 80000, 'color' : 'red',   'make' : 'bmw',    
'sold' : '2014-01-01', 'city': 'Paris',  'insurance': 'y'},
{'doc_id' : 8, 'price' : 25000, 'color' : 'blue',  'make' : 'ford',   
'sold' : '2014-02-12', 'city': 'Paris',  'insurance': 'y'}]

#create a mapping 
my_mapping = {
    'my_example': {
        'properties': {
        'doc_id': {'type': 'integer'}, 
        'price': {'type': 'integer'},
         'color': {'type': 'string', 'index': 'not_analyzed'},
         'make': {'type': 'string', 'index': 'not_analyzed'},
         'city': {'type': 'string', 'index': 'not_analyzed'},
         'insurance': {'type': 'string', 'index': 'not_analyzed'},
         'sold': {'type': 'date'}
}}}


#create an index and add the mapping 
if es.indices.exists('my_index_test'):
    es.indices.delete(index="my_index_test")
es.indices.create('my_index_test')

# mapping for the document type 
if es.indices.exists_type(index = 'my_index_test', doc_type = 'my_example'):
    es.indices.delete_mapping(index='my_index_test',doc_type='my_example')
es.indices.put_mapping(index='my_index_test',doc_type='my_example',body=my_mapping)

# indexing 
for xRow in xData:
    es.index(index = 'my_index',
             doc_type= 'my_example',
             id = xRow['doc_id'],
             body = xRow
             )


### MANUALLY CHAINING WORKS 

a = A('terms', field = 'color')
b = A('terms', field = 'make')
c = A('terms', field = 'city')

s1 = dsl_search(es, index = 'my_index', doc_type= 'my_example')
s1.aggs.bucket('xColor', a).bucket('xMake', b).bucket('xCity', c)\
                          .metric('xMyPriceSum', 'sum', field = 'price')\
                          .metric('xMyPriceAvg', 'avg', field = 'price')
resp = s1.execute()
#get results 
q1 = resp.aggregations
q1



#### but not PROGRAMMATICALLY 
# Programmatically chaining 

xVarBuckets = [{'field': 'color', 'label': 'xColor'},
               {'field': 'make',  'label': 'xMake'},
               {'field': 'city',  'label': 'xCity'}]

xVar_Metrics = [{'field': 'price', 'agg_function': 'sum', 'label': 
'xMyPriceSum'},
                {'field': 'price', 'agg_function': 'avg', 'label': 
'xMyPriceAvg'}]


s2 = None 
s2 = dsl_search(es, index = 'my_index', doc_type = 'my_example')

#add buckets    
for xBucketVar in xVarBuckets:
    xAgg = A('terms', field= xBucketVar['field'])
    s2.aggs.bucket(xBucketVar['label'], xAgg)
resp2 = s2.execute()
#get results 
q2 = resp2.aggregations   


I guess it has to do with the fact that the newly create bucket is 
overwritten by the new bucket, but how can append the new bucket to the 
previous one 


Any help appreciated 






-- 
You received this message because you are subscribed to the Google Groups 
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elasticsearch+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elasticsearch/fa471fcf-9ed7-49f9-9e34-4cbefb90abb8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to