Skip to content

Commit fc5ee7f

Browse files
committed
move index names to settings
1 parent 4f77de8 commit fc5ee7f

File tree

15 files changed

+59
-40
lines changed

15 files changed

+59
-40
lines changed

findthatpostcode/blueprints/places.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from findthatpostcode.blueprints.utils import return_result
55
from findthatpostcode.controllers.places import Place
66
from findthatpostcode.db import ElasticsearchDep
7+
from findthatpostcode.settings import PLACENAME_INDEX
78

89
bp = APIRouter(prefix="/places")
910

@@ -32,7 +33,7 @@ def nearest(
3233
}
3334

3435
data = es.search(
35-
index="geo_placename",
36+
index=PLACENAME_INDEX,
3637
body=query,
3738
ignore=[404], # type: ignore
3839
size=10, # type: ignore

findthatpostcode/blueprints/postcodes.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
RUC21_CODES,
1515
STATS_FIELDS,
1616
)
17+
from findthatpostcode.settings import AREA_INDEX, POSTCODE_INDEX
1718

1819
bp = APIRouter(prefix="/postcodes")
1920

@@ -107,14 +108,14 @@ def get_postcode_by_hash(
107108
results = list(
108109
scan(
109110
es,
110-
index="geo_postcode",
111+
index=POSTCODE_INDEX,
111112
query={"query": {"bool": {"should": query}}},
112113
_source_includes=fields + name_fields + stats_fields,
113114
)
114115
)
115116
areas = scan(
116117
es,
117-
index="geo_area",
118+
index=AREA_INDEX,
118119
query={"query": {"terms": {"type": name_fields}}},
119120
_source_includes=["name"],
120121
)
@@ -157,7 +158,7 @@ def get_stats(data, lsoas):
157158
i["_id"]: i["_source"]
158159
for i in scan(
159160
es,
160-
index="geo_area",
161+
index=AREA_INDEX,
161162
query={"query": {"terms": {"_id": list(lsoas_to_get)}}},
162163
_source_includes=[i.location for i in stats],
163164
)

findthatpostcode/blueprints/process_csv.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from elasticsearch import Elasticsearch
88

99
from findthatpostcode.controllers.postcodes import Postcode
10+
from findthatpostcode.settings import AREA_INDEX, POSTCODE_INDEX
1011
from findthatpostcode.utils import ESConfig
1112

1213
# List of potential postcode fields
@@ -22,7 +23,7 @@ def process_csv(
2223
es_config: ESConfig | None = None,
2324
) -> None:
2425
if not es_config:
25-
es_config = ESConfig(es_index="geo_postcode")
26+
es_config = ESConfig(es_index=POSTCODE_INDEX)
2627

2728
# @TODO add option for different CSV dialects and for no headers
2829
# In the case of no headers you would find the field by number
@@ -52,7 +53,7 @@ def process_csv(
5253
row[i] = code_cache[code]
5354
elif code:
5455
area = es.get(
55-
index="geo_area",
56+
index=AREA_INDEX,
5657
doc_type=es_config.es_type,
5758
id=code,
5859
ignore=[404], # type: ignore

findthatpostcode/commands/codes.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from findthatpostcode.commands.utils import bulk_upload, get_latest_geoportal_url
1919
from findthatpostcode.db import get_es
2020
from findthatpostcode.metadata import ENTITIES
21-
from findthatpostcode.settings import DEBUG
21+
from findthatpostcode.settings import DEBUG, ENTITY_INDEX
2222

2323
PRD_RGC = "PRD_RGC"
2424
PRD_CHD = "PRD_CHD"
@@ -28,8 +28,6 @@
2828
MSOA_2021_URL = (
2929
"https://houseofcommonslibrary.github.io/msoanames/MSOA-Names-Latest2.csv"
3030
)
31-
ENTITY_INDEX = "geo_entity"
32-
AREA_INDEX = "geo_area"
3331
DEFAULT_ENCODING = "utf-8-sig"
3432

3533

findthatpostcode/commands/placenames.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212

1313
from findthatpostcode.commands.utils import bulk_upload, get_latest_geoportal_url
1414
from findthatpostcode.db import get_es
15-
from findthatpostcode.settings import DEBUG
15+
from findthatpostcode.settings import DEBUG, PLACENAME_INDEX
1616

17-
PLACENAMES_INDEX = "geo_placename"
1817
PRD_IPN = "PRD_IPN"
1918

2019
PLACE_TYPES: dict[str, tuple[str, str]] = {
@@ -76,9 +75,9 @@
7675

7776

7877
@click.command("placenames")
79-
@click.option("--es-index", default=PLACENAMES_INDEX)
78+
@click.option("--es-index", default=PLACENAME_INDEX)
8079
@click.option("--url", default=None)
81-
def import_placenames(url=None, es_index=PLACENAMES_INDEX):
80+
def import_placenames(url=None, es_index=PLACENAME_INDEX):
8281
if DEBUG:
8382
requests_cache.install_cache()
8483

findthatpostcode/commands/postcodes.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@
1414

1515
from findthatpostcode.commands.utils import bulk_upload, get_latest_geoportal_url
1616
from findthatpostcode.db import get_es
17-
from findthatpostcode.settings import DEBUG
17+
from findthatpostcode.settings import DEBUG, POSTCODE_INDEX
1818

19-
PC_INDEX = "geo_postcode"
2019
PRD_NSPL = "PRD_NSPL"
2120

2221

2322
@click.command("nspl")
24-
@click.option("--es-index", default=PC_INDEX)
23+
@click.option("--es-index", default=POSTCODE_INDEX)
2524
@click.option("--url", default=None)
26-
def import_nspl(url=None, es_index=PC_INDEX):
25+
def import_nspl(url=None, es_index=POSTCODE_INDEX):
2726
if not url:
2827
url = get_latest_geoportal_url(PRD_NSPL)
2928

findthatpostcode/controllers/areas.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@
1515
)
1616
from findthatpostcode.controllers.places import Place
1717
from findthatpostcode.metadata import AREA_TYPES
18-
from findthatpostcode.settings import S3_BUCKET
18+
from findthatpostcode.settings import (
19+
AREA_INDEX,
20+
ENTITY_INDEX,
21+
PLACENAME_INDEX,
22+
POSTCODE_INDEX,
23+
S3_BUCKET,
24+
)
1925
from findthatpostcode.utils import ESConfig
2026

2127
if TYPE_CHECKING:
2228
from findthatpostcode.controllers.postcodes import Postcode
2329

2430

2531
class Areatype(Controller):
26-
es_index = "geo_entity"
32+
es_index = ENTITY_INDEX
2733
url_slug = "areatypes"
2834
areatypes = AREA_TYPES
2935

@@ -98,7 +104,7 @@ def get_areas(
98104
}
99105
}
100106
search_params: dict[str, str | dict | int] = dict(
101-
index="geo_area",
107+
index=AREA_INDEX,
102108
body=query,
103109
sort="_id:asc",
104110
)
@@ -144,7 +150,7 @@ def area_types_count(
144150

145151

146152
class Area(Controller):
147-
es_index = "geo_area"
153+
es_index = AREA_INDEX
148154
url_slug = "areas"
149155
template = "area.html.j2"
150156
date_fields = ["date_end", "date_start"]
@@ -306,7 +312,7 @@ def get_example_postcodes(
306312
}
307313
}
308314
example = es.search(
309-
index="geo_postcode",
315+
index=POSTCODE_INDEX,
310316
body=query,
311317
size=examples_count, # type: ignore
312318
)
@@ -327,7 +333,7 @@ def get_children(areacode: str, es: Elasticsearch) -> dict:
327333
},
328334
}
329335
children = es.search(
330-
index="geo_area",
336+
index=AREA_INDEX,
331337
body=query,
332338
size=100, # type: ignore
333339
_source_includes=["code", "name", "type", "active"], # type: ignore
@@ -424,7 +430,7 @@ def search_areas(
424430
Search for areas based on a name
425431
"""
426432
if not es_config:
427-
es_config = ESConfig(es_index="geo_area")
433+
es_config = ESConfig(es_index=AREA_INDEX)
428434
query = {
429435
"query": {
430436
"function_score": {
@@ -489,7 +495,7 @@ def search_areas(
489495
}
490496
if pagination:
491497
result = es.search(
492-
index="geo_area,geo_placename",
498+
index=f"{AREA_INDEX},{PLACENAME_INDEX}",
493499
body=query,
494500
from_=pagination.from_, # type: ignore
495501
size=pagination.size, # type: ignore
@@ -498,14 +504,14 @@ def search_areas(
498504
)
499505
else:
500506
result = es.search(
501-
index="geo_area,geo_placename",
507+
index=f"{AREA_INDEX},{PLACENAME_INDEX}",
502508
body=query,
503509
_source_excludes=["boundary"], # type: ignore
504510
ignore=[404], # type: ignore
505511
)
506512
return_result = []
507513
for a in result.get("hits", {}).get("hits", []):
508-
if a["_index"] == "geo_placename":
514+
if a["_index"] == PLACENAME_INDEX:
509515
relationships = {}
510516
if a["_source"].get("areas", {}).get("laua"):
511517
relationships["areas"] = [

findthatpostcode/controllers/places.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from elasticsearch import Elasticsearch
55

66
from findthatpostcode.controllers.controller import Controller
7+
from findthatpostcode.settings import PLACENAME_INDEX, POSTCODE_INDEX
78
from findthatpostcode.utils import ESConfig
89

910
if TYPE_CHECKING:
@@ -12,7 +13,7 @@
1213

1314

1415
class Place(Controller):
15-
es_index = "geo_placename"
16+
es_index = PLACENAME_INDEX
1617
url_slug = "places"
1718
template = "place.html.j2"
1819

@@ -107,7 +108,7 @@ def get_nearest_postcodes(
107108
],
108109
}
109110
example = es.search(
110-
index="geo_postcode",
111+
index=POSTCODE_INDEX,
111112
body=query,
112113
size=examples_count, # type: ignore
113114
)
@@ -134,7 +135,7 @@ def get_nearest_places(
134135
],
135136
}
136137
example = es.search(
137-
index="geo_placename",
138+
index=PLACENAME_INDEX,
138139
body=query,
139140
size=examples_count, # type: ignore
140141
)

findthatpostcode/controllers/points.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
from findthatpostcode.controllers.controller import Controller
66
from findthatpostcode.controllers.postcodes import Postcode
7+
from findthatpostcode.settings import POSTCODE_INDEX
78
from findthatpostcode.utils import ESConfig
89

910

1011
class Point(Controller):
11-
es_index = "geo_postcode"
12+
es_index = POSTCODE_INDEX
1213
url_slug = "points"
1314
template = "postcode.html.j2"
1415
max_distance = 10000

findthatpostcode/controllers/postcodes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
RUC21_CODES,
1515
STATS_FIELDS,
1616
)
17+
from findthatpostcode.settings import PLACENAME_INDEX, POSTCODE_INDEX
1718
from findthatpostcode.utils import ESConfig
1819

1920

2021
class Postcode(Controller):
21-
es_index = "geo_postcode"
22+
es_index = POSTCODE_INDEX
2223
url_slug = "postcodes"
2324
date_fields = ["dointr", "doterm"]
2425
not_area_fields = ["osgrdind", "usertype"]
@@ -127,7 +128,7 @@ def get_nearest_places(
127128
],
128129
}
129130
example = es.search(
130-
index="geo_placename",
131+
index=PLACENAME_INDEX,
131132
body=query,
132133
size=examples_count, # type: ignore
133134
)

0 commit comments

Comments
 (0)