Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search: adding extra metadata field #16

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions invenio_geographic_identifiers/contrib/geonames/datastreams.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from itertools import chain

import pycountry
from invenio_access.permissions import system_identity
from invenio_vocabularies.datastreams.transformers import BaseTransformer
from invenio_vocabularies.datastreams.writers import ServiceWriter
Expand All @@ -25,21 +26,19 @@ class GeoNamesTransformer(BaseTransformer):

def apply(self, stream_entry, *args, **kwargs):
"""Applies the transformation to the entry."""
record_country = []

country_code = stream_entry.entry.get("country_code")
country_code = pycountry.countries.get(alpha_2=country_code)

if country_code:
record_country = [country_code.name, country_code.official_name]

stream_entry.entry = {
"id": f"geonames::{stream_entry.entry['geonameid']}",
"scheme": "geonames",
"name": list(
chain(
*list(
filter(
lambda x: x is not None,
map(
lambda y: stream_entry.entry.get(y),
["asciiname", "name", "alternativenames"],
),
)
)
)
"name": (
stream_entry.entry.get("name") or stream_entry.entry.get("asciiname")
),
"locations": [
{
Expand All @@ -52,6 +51,22 @@ def apply(self, stream_entry, *args, **kwargs):
}
}
],
"extras": list(
chain(
*[
list(
filter(
lambda x: x is not None and x.strip(),
map(
lambda y: stream_entry.entry.get(y),
["asciiname", "name", "alternativenames"],
),
)
),
record_country,
]
)
),
}

return stream_entry
Expand Down
6 changes: 3 additions & 3 deletions invenio_geographic_identifiers/geoidentifiers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class GeographicIdentifiersSearchOptions(SearchOptions):
suggest_parser_cls = FilteredSuggestQueryParser.factory(
filter_field="scheme",
fields=[ # suggest fields
"name^100",
"name._2gram",
"name._3gram",
"extras^100",
"extras._2gram",
"extras._3gram",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
},
"name": {
"description": "Human readable label.",
"type": "array",
"items": {
"type": "string"
}
"type": "string"
},
"locations": {
"type": "array",
Expand All @@ -34,6 +31,13 @@
"$ref": "local://records/definitions-v2.0.0.json#/GeoJSON-Geometry"
}
}
},
"extras": {
"description": "Extra metadata about the identifier.",
"type": "array",
"items": {
"type": "string"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
}
}
},
"extras": {
"type": "text",
"fields": {
"suggest": {
"type": "search_as_you_type"
}
}
},
"pid": {
"type": "object",
"properties": {
Expand Down
28 changes: 25 additions & 3 deletions invenio_geographic_identifiers/geoidentifiers/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,33 @@ class LocationSchema(Schema):
class GeographicIdentifiersSchema(BaseVocabularySchema):
"""Service schema for geographic identifiers."""

# following the definition made in the ``invenio-vocabularies
# (subjects)``, here the ``id`` is ``required``
#
# Identifier
#
id = SanitizedUnicode(required=True)

#
# Scheme
#
scheme = SanitizedUnicode(required=True)
name = fields.List(required=True, cls_or_instance=SanitizedUnicode(required=True))

#
# Identifier name (e.g., Geonames feature name)
#
name = SanitizedUnicode(required=True)

#
# Location (e.g., list of points)
#
locations = fields.List(
required=True, cls_or_instance=fields.Nested(LocationSchema)
)

#
# Extra metadata (This field is used in the search)
#
extras = fields.List(
required=True,
cls_or_instance=SanitizedUnicode(required=True),
load_only=True,
)
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ packages = find:
python_requires = >=3.7
zip_safe = False
install_requires =
pycountry<=23.12.11
invenio-i18n<=1.3.3
invenio-vocabularies>=1.0.0,<2.0.0

Expand Down
Loading