]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/index.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / nominatim / clicmd / index.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Implementation of the 'index' subcommand.
9 """
10 import argparse
11
12 import psutil
13
14 from nominatim.db import status
15 from nominatim.db.connection import connect
16 from nominatim.clicmd.args import NominatimArgs
17
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
22
23
24 class UpdateIndex:
25     """\
26     Reindex all new and modified data.
27
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.
32     """
33
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')
44
45
46     def run(self, args: NominatimArgs) -> int:
47         from ..indexer.indexer import Indexer
48         from ..tokenizer import factory as tokenizer_factory
49
50         tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
51
52         indexer = Indexer(args.config.get_libpq_dsn(), tokenizer,
53                           args.threads or psutil.cpu_count() or 1)
54
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
60         if not args.no_boundaries and not args.boundaries_only \
61            and args.minrank == 0 and args.maxrank == 30:
62             with connect(args.config.get_libpq_dsn()) as conn:
63                 status.set_indexed(conn, True)
64
65         return 0