mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-03-11 13:24:07 +00:00
Added --no-replace command for special phrases importation and added corresponding tests
This commit is contained in:
@@ -27,6 +27,8 @@ class ImportSpecialPhrases:
|
||||
help='Import special phrases from the OSM wiki to the database.')
|
||||
group.add_argument('--import-from-csv', metavar='FILE',
|
||||
help='Import special phrases from a CSV file.')
|
||||
group.add_argument('--no-replace', action='store_true',
|
||||
help='Keep the old phrases and only add the new ones.')
|
||||
|
||||
@staticmethod
|
||||
def run(args):
|
||||
@@ -51,7 +53,8 @@ class ImportSpecialPhrases:
|
||||
from ..tokenizer import factory as tokenizer_factory
|
||||
|
||||
tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
|
||||
should_replace = not args.no_replace
|
||||
with connect(args.config.get_libpq_dsn()) as db_connection:
|
||||
SPImporter(
|
||||
args.config, args.phplib_dir, db_connection, loader
|
||||
).import_phrases(tokenizer)
|
||||
).import_phrases(tokenizer, should_replace)
|
||||
|
||||
@@ -306,7 +306,7 @@ class LegacyICUNameAnalyzer:
|
||||
# WHERE word_id is null and type = 'postcode'""")
|
||||
|
||||
|
||||
def update_special_phrases(self, phrases):
|
||||
def update_special_phrases(self, phrases, should_replace):
|
||||
""" Replace the search index for special phrases with the new phrases.
|
||||
"""
|
||||
norm_phrases = set(((self.normalize(p[0]), p[1], p[2], p[3])
|
||||
@@ -345,7 +345,7 @@ class LegacyICUNameAnalyzer:
|
||||
columns=['word', 'word_token', 'class', 'type',
|
||||
'operator', 'search_name_count'])
|
||||
|
||||
if to_delete:
|
||||
if to_delete and should_replace:
|
||||
psycopg2.extras.execute_values(
|
||||
cur,
|
||||
""" DELETE FROM word USING (VALUES %s) as v(name, in_class, in_type, op)
|
||||
|
||||
@@ -314,7 +314,7 @@ class LegacyNameAnalyzer:
|
||||
FROM location_postcode) x""")
|
||||
|
||||
|
||||
def update_special_phrases(self, phrases):
|
||||
def update_special_phrases(self, phrases, should_replace):
|
||||
""" Replace the search index for special phrases with the new phrases.
|
||||
"""
|
||||
norm_phrases = set(((self.normalize(p[0]), p[1], p[2], p[3])
|
||||
@@ -343,7 +343,7 @@ class LegacyNameAnalyzer:
|
||||
FROM (VALUES %s) as v(name, class, type, op))""",
|
||||
to_add)
|
||||
|
||||
if to_delete:
|
||||
if to_delete and should_replace:
|
||||
psycopg2.extras.execute_values(
|
||||
cur,
|
||||
""" DELETE FROM word USING (VALUES %s) as v(name, in_class, in_type, op)
|
||||
|
||||
@@ -23,7 +23,7 @@ LOG = logging.getLogger()
|
||||
class SPImporter():
|
||||
# pylint: disable-msg=too-many-instance-attributes
|
||||
"""
|
||||
Class handling the process of special phrases importations into the database.
|
||||
Class handling the process of special phrases importation into the database.
|
||||
|
||||
Take a sp loader which load the phrases from an external source.
|
||||
"""
|
||||
@@ -42,10 +42,14 @@ class SPImporter():
|
||||
#special phrases class/type on the wiki.
|
||||
self.table_phrases_to_delete = set()
|
||||
|
||||
def import_phrases(self, tokenizer):
|
||||
def import_phrases(self, tokenizer, should_replace):
|
||||
"""
|
||||
Iterate through all specified languages and
|
||||
extract corresponding special phrases from the wiki.
|
||||
Iterate through all SpecialPhrases extracted from the
|
||||
loader and import them into the database.
|
||||
|
||||
If should_replace is set to True only the loaded phrases
|
||||
will be kept into the database. All other phrases already
|
||||
in the database will be removed.
|
||||
"""
|
||||
LOG.warning('Special phrases importation starting')
|
||||
self._fetch_existing_place_classtype_tables()
|
||||
@@ -60,11 +64,12 @@ class SPImporter():
|
||||
class_type_pairs.update(result)
|
||||
|
||||
self._create_place_classtype_table_and_indexes(class_type_pairs)
|
||||
self._remove_non_existent_tables_from_db()
|
||||
if should_replace:
|
||||
self._remove_non_existent_tables_from_db()
|
||||
self.db_connection.commit()
|
||||
|
||||
with tokenizer.name_analyzer() as analyzer:
|
||||
analyzer.update_special_phrases(self.word_phrases)
|
||||
analyzer.update_special_phrases(self.word_phrases, should_replace)
|
||||
|
||||
LOG.warning('Import done.')
|
||||
self.statistics_handler.notify_import_done()
|
||||
|
||||
Reference in New Issue
Block a user