-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticsearch.py
241 lines (196 loc) · 5.52 KB
/
elasticsearch.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
'''
Created on Feb 6, 2012
@author: yoav
Basic POC methods skeleton for elasticsearch
'''
import pyes # http://pypi.python.org/pypi/pyes/
import csv
import subprocess # only for temporary dirty hack
import json
######## configuration parameters ########
SERVER = '107.22.137.196'
PORT = '9200'
RAW_DATA_FILE = 'artist-test.tsv'
INDEX_NAME = 'artist'
DOC_TYPE = 'artist-test-doc-type'
SETUP_SCRIPT = './setup_artist.sh'
# Index SETTINGS -- Used to configure analysers, etc... (es.update_settings)
SETTINGS = ''
#SETTINGS = {
# u'analysis': {
# u'filter': {
# 'title_ngrams': {
# 'side': 'front',
# 'max_gram': 20,
# 'min_gram': 1,
# 'type': 'edgeNGram'
# }
# },
# u'analyzer': {
# 'full_title': {
# 'type': 'custom',
# 'filter': [
# 'standard',
# 'lowercase',
# 'asciifolding'
# ],
# 'tokenizer': 'standard'
# },
# 'partial_title': {
# 'type': 'custom',
# 'filter': [
# 'standard',
# 'lowercase',
# 'asciifolding',
# 'title_ngrams'
# ],
# 'tokenizer': 'standard'
# }
# }
# }
#}
# TODO: modify the mapping to represent the current schema
MAPPING = '' # See setup_artist.sh for mapping info
#{
# u'id' : {
# u'store': u'yes',
# u'type': u'string',
# },
# u'title': {
# u'boost': 1.0,
# u'type': u'multi_field',
# u'fields': {
# u'partial': {
# u'type': u'string',
# u'search_analyzer': u'full_phrase',
# u'index_analyzer': u'partial_phrase'
# },
# u'title': {
# u'type': u'string',
# u'analyzer': u'full_phrase'
# }
# }
# },
# u'context' : {
# u'index': u'analyzed',
# u'store': u'yes',
# u'type': u'string'
# },
# u'weight' : {
# u'store': u'yes',
# u'type': u'float'
# },
# u'disambig_number' : {
# u'store': u'yes',
# u'type': u'integer'
# },
# u'aliases' : {
# u'index': u'analyzed',
# u'store': u'yes',
# u'type': u'string'
# }
# 'type' conflicts with internal ES 'type' field -- if you want to handle this field specially, the name needs changing
# u'type' : {
# 'store': 'yes',
# 'type': 'string'
# },
#}
###########################################
######## helper functions ########
def deleteIndex(conn, indexName):
try:
conn.delete_index(indexName)
print "deleting " + indexName
return True
except:
return False
def createIndex(conn, indexName, deleteIfExists=False):
#if deleteIfExists:
# deleteIndex(conn, indexName)
print "creating " + indexName
subprocess.call([SETUP_SCRIPT])
#conn.create_index(indexName)
def createMapping(conn, indexName, docType, mapping):
conn.put_mapping(docType, mapping, [indexName])
def updateSettings(conn, indexName, settings):
conn.update_settings(indexName, settings)
def init(indexName, docType, mapping, settings):
conn = pyes.ES(['%s:%s' % (SERVER, PORT)])
createIndex(conn, indexName, deleteIfExists=False)
#createMapping(conn, indexName, docType, mapping)
#updateSettings(conn, indexName, settings)
return conn
def printResult(rs):
print "==================================="
print "Query: %(query)s" % {"query":rs.query}
print "-----------------------------------"
print "Found %(total)d results" % {"total":rs.total}
print "-----------------------------------"
print "Facets: %(facets)r" % {"facets":rs.facets}
print "==================================="
for result in rs:
print result
print "..................................."
print
print
##################################
######## indexing and search functions ########
def index(conn, csvDataFile, indexName, docType):
f = open(csvDataFile)
dialect = csv.Sniffer().sniff(f.read(1024))
f.seek(0)
reader = csv.DictReader(f, dialect=dialect)
for i in reader:
conn.index(i, indexName, docType)
def search(conn, query):
#q = pyes.TermQuery("title", query)
q = pyes.TermQuery("title", query)
#q = pyes.Query()
print q.serialize()
result = conn.search(query=q)
print result
def exactMatchSearch(conn, query):
q = pyes.TextQuery("title", query, 'phrase')
s = q.search() # get a search object
s.facet.add_term_facet('type')
printResult(conn.search(query=s))
def prefixingSearch(conn, query):
q = pyes.TextQuery("title", query, 'phrase_prefix')
#s = q.search(start=5, size=10, sort={"type":"desc"}) # example of pagination + sorting
s = q.search()
s.facet.add_term_facet('type')
printResult(conn.search(query=s))
def ngramPrefixingSearch(conn, query):
xq = pyes.TextQuery("title", query);
pq = pyes.TextQuery("title.partial", query)
q = pyes.BoolQuery()
q.add_should(xq).add_should(pq)
s = q.search()
s.facet.add_term_facet('type')
printResult(conn.search(query=s))
def interactiveQuery(conn, queryFunc):
while True:
q = raw_input('--> ')
queryFunc(conn, q)
def chooseSearch():
print ""
print "Choose search type?"
print "==================="
print "1. Exact Match"
print "2. Prefix Match"
print "3. Prefix Match using Ngrams"
choice = raw_input('>> ')
if (choice=='1'): return exactMatchSearch
elif (choice=='2'): return prefixingSearch
elif (choice=='3'): return ngramPrefixingSearch
else: return chooseSearch()
###############################################
######## main ########
if __name__ == '__main__':
print "For now, create the index and custom analyzers first using setup_artist.sh"
conn = init(INDEX_NAME, DOC_TYPE, MAPPING, SETTINGS)
index(conn, RAW_DATA_FILE, INDEX_NAME, DOC_TYPE )
conn.flush()
queryFunc = chooseSearch()
interactiveQuery(conn, queryFunc)
######################