This repository was archived by the owner on Feb 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsof_es_poster.py
More file actions
executable file
·136 lines (129 loc) · 3.33 KB
/
sof_es_poster.py
File metadata and controls
executable file
·136 lines (129 loc) · 3.33 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
#!/usr/bin/python
from pprint import pprint
from pyes import *
import csv
import string
# for the sample we want to clear the index before indexing again
conn = ES('http://127.0.0.1:9200')
try:
conn.indices.delete_index("sof-sample")
except:
pass
conn.indices.create_index("sof-sample")
# create document mapping
mapping = {
'post_id': {
'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': 'string'},
'post_creation_date': {
'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': 'string'},
'owner_user_id': {
'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': 'string'},
'owner_creation_date': {
'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': 'string'},
'reputation_at_post_creation': {
'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': 'string'},
'owner_undeleted_answer_count_at_post_time': {
'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': 'string'},
'title': {
'boost': 1.0,
'index': 'analyzed',
'store': 'yes',
'type': 'string',
'term_vector': 'yes',},
'body': {
'boost': 1.0,
'index': 'analyzed',
'store': 'yes',
'type': 'string',
'term_vector':'yes'},
'tag_1': {
'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': 'string',
'term_vector': 'yes',},
'tag_2': {
'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': 'string',
'term_vector': 'yes',},
'tag_3': {
'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': 'string',
'term_vector': 'yes',},
'tag_4': {
'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': 'string',
'term_vector': 'yes',},
'tag_5': {
'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': 'string',
'term_vector': 'yes',},
'post_closed_date': {
'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': 'string'},
'open_status': {
'boost': 1.0,
'index': 'not_analyzed',
'store': 'yes',
'type': 'string'}}
conn.indices.put_mapping("sof-document", {'properties':mapping}, ["sof-sample"])
# index documents
counter = 1
with open('train-sample.csv', 'rt') as csvfile:
sofreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in sofreader:
[post_id, post_creation_date, owner_user_id, owner_creation_date, reputation_at_post_creation, owner_undeleted_answer_count_at_post_time, title, body, tag_1, tag_2, tag_3, tag_4, tag_5, post_closed_date, open_status] = row;
document = {
"post_id": post_id,
"post_creation_date": post_creation_date,
"owner_user_id": owner_user_id,
"owner_creation_date" : owner_creation_date,
"reputation_at_post_creation": reputation_at_post_creation,
"owner_undeleted_answer_count_at_post_time": owner_undeleted_answer_count_at_post_time,
"title": title,
"body": body,
"tag_1": tag_1,
"tag_2": tag_2,
"tag_3": tag_3,
"tag_4": tag_4,
"tag_5": tag_5,
"post_closed_date": post_closed_date,
"open_status": open_status}
print "Indexing"
print counter
print post_id
conn.index(document, "sof-sample", "sof-document", counter)
counter = counter + 1
conn.indices.refresh("sof-sample")
q = TermQuery("title", "Elastic Search")
results = conn.search(query = q)
for r in results:
pprint(r)