-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
178 lines (154 loc) · 3.83 KB
/
index.py
File metadata and controls
178 lines (154 loc) · 3.83 KB
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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
###
# File: index.py
# Project: es-python-example
# File Created: Sunday, 17th November 2019 8:18:37 pm
# Author: Jaseem Jas (jaseem@socialanimal.com)
# -----
# Last Modified: Sunday, 17th November 2019 8:43:34 pm
# Modified By: Jaseem Jas (jaseem@socialanimal.com)
# -----
# Copyright 2016 - 2019 Socialanimal.com
###
from elasticsearch import Elasticsearch
import datetime
es = Elasticsearch()
titles = [
{ "index": { "_index": 'titles', "_id": 1 } },
{
"id": 1,
"title": 'Traditional Marketing Vs Digital Marketing',
"author": 'eCommerce FAQs',
"date": "2019-11-17T11:39:17.376Z"
},
{ "index": { "_index": 'titles', "_id": 2 } },
{
"id": 2,
"title": 'Global Digital Marketing Courses Market 2019 Business Strategies ',
"author": 'Coursera',
"date": "2019-11-17T11:39:17.376Z"
},
{ "index": { "_index": 'titles', "_id": 3 } },
{
"id": 3,
"title": 'Big Data vs Data Warehouse',
"author": 'Igor',
"date": "2019-11-17T11:39:17.376Z"
},
{ "index": { "_index": 'titles', "_id": 4 } },
{
"id": 4,
"title": 'Traditional Marketing Vs Digital Marketing',
"author": 'eCommerce FAQs',
"date": "2019-11-17T11:39:17.376Z"
},
{ "index": { "_index": 'titles', "_id": 5 } },
{
"id": 5,
"title": 'Cloudera Data Platform gives big data users multi-cloud path',
"author": 'Erpinnews',
"date": "2019-11-17T11:39:17.376Z"
},
{ "index": { "_index": 'titles', "_id": 6 } },
{
"id": 6,
"title": 'IoT Event Blog Affinity IoT',
"author": 'infoMegan Davis',
"date": "2019-11-17T11:39:17.376Z"
},
{ "index": { "_index": 'titles', "_id": 7 } },
{
"id": 7,
"title": 'Cloud to cloud backup Solutions Archives',
"author": 'Michael Schneider',
"date": "2019-11-17T11:39:17.376Z"
},
{ "index": { "_index": 'titles', "_id": 8 } },
{
"id": 8,
"title": 'Car hire with car insurance',
"author": 'Gary Hunter',
"date": "2019-11-17T11:39:17.376Z"
},
{ "index": { "_index": 'titles', "_id": 9 } },
{
"id": 9,
"title": 'Fashion Jobs and Fashion Career Advice',
"author": 'Randy C. Marque',
"date": "2019-11-17T11:39:17.376Z"
},
{ "index": { "_index": 'titles', "_id": 10 } },
{
"id": 10,
"title": 'Fashion Designer Zac Posen is Shutting Down his Fashion Label',
"author": 'MARCY OSTER, JTA',
"date": "2019-11-17T11:39:17.376Z"
}
]
def createTitles(body):
try:
resp = es.bulk(index="titles", body=body)
except Exception as e:
print(e)
createTitles(titles)
def countTitles():
try:
resp = es.count(index="titles")
print(resp)
except Exception as e:
print(e)
# countTitles()
def getTitle(id):
try:
resp = es.get(index="titles", id=id)
print(resp)
except Exception as e:
print(e)
getTitle(11)
def updateTitle(body):
try:
resp = es.update(index="titles", id=10, body=body)
print(resp)
except Exception as e:
print(e)
data = {
"doc": {
"title": "My own title",
"author": "JaseemJas",
"date": datetime.datetime.now()
}
}
# updateTitle(data)
def deleteTitle(id):
try:
resp = es.delete(index="titles", id=id)
except Exception as e:
print(e)
# deleteTitle(10)
#
def searchTitles(body):
try:
resp = es.search(index="titles", body=body)
print(resp)
except Exception as e:
print(e)
query = {
"query":{
"match": {
"title": "big data"
}
}
}
# searchTitles(query)
def createTitle(body):
try:
resp = es.create(index="titles", id=11, body=body)
except Exception as e:
print(e)
body = {
"title": "My new title",
"author": "JaseemJas",
"date": datetime.datetime.now()
}
# createTitle(body)