1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Implementation of the 'index' subcommand.
15 from ..db import status
16 from ..db.connection import connect
17 from .args import NominatimArgs
22 Reindex all new and modified data.
24 Indexing is the process of computing the address and search terms for
25 the places in the database. Every time data is added or changed, indexing
26 needs to be run. Imports and replication updates automatically take care
27 of indexing. For other cases, this function allows to run indexing manually.
30 def add_args(self, parser: argparse.ArgumentParser) -> None:
31 group = parser.add_argument_group('Filter arguments')
32 group.add_argument('--boundaries-only', action='store_true',
33 help="""Index only administrative boundaries.""")
34 group.add_argument('--no-boundaries', action='store_true',
35 help="""Index everything except administrative boundaries.""")
36 group.add_argument('--minrank', '-r', type=int, metavar='RANK', default=0,
37 help='Minimum/starting rank')
38 group.add_argument('--maxrank', '-R', type=int, metavar='RANK', default=30,
39 help='Maximum/finishing rank')
41 def run(self, args: NominatimArgs) -> int:
42 asyncio.run(self._do_index(args))
44 if not args.no_boundaries and not args.boundaries_only \
45 and args.minrank == 0 and args.maxrank == 30:
46 with connect(args.config.get_libpq_dsn()) as conn:
47 status.set_indexed(conn, True)
51 async def _do_index(self, args: NominatimArgs) -> None:
52 from ..tokenizer import factory as tokenizer_factory
54 tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
55 from ..indexer.indexer import Indexer
57 indexer = Indexer(args.config.get_libpq_dsn(), tokenizer,
58 args.threads or psutil.cpu_count() or 1)
60 has_pending = True # run at least once
62 if not args.no_boundaries:
63 await indexer.index_boundaries(args.minrank, args.maxrank)
64 if not args.boundaries_only:
65 await indexer.index_by_rank(args.minrank, args.maxrank)
66 await indexer.index_postcodes()
67 has_pending = indexer.has_pending()