Skip to content

Commit abe3312

Browse files
committed
chg: fts text
1 parent d9497b4 commit abe3312

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ dependencies = [
1818
"typer>=0.20.0",
1919
"pygeometa>=0.19.0",
2020
"orjson>=3.11.4",
21-
"duckdb>=1.4.2"
21+
"duckdb>=1.4.2",
22+
"lxml>=6.0.2"
2223
]
2324
description = ""
2425
license = "GPL-3.0+"

src/datasync/dms.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import typer
44
from pygeometa.schemas.iso19139 import ISO19139OutputSchema
55

6+
from .libs.helpers import get_anytext
67
from .settings import log
78

89
app = typer.Typer()
@@ -30,6 +31,9 @@ def generate_csw_metadata(
3031
conn = duckdb.connect()
3132
conn.sql("INSTALL yaml FROM community; LOAD yaml; Install spatial; load spatial")
3233

34+
conn.create_function("to_iso19139", to_iso19139)
35+
conn.create_function("xml_bag", get_anytext)
36+
3337
datasets = conn.read_parquet(base_url + "datasets_dataset.parquet").filter(
3438
"json_keys(metadata) <> []"
3539
)
@@ -219,9 +223,11 @@ def generate_csw_metadata(
219223
)
220224
log.debug(res)
221225

222-
conn.create_function("to_iso19139", to_iso19139)
226+
iso_xml = res.select("id, to_iso19139(metadata) as xml")
227+
228+
log.debug(iso_xml)
223229

224-
log.debug(res.select("id, to_iso19139(metadata) as xml"))
230+
log.debug(iso_xml.select("*, xml_bag(xml) as fts_text"))
225231

226232

227233
if __name__ == "__main__":

src/datasync/libs/helpers.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import re
2+
3+
from lxml import etree
4+
5+
PARSER = etree.XMLParser(resolve_entities=False)
6+
7+
8+
def get_anytext(bag: str) -> str:
9+
"""
10+
generate bag of text for free text searches
11+
accepts list of words, string of XML, or etree.Element
12+
"""
13+
14+
if isinstance(bag, list): # list of words
15+
return " ".join([_f for _f in bag if _f]).strip()
16+
else: # xml
17+
if isinstance(bag, bytes) or isinstance(bag, str):
18+
# serialize to lxml
19+
bag = etree.fromstring(bag, PARSER) # noqa: S320
20+
# get all XML element content
21+
return re.sub(
22+
r"\s+",
23+
" ",
24+
" ".join([value.strip() for value in bag.xpath("//text()")]).strip(),
25+
)

uv.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)