Buckets:

download
raw
2.8 kB
import logging
from typing import Any
from karp.lex.application import resource_queries, search_queries
from karp.search.domain import QueryRequest
from karp.search.domain.query_dsl.karp_query_model import Identifier, TextArgExpression
from .plugin import INDEXED, Plugin, group_batch_by
logger = logging.getLogger(__name__)
class BacklinkPlugin(Plugin):
"""
A plugin that find all references to a entry via a target field
For example:
- a resource has a field called thing_id and ref_field
- ref_field can contain values that occur in thing_id (typically a ID of some sort)
- a virtual field with BacklinkPlugin for thing_id should contain all entries that have the value of thing_id in ref_field
This plugin adds the link data during fetching in the API and is not available for search.
"""
def output_config(self, resource, field, *args, **kwargs):
"""
Note that currently the relationship is assumed to be one-to-many or many-to-many
"""
# lookup <field> in the <resource> and check its type
resource_dto = resource_queries.by_resource_id_optional(resource, expand_plugins=False)
if not resource_dto:
logger.warning(
f'The linked resource "{resource}", does not exist. '
"Reindex this resource after it has been added to make backlinks work."
)
return {"type": "object", "fields": {}}
field_type = resource_dto.config.fields[field].type
return {
"type": field_type,
"collection": True,
"searchable": False,
}
@group_batch_by("resource", "field", "target")
def generate_batch(self, resource: str, field: str, target: Any, batch: list[dict[str, str]]):
"""
target is the referencing field in other entry, used in the query to get the referees
field is the name of identifying field for the reference, used to populate the backlink field
"""
# collect all the ids and run one multi_query to find all of them
all_ids = [item["id"] for item in batch]
requests = [
QueryRequest(
resources=[resource],
q=TextArgExpression(op="equals", field=Identifier(ast=target), arg=id),
lexicon_stats=False,
size=None,
)
for id in all_ids
]
query_results = search_queries.multi_query(requests, expand_plugins=INDEXED)
id_references = {
id: [entry["entry"][field] for entry in entries["hits"]]
for id, entries in zip(all_ids, query_results, strict=False)
}
# now collect the results
result = [id_references.get(id, []) for id in all_ids]
return result

Xet Storage Details

Size:
2.8 kB
·
Xet hash:
6774a0b845e2b2a6f89060eed973a645b7e6b0cad38c60723d5c0c7838377111

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.