/
queryBuilder.py
214 lines (184 loc) · 6.93 KB
/
queryBuilder.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
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q, A
import re
import datetime
import pprint
'''
in this file are functions used to build elasticsearch queries
'''
es = Elasticsearch(['localhost'], http_auth=('elastic', 'changeme'), port=9200)
global sortedresult
sortedresult = []
# do general search
# input: jobtitle, description, state, city, jobtype, salary, date
# output: list of results
def generalSearch(search):
global sortedresult
print search
search['offset'] = int(search['offset'])
if search['sort_by_date'] and search['offset'] > 0 and len(sortedresult) > 0:
print sortedresult
return sortedresult[search['offset']: search['offset'] + 10]
s = Search(using=es)
s = s.index('job_index')
s = s.query(Q('match_all'))
# title
if search.has_key('jobtitle'):
s = s.query('multi_match', query = search['jobtitle'], type = 'cross_fields', fields = ['title', 'summary'], operator = 'and')
# job description
if search.has_key('description') or search.has_key('jobtitle'):
summary = ""
if search.has_key('jobtitle'):
summary += search["jobtitle"]
if search.has_key('description'):
summary += " " + search['description']
else:
summary = search['description']
s = s.query('match', summary = summary)
# company
if search.has_key('company'):
s = s.query('match', company=search['company'])
# location
if search.has_key('state'):
s = s.query('match_phrase', state = search['state'])
if search.has_key('city'):
s = s.query('match', city = search['city'])
# jobtype
if search.has_key('type'):
s = s.query('match', jobtype=search['type'])
# salary
if search.has_key('salary'):
search['salary'] = int(search['salary'])
s = s.query('range', salary = {'gte': search['salary']})
# date
if search.has_key('date'):
days = re.findall(r"(\d+)", search['date'])[0]
days = int(days)
today = datetime.datetime.now().toordinal()
s = s.query('range', date = {'gte': today - days})
pp = pprint.PrettyPrinter(depth = 6)
pp.pprint(s.to_dict())
if search['sort_by_date']:
s = s[0: 3000]
response = s.execute()
resultlist = []
print response.hits.total
print len(response.hits)
for hit in response.hits:
result = {}
result['id'] = hit.meta.id
result['score'] = hit.meta.score
result['title'] = hit['title']
result['summary'] = hit['summary'][:180]
result['url'] = 'www.indeed.com' + hit['url']
result['company'] = hit['company']
result['location'] = hit['location']
result['postingdate'] = str(datetime.datetime.fromordinal(hit['date']))
resultlist.append(result)
sortedresult = sorted(resultlist, key=lambda d : d['postingdate'], reverse = 1)
return sortedresult[search['offset']: search['offset']+10]
else:
s = s[search['offset']: search['offset']+10]
response = s.execute()
resultlist = []
print response.hits.total
for hit in response.hits:
result = {}
result['id'] = hit.meta.id
result['score'] = hit.meta.score
result['title'] = hit['title']
result['summary'] = hit['summary'][:180]
result['url'] = 'www.indeed.com' + hit['url']
result['company'] = hit['company']
result['location'] = hit['location']
result['postingdate'] = str(datetime.datetime.fromordinal(hit['date']))
resultlist.append(result)
return resultlist
# search for the jobs posted by the company
def companySearch(search):
s = Search(using=es)
search['offset'] = int(search['offset'])
s = s.index('job_index')
s = s.query('match_phrase', company=search['company'])
s = s[search['offset']: search['offset'] +10]
response = s.execute()
resultlist = []
print response.hits.total
for hit in response.hits:
result = {}
result['id'] = hit.meta.id
result['score'] = hit.meta.score
result['title'] = hit['title']
result['summary'] = hit['summary'][:180]
result['url'] = 'www.indeed.com' + hit['url']
result['company'] = hit['company']
result['location'] = hit['location']
result['postingdate'] = str(datetime.datetime.fromordinal(hit['date']))
resultlist.append(result)
return resultlist
# recommend jobs based on user's resume
def recommendationSearch(search):
s = Search(using=es)
s = s.index('job_index')
search['offset'] = int(search['offset'])
condition = []
#location
if search.has_key('state'):
qState = Q('match_phrase', state=search['state'])
condition.append(qState)
if search.has_key('city'):
qCity = Q('match', city=search['city'])
condition.append(qCity)
# professional & education background
if search.has_key('pbg') or search.has_key('degree') or search.has_key('major'):
qBG = Q('multi_match', query=search['pbg'] + ' ' + str(search['degree']) + " " + str(search['major']), type='cross_fields', fields=['title', 'summary'])
condition.append(qBG)
# jobtype
if search.has_key('type'):
qType = Q('match', jobtype=search['type'])
condition.append(qType)
# salary
if search.has_key('salary'):
search['salary'] = int(search['salary'])
qSalary = Q('range', salary={'gte': search['salary']})
condition.append(qSalary)
q = Q('bool', should = condition, minimum_should_match = 1)
s = s.query(q)
s = s[search['offset'] : search['offset'] + 10]
pp = pprint.PrettyPrinter(depth=6)
pp.pprint(s.to_dict())
response = s.execute()
resultlist = []
print response.hits.total
for hit in response.hits:
result = {}
result['id'] = hit.meta.id
result['score'] = hit.meta.score
result['title'] = hit['title']
result['summary'] = hit['summary'][:180]
result['url'] = 'www.indeed.com' + hit['url']
result['company'] = hit['company']
result['location'] = hit['location']
result['postingdate'] = str(datetime.datetime.fromordinal(hit['date']))
resultlist.append(result)
return resultlist
# return one job's detail information based on its id
def jobdetail(id):
s = Search(using=es)
s = s.index('job_index')
s = s.filter('term', _id=id)
ret = s.execute()
hit = ret.hits[0].to_dict()
job = {}
job['id'] = id
job['title'] = hit['title']
job['summary'] = hit['summary']
job['url'] = 'www.indeed.com' + hit['url']
job['company'] = hit['company']
job['location'] = hit['location']
if hit['salary'] == '':
job['salary'] = 'Unknown'
else:
job['salary'] = hit['salary']
job['jobtype'] = hit['jobtype']
return job