-
Notifications
You must be signed in to change notification settings - Fork 0
/
zipcodes_US.py
38 lines (31 loc) · 898 Bytes
/
zipcodes_US.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
import fsphinx
import sphinxapi
from sgeo.config import db
# create sphinx client
cl = fsphinx.FSphinxClient()
# connect to searchd
cl.SetServer('localhost', 9312)
# matching mode (faceted client must be SPH_MATCH_EXTENDED2)
cl.SetMatchMode(sphinxapi.SPH_MATCH_EXTENDED2)
# sorting and possible custom sorting function
cl.SetSortMode(sphinxapi.SPH_SORT_EXTENDED, '@relevance desc')
## sql query to fetch the hits
db_fetch = fsphinx.DBFetch(db, sql = '''
select
id,
"zipcodesdotcom" as source_db,
ZipCode as zip,
City as place,
State as state,
State as state_code,
'United States' as country,
'us' as country_code,
Latitude*PI()/180 as lat,
Longitude*PI()/180 as lon
from zipcodesdotcom.zipcodes
where id in ($id)
order by field(id, $id)'''
)
cl.AttachDBFetch(db_fetch)
# by default only this index will be queried
cl.SetDefaultIndex('zipcodes_US')