-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecommerce_data.py
182 lines (165 loc) · 3.61 KB
/
ecommerce_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import configparser
config = configparser.ConfigParser()
config.read('config.ini')
from elasticsearch import Elasticsearch
from pprint import pprint
client = Elasticsearch(
cloud_id=config['ELASTIC']['cloud_id'],
basic_auth=(config['ELASTIC']['user'], config['ELASTIC']['password'])
)
"""The following queries delete the documents who have unit prices below 0 and above 500"""
query = {
"query":{
"range":{
"UnitPrice":{
"lte":0
}
}
}
}
query1 = {
"query":{
"range":{
"UnitPrice":{
"gte":500
}
}
}
}
result = client.delete_by_query(index='ecommerce_data',body=query)
"""The following query finds sum of the unitprices. Similarly when sum is replaced by min, max, avg is respective
mathematical operations are carried out"""
query2 = {
"aggs": {
"index_sum": {
"sum": {
"field": "UnitPrice"
}
}
}
}
"""the query below gives all the above mathematical operation in one command"""
query3 = {
"aggs": {
"all_stats_unit_price": {
"stats": {
"field": "UnitPrice"
}
}
}
}
"""The below query gives no of unique customer ids"""
query4 = {
"aggs": {
"unique_orders": {
"cardinality": {
"field": "CustomerID"
}
}
}
}
"""The following query gives you documents whose country is germany with average unit price in that country"""
query5 = {
"query": {
"match": {
"Country": "Germany"
}
},
"aggs": {
"germany_avg_price": {
"avg": {
"field": "UnitPrice"
}
}
}
}
"""The following query gives documents that have 8 hrs difference between invoice dates. The calender interval
property can be replaced with fixed interval which has a smaller time gap. Also desc can be replaced with asc"""
query6 = {
"size": 0,
"aggs": {
"transactions_by_month": {
"date_histogram": {
"field": "InvoiceDate",
"calendar_interval": "1M",
"order": {
"_key": "desc"
}
}
}
}
}
"""The query gives you which has unitprice in intervals of 10 and its counts"""
query7 = {
"aggs": {
"transactions_per_price_interval": {
"histogram": {
"field": "UnitPrice",
"interval": 10
}
}
}
}
"""THe following query gives you unit price counts with specific ranges"""
query8 = {
"aggs": {
"transactions_per_custom_price_interval": {
"range": {
"field": "UnitPrice",
"ranges": [
{
"to": 50
},
{
"from": 50,
"to": 200
},
{
"from": 200
}
]
}
}
}
}
# pprint(client.search(index="ecommerce_data",body=query8))
"""The following query return top 5 customers who have bought most items"""
query9 = {
"aggs": {
"top_5_customers": {
"terms": {
"field": "CustomerID",
"size": 5
}
}
}
}
"""The following query gives us transactions per day from highest to lowest. We calculate daily revenue using
Unit price and quantity and no of unique customers."""
query10 = {
"aggs": {
"transactions_per_day": {
"date_histogram": {
"field": "InvoiceDate",
"calendar_interval": "day",
"order": {
"daily_revenue": "desc"
}
},
"aggs": {
"daily_revenue": {
"sum": {
"script": {
"source": "doc['UnitPrice'].value * doc['Quantity'].value"
}
}
},
"number_of_unique_customers_all_day":{
"cardinality": {
"field": "CustomerID"
}
}
}
}
}
}