Skip to content

Commit 17021e9

Browse files
committed
Calculate search rank on import
1 parent 535c09c commit 17021e9

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

importer/src/hierarchyitem.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ HierarchyItem::HierarchyItem(const pqxx::row &row)
2525
m_latitude = row["latitude"].as<float>(0);
2626
m_longitude = row["longitude"].as<float>(0);
2727
m_osm_id = row["osm_id"].as<uint64_t>(0);
28+
m_search_rank = row["search_rank"].as<int>(0);
2829

2930
m_data_name = parse_to_map(row["name"].as<std::string>(""));
3031
m_data_extra = parse_to_map(row["extra"].as<std::string>(""));
@@ -243,11 +244,13 @@ void HierarchyItem::write(sqlite3pp::database &db) const
243244
std::string website = get_with_def(m_data_extra, "website");
244245

245246
{
246-
sqlite3pp::command cmd(db, "INSERT INTO object_primary_tmp (id, postgres_id, name, name_extra, "
247-
"name_en, phone, postal_code, website, parent, longitude, "
248-
"latitude) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
247+
sqlite3pp::command cmd(db,
248+
"INSERT INTO object_primary_tmp (id, postgres_id, name, name_extra, "
249+
"name_en, phone, postal_code, website, parent, longitude, "
250+
"latitude, search_rank) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
249251
cmd.binder() << m_my_index << (int)m_id << m_name << m_name_extra << name_en << phone
250-
<< m_postcode << website << m_parent_index << m_longitude << m_latitude;
252+
<< m_postcode << website << m_parent_index << m_longitude << m_latitude
253+
<< m_search_rank;
251254
if (cmd.execute() != SQLITE_OK)
252255
std::cerr << "WriteSQL : error inserting primary data for " << m_id << ", " << m_my_index
253256
<< "\n";

importer/src/hierarchyitem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class HierarchyItem
6868
std::string m_housenumber;
6969
std::string m_name;
7070
std::string m_name_extra;
71+
int m_search_rank;
7172

7273
std::map<std::string, std::string> m_data_name;
7374
std::map<std::string, std::string> m_data_extra;

importer/src/main.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,17 @@ int main(int argc, char *argv[])
159159

160160
const std::string base_query
161161
= R"SQL(
162-
select pl.place_id, linked_place_id, parent_place_resolved as parent_place_id, country_code, class, type,
162+
select pl.place_id, linked_place_id, parent_place_resolved as parent_place_id, pl.country_code, class, type,
163163
hstore_to_json(name) as name, hstore_to_json(extratags) as extra,
164164
COALESCE(address->'housenumber',housenumber) AS housenumber,
165-
postcode, ST_X(centroid) as longitude, ST_Y(centroid) as latitude,
165+
postcode, ST_X(pl.centroid) as longitude, ST_Y(pl.centroid) as latitude,
166+
CASE
167+
WHEN sname.importance IS NOT NULL THEN (100*(1-sname.importance))::int
168+
ELSE 100 + pl.rank_search
169+
END AS search_rank,
166170
osm_type, osm_id
167171
from placex pl
172+
left join search_name as sname on sname.place_id=pl.place_id
168173
left join lateral
169174
(select address_place_id from place_addressline a where a.place_id=pl.place_id
170175
order by cached_rank_address desc, fromarea desc, distance asc limit 1) as a on true
@@ -238,6 +243,7 @@ select place_id as parent_place_resolved from prec where linked_place_id is null
238243
select pc.place_id, NULL as linked_place_id, parent_place_resolved as parent_place_id, country_code, 'postal' as class, 'code' as type,
239244
NULL as name, NULL as extra, NULL as housenumber,
240245
postcode, ST_X(geometry) as longitude, ST_Y(geometry) as latitude,
246+
100 AS search_rank,
241247
NULL as osm_type, NULL as osm_id
242248
from location_postcode pc
243249
left join lateral
@@ -306,7 +312,7 @@ select place_id as parent_place_resolved from prec where linked_place_id is null
306312
for (std::string country : hierarchy.get_root_countries())
307313
{
308314
for (auto row : txn.exec_params(
309-
base_query + "where rank_address = 4 and country_code = $1 limit 1", country))
315+
base_query + "where pl.rank_address = 4 and pl.country_code = $1 limit 1", country))
310316
{
311317
hindex id = row["place_id"].as<hindex>(0);
312318
if (!hierarchy.has_item(id))
@@ -364,6 +370,7 @@ select place_id as parent_place_resolved from prec where linked_place_id is null
364370
db.execute("CREATE " TEMPORARY " TABLE object_primary_tmp ("
365371
"id INTEGER PRIMARY KEY AUTOINCREMENT, postgres_id INTEGER, name TEXT, name_extra "
366372
"TEXT, name_en TEXT, phone TEXT, postal_code TEXT, website TEXT, parent INTEGER, "
373+
"search_rank INTEGER, "
367374
"latitude REAL, longitude REAL)");
368375
db.execute("CREATE " TEMPORARY " TABLE object_type_tmp (prim_id INTEGER, type TEXT NOT NULL, "
369376
"FOREIGN KEY (prim_id) REFERENCES objects_primary_tmp(id))");
@@ -385,13 +392,14 @@ select place_id as parent_place_resolved from prec where linked_place_id is null
385392
db.execute("CREATE " TEMPORARY
386393
" TABLE object_primary_tmp2 (id INTEGER PRIMARY KEY AUTOINCREMENT, "
387394
"name TEXT, name_extra TEXT, name_en TEXT, phone TEXT, postal_code TEXT, website "
388-
"TEXT, parent INTEGER, type_id INTEGER, latitude REAL, longitude REAL, boxstr TEXT, "
395+
"TEXT, parent INTEGER, type_id INTEGER, latitude REAL, longitude REAL, "
396+
"search_rank INTEGER, boxstr TEXT, "
389397
"FOREIGN KEY (type_id) REFERENCES type(id))");
390398

391399
db.execute("INSERT INTO object_primary_tmp2 (id, name, name_extra, name_en, phone, postal_code, "
392-
"website, parent, type_id, latitude, longitude, boxstr) "
400+
"website, parent, type_id, latitude, longitude, search_rank, boxstr) "
393401
"SELECT p.id, p.name, p.name_extra, p.name_en, p.phone, p.postal_code, p.website, "
394-
"p.parent, type.id, p.latitude, p.longitude, "
402+
"p.parent, type.id, p.latitude, p.longitude, p.search_rank, "
395403
// LINE BELOW DETERMINES ROUNDING USED FOR BOXES
396404
"CAST(CAST(p.latitude*100 AS INTEGER) AS TEXT) || ',' || CAST(CAST(p.longitude*100 AS "
397405
"INTEGER) AS TEXT) "
@@ -404,13 +412,15 @@ select place_id as parent_place_resolved from prec where linked_place_id is null
404412

405413
db.execute("CREATE TABLE object_primary (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, "
406414
"name_extra TEXT, name_en TEXT, phone TEXT, postal_code TEXT, website TEXT, "
407-
"parent INTEGER, type_id INTEGER, latitude REAL, longitude REAL, box_id INTEGER, "
415+
"parent INTEGER, type_id INTEGER, latitude REAL, longitude REAL, search_rank INTEGER, "
416+
"box_id INTEGER, "
408417
"FOREIGN KEY (type_id) REFERENCES type(id))");
409418
db.execute(
410419
"INSERT INTO object_primary (id, name, name_extra, name_en, phone, postal_code, website, "
411-
"parent, type_id, latitude, longitude, box_id) "
420+
"parent, type_id, latitude, longitude, search_rank, box_id) "
412421
"SELECT o.id, name, name_extra, name_en, phone, postal_code, website, parent, type_id, "
413-
"latitude, longitude, b.id FROM object_primary_tmp2 o JOIN boxids b ON o.boxstr=b.boxstr");
422+
"latitude, longitude, search_rank, b.id FROM object_primary_tmp2 o JOIN boxids b ON "
423+
"o.boxstr=b.boxstr");
414424

415425
db.execute("DROP INDEX IF EXISTS idx_object_primary_box");
416426
db.execute("CREATE INDEX idx_object_primary_box ON object_primary (box_id)");

0 commit comments

Comments
 (0)