]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/index.py
Merge pull request #2493 from lonvia/handle-frequent-partials
[nominatim.git] / nominatim / clicmd / index.py
1 """
2 Implementation of the 'index' subcommand.
3 """
4 import psutil
5
6 from nominatim.db import status
7 from nominatim.db.connection import connect
8
9 # Do not repeat documentation of subcommand classes.
10 # pylint: disable=C0111
11 # Using non-top-level imports to avoid eventually unused imports.
12 # pylint: disable=E0012,C0415
13
14
15 class UpdateIndex:
16     """\
17     Reindex all new and modified data.
18
19     Indexing is the process of computing the address and search terms for
20     the places in the database. Every time data is added or changed, indexing
21     needs to be run. Imports and replication updates automatically take care
22     of indexing. For other cases, this function allows to run indexing manually.
23     """
24
25     @staticmethod
26     def add_args(parser):
27         group = parser.add_argument_group('Filter arguments')
28         group.add_argument('--boundaries-only', action='store_true',
29                            help="""Index only administrative boundaries.""")
30         group.add_argument('--no-boundaries', action='store_true',
31                            help="""Index everything except administrative boundaries.""")
32         group.add_argument('--minrank', '-r', type=int, metavar='RANK', default=0,
33                            help='Minimum/starting rank')
34         group.add_argument('--maxrank', '-R', type=int, metavar='RANK', default=30,
35                            help='Maximum/finishing rank')
36
37     @staticmethod
38     def run(args):
39         from ..indexer.indexer import Indexer
40         from ..tokenizer import factory as tokenizer_factory
41
42         tokenizer = tokenizer_factory.get_tokenizer_for_db(args.config)
43
44         indexer = Indexer(args.config.get_libpq_dsn(), tokenizer,
45                           args.threads or psutil.cpu_count() or 1)
46
47         if not args.no_boundaries:
48             indexer.index_boundaries(args.minrank, args.maxrank)
49         if not args.boundaries_only:
50             indexer.index_by_rank(args.minrank, args.maxrank)
51
52         if not args.no_boundaries and not args.boundaries_only \
53            and args.minrank == 0 and args.maxrank == 30:
54             with connect(args.config.get_libpq_dsn()) as conn:
55                 status.set_indexed(conn, True)
56
57         return 0