Elaborate search endpoint

This commit is contained in:
Alexey Zakharenkov 2020-12-30 18:44:18 +03:00
parent d4429c3a86
commit 4642948638
3 changed files with 16 additions and 11 deletions

View file

@ -218,17 +218,22 @@ def get_server_configuration():
mwm_size_thr=config.MWM_SIZE_THRESHOLD)
def prepare_sql_search_string(string):
if string.startswith('^'):
string = string[1:]
else:
string = f"%{string}"
if string.endswith('$'):
string = string[:-1]
else:
string = f"{string}%"
return string
@app.route('/search')
def search():
query = request.args.get('q')
if query.startswith('^'):
query = query[1:]
else:
query = f"%{query}"
if query.endswith('$'):
query = query[:-1]
else:
query = f"{query}%"
sql_search_string = prepare_sql_search_string(query)
with g.conn.cursor() as cursor:
cursor.execute(f"""
@ -236,7 +241,7 @@ def search():
FROM {config.BORDERS_TABLE}
WHERE name ILIKE %s
ORDER BY (ST_Area(geography(geom)))
LIMIT 1""", (query,)
LIMIT 1""", (sql_search_string,)
)
if cursor.rowcount > 0:
rec = cursor.fetchone()

View file

@ -43,7 +43,7 @@ def create_countries_initial_structure(conn):
with conn.cursor() as cursor:
# TODO: process overlapping countries, like Ukraine and Russia with common Crimea
cursor.execute(f"""
SELECT osm_id, name
SELECT osm_id
FROM {osm_table}
WHERE admin_level = 2
"""

View file

@ -210,7 +210,7 @@
<span id="wait_start_over">ожидайте...</span>
</div>
<div id="search">
Поиск <input type="text" id="fsearch">
Поиск <input type="text" id="fsearch" placeholder="Use ^/$ for start/end">
<button id="b_search" onclick="doSearch()">&#x1f50d;</button>
</div>
</div>