-
Notifications
You must be signed in to change notification settings - Fork 1
/
temp_add_customer.py
130 lines (119 loc) · 3.65 KB
/
temp_add_customer.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
# -*- coding: utf-8 -*-
from elasticsearch import Elasticsearch, helpers
from elasticsearch_dsl import Search
from config import config
if __name__ == '__main__':
app_config = config['default']
indice = 'rastarockets_customers'
customers = [
{
'Name': 'BNP PARIBAS'
},
{
'Name': 'BNP NTM'
},
{
'Name': 'ORANGE'
},
{
'Name': 'SOCIÉTÉ GÉNÉRALE'
},
{
'Name': 'BPCE'
},
{
'Name': 'EDF'
},
{
'Name': 'CRÉDIT AGRICOLE'
},
{
'Name': 'THALES'
},
{
'Name': 'SNCF'
},
{
'Name': 'TELEFONICA'
},
{
'Name': 'ALLIANZ'
}
]
client = Elasticsearch([{'host': app_config.ELS_HOST, 'port': app_config.ELS_PORT}])
if not client.indices.exists(indice):
client.indices.create(
index=indice,
body={
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0,
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
},
"mappings": {
"customer": {
"properties": {
"Name": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
}
}
},
'contact': {
'properties': {
'Customer': {
'type': 'text'
},
'Name': {
'type': 'text',
'analyzer': 'autocomplete',
'search_analyzer': 'standard'
},
'Email': {
'type': 'text',
'analyzer': 'autocomplete',
'search_analyzer': 'standard'
}
}
}
}
}
)
bulk_commands = []
for customer in customers:
search = Search(
using=client,
index=indice
).query("match_phrase", Name=customer['Name'])
response = search.execute()
if response.hits.total == 0:
print('{0} added to bulk'.format(customer['Name']))
bulk_commands.append({
'_type': 'customer',
'_index': indice,
'_source': customer
})
else:
print('{0} already exist'.format(customer['Name']))
if len(bulk_commands) > 0:
helpers.bulk(client, bulk_commands)
print('{0} customer(s) added'.format(len(bulk_commands)))