1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Implementation of the 'index' subcommand.
14 from nominatim.db import status
15 from nominatim.db.connection import connect
16 from nominatim.clicmd.args import NominatimArgs
18 # Do not repeat documentation of subcommand classes.
19 # pylint: disable=C0111
20 # Using non-top-level imports to avoid eventually unused imports.
21 # pylint: disable=E0012,C0415
26 Reindex all new and modified data.
28 Indexing is the process of computing the address and search terms for
29 the places in the database. Every time data is added or changed, indexing
30 needs to be run. Imports and replication updates automatically take care
31 of indexing. For other cases, this function allows to run indexing manually.
34 def add_args(self, parser: argparse.ArgumentParser) -> None:
35 group = parser.add_argument_group('Filter arguments')
36 group.add_argument('--boundaries-only', action='store_true',
37 help="""Index only administrative boundaries.""")
38 group.add_argument('--no-boundaries', action='store_true',
39 help="""Index everything except administrative boundaries.""")
40 group.add_argument('--minrank', '-r', type=int, metavar='RANK', default=0,
41 help='Minimum/starting rank')
42 group.add_argument('--maxrank', '-R', type=int, metavar='RANK', default=30,
43 help='Maximum/finishing rank')
46 def run(self, args: NominatimArgs) -> int:
47 from ..indexer.indexer import Indexer
48 from ..tokenizer import factory as tokenizer_factory
50 tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
52 indexer = Indexer(args.config.get_libpq_dsn(), tokenizer,
53 args.threads or psutil.cpu_count() or 1)
55 if not args.no_boundaries:
56 indexer.index_boundaries(args.minrank, args.maxrank)
57 if not args.boundaries_only:
58 indexer.index_by_rank(args.minrank, args.maxrank)
59 indexer.index_postcodes()
61 if not args.no_boundaries and not args.boundaries_only \
62 and args.minrank == 0 and args.maxrank == 30:
63 with connect(args.config.get_libpq_dsn()) as conn:
64 status.set_indexed(conn, True)