-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.py
373 lines (299 loc) · 8.53 KB
/
script.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# import these at the begining...
import git
import click
import datetime
import json
from utils import parse_commits, print_hits, print_hit, print_search_stats, pp
from elasticsearch.exceptions import TransportError
##
import logging
tracer = logging.getLogger("elasticsearch.trace")
tracer.setLevel(logging.INFO)
tracer.addHandler(logging.FileHandler("/tmp/es_trace.log"))
from elasticsearch import Elasticsearch
client = Elasticsearch()
# lets not repeat ourselves
user_mapping = {
"properties": {"name": {"type": "text", "fields": {"keyword": {"type": "keyword"}}}}
}
create_index_body = {
"settings": {
# just one shard, no replicas for testing
"number_of_shards": 1,
"number_of_replicas": 0,
# custom analyzer for analyzing file paths
"analysis": {
"analyzer": {
"file_path": {
"type": "custom",
"tokenizer": "path_hierarchy",
"filter": ["lowercase"],
}
}
},
},
"mappings": {
"properties": {
"repository": {"type": "keyword"},
"author": user_mapping,
"authored_date": {"type": "date"},
"committer": user_mapping,
"committed_date": {"type": "date"},
"parent_shas": {"type": "keyword"},
"description": {"type": "text", "analyzer": "snowball"},
"files": {"type": "text", "analyzer": "file_path", "fielddata": True},
}
},
}
client.indices.create(index="git", body=create_index_body)
from elasticsearch.helpers import bulk, streaming_bulk
repo = git.Repo("/elasticsearch-py")
for ok, result in streaming_bulk(
client,
parse_commits(repo.refs.master.commit, "elasticsearch-py"),
index="git",
chunk_size=50,
):
action, result = result.popitem()
_id = result["_id"]
doc_id = f"/git/_doc/{_id}"
# process the information from ES whether the document has been
# successfully indexed
if not ok:
print(f"Failed to {action} document {doc_id}: {result}")
else:
print(doc_id)
UPDATES = [
{
"_type": "_doc",
"_id": "20fbba1230cabbc0f4644f917c6c2be52b8a63e8",
"_op_type": "update",
"doc": {"initial_commit": True},
},
{
"_type": "_doc",
"_id": "ae0073c8ca7e24d237ffd56fba495ed409081bf4",
"_op_type": "update",
"doc": {"release": "5.0.0"},
},
]
success, _ = bulk(client, UPDATES, index="git")
client.indices.refresh(index="git")
initial_commit = client.get(index="git", id="20fbba1230cabbc0f4644f917c6c2be52b8a63e8")
# and now we can count the documents
print(client.count(index="git")["count"], "documents in index")
import csv
with open("cars.csv") as csvfile:
reader = csv.DictReader(csvfile, delimiter=";")
ret = bulk(client, reader, index="cars")
result = client.search(
index="git",
body={
"query": {
"bool": {
"must": {"match": {"description": "fix"}},
"must_not": {"term": {"files": "test_elasticsearch"}},
}
}
},
)
print_hits(result)
from elasticsearch.helpers import scan
results = scan(
client,
query={
"query": {
"bool": {
"must": {"match": {"description": "fix"}},
"must_not": {"term": {"files": "test_elasticsearch"}},
}
}
},
index="git",
)
for result in results:
print_hit(result)
result = client.search(
index="git",
body={
"size": 0,
"aggs": {
"committers": {
"terms": {"field": "committer.name.keyword"},
"aggs": {"line_stats": {"stats": {"field": "stats.lines"}}},
}
},
},
)
print_search_stats(result)
for committer in result["aggregations"]["committers"]["buckets"]:
print(
"%15s: %3d commits changing %6d lines"
% (committer["key"], committer["doc_count"], committer["line_stats"]["sum"])
)
print("=" * 80)
for key, value in client.cluster.health().items():
print(f"\t{key}: {value}")
for key, value in client.cluster.health(index="git").items():
print(f"\t{key}: {value}")
results = client.cat.health(
format="json", h="timestamp,status,node.total,active_shards_percent"
)
print(results)
result = client.xpack.sql.query(
body={
"query": "SELECT description,authored_date FROM git WHERE author.name='Nick Lang' LIMIT 5"
}
)
columns = result["columns"]
rows = result["rows"]
print("=" * 80)
print("Authored Date \t| Description")
print("=" * 80)
for row in rows:
print(f"{row[1]}\t| {row[0]}")
print("=" * 80)
result = client.xpack.sql.translate(
body={
"query": "SELECT description,authored_date FROM git WHERE author.name='Nick Lang' LIMIT 5"
}
)
print("=" * 80)
print("Translate our SQL query to Elasticearch DSL query syntax")
print("=" * 80)
print("")
print(json.dumps(result, indent=2))
######################
from elasticsearch_dsl import Q, Search
"""
Straightforward mapping to json - kwargs are translated into keys into json.
You can use the to_dict() method to see the result json.
"""
q = Q("terms", tags=["python", "search"])
q.to_dict()
"""
All objects can also be constructed using the raw dict.
"""
q = Q({"terms": {"tags": ["python", "search"]}})
q.to_dict()
"""
Query objects support logical operators which result in bool queries
"""
q = q | Q("match", title="python")
q.to_dict()
"""
DSL objects also allow for attribute access instead of ['key']
"""
q.minimum_should_match = 2
q.minimum_should_match
q.to_dict()
from datetime import date
q = q & Q("range", **{"@timestamp": {"lt": date(2019, 1, 1)}})
q.to_dict()
"""
Configuration is global so no client needs to be passed around.
"""
from elasticsearch_dsl import connections
"""
Default connection used where no other connection specified. Any configuration
methods just pass all parameters to the underlying elasticsearch-py client.
"""
connections.create_connection(hosts=["localhost"])
"""
Optionally specify an alias for the connection in case of multiple connections.
"""
connections.create_connection("prod", hosts=["localhost"])
s = Search(using="prod")
s.count()
"""
You can always just pass in your own client instance
"""
s = Search(using=Elasticsearch())
s.count()
"""
Any method on Search returns a clone so you need to always assign it back to
the same variable.
"""
s = Search()
s = s.params(q="fix")
"""
Multiple queries are combined together using the AND operator
"""
s = Search()
s = s.query("match", description="fix")
s = s.query("match", author="Honza")
"""
Filter shortcut to use {bool: {filter: []}}
"""
s = s.filter("range", committed_date={"lt": date(2016, 1, 1)})
s.to_dict()
"""
Exclude as a wrapper around must_not, use __ instead of dots for convenience.
"""
s = s.exclude("term", committer__name__keyword="Honza Král")
"""
Search is executed when iterated on or when .execute() is called.
"""
for hit in s:
"""
Hit class offers direct access to fields and via .meta any other properties
on the returned hit (_id, _seq_no, ...)
"""
print(f"{hit.meta.id[:6]} ({hit.author.name}): {hit.description[:50]}")
"""
Aggregations are implemented in place to allow for chaining
"""
s = Search(index="git")
s.aggs.bucket("tags", "terms", field="terms").metric(
"lines", "sum", field="stats.lines"
).metric("authors", "cardinality", field="author.name.keyword")
r = s.execute()
"""
Or modify aggregation in place
"""
s.aggs["tags"].bucket(
"months", "date_histogram", field="committed_date", interval="month"
)
"""
Analysis
"""
from elasticsearch_dsl import analyzer, token_filter
a = analyzer(
"file_analyzer",
tokenizer="path_hierarchy",
filter=[
"lowercase",
token_filter(
"split_ext",
"pattern_capture",
preserve_original=True,
patterns=[r"^([^\.]+)"],
),
],
)
a.simulate("test/integration/search.py")
"""
"""
from elasticsearch_dsl import Document, Text, Keyword, InnerDoc, Date, Nested
class FileDiff(InnerDoc):
filename = Text(analyzer=a)
patch = Text()
class Commit(Document):
description = Text()
committed_date = Date()
author = Text(fields={"keyword": Keyword()})
files = Nested(FileDiff)
def subject(self):
return self.description.split("\n", 1)[0][:80]
class Index:
name = "git*"
settings = {"number_of_replicas": 0}
"""
Create the index
"""
Commit.init(index="git-v2")
"""
Search now returns Commit objects
"""
for c in Commit.search():
print(f"{c.meta.id}: {c.subject()}")