Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
1.6 kB
3
Indexable
Never
import boto3
from pydantic import BaseModel
from typing import List
import json

class PeriodSearchResultItem(BaseModel):
    accounting_period_name: str
    accounting_period_end_date: str
    accounting_period_id: str
    accounting_period_start_date: str

class PeriodSearchResults(BaseModel):
    features: List[PeriodSearchResultItem]

class PeriodSearchParameters(BaseModel):
    query: str
    fields: List[str] = ["accounting_period_name"]
    sort: str = '_score desc'
    size: int = 10

class PeriodCloudSearchClient:
    def __init__(self, domain_endpoint: str):
        self.client = boto3.client('cloudsearchdomain', endpoint_url=domain_endpoint)

    def search(self, params: PeriodSearchParameters) -> PeriodSearchResults:
        response = self.client.search(
            query=params.query,
            queryParser='simple',
            sort=params.sort,
            size=params.size,
            q_options=json.dumps({"fields": params.fields})
        )
        search_results = []
        for hit in response['hits']['hit']:
            search_results.append(PeriodSearchResultItem(
                accounting_period_name=hit['fields'].get('accounting_period_name', [''])[0],
                accounting_period_end_date=hit['fields'].get('accounting_period_end_date', [''])[0],
                accounting_period_id=hit['fields'].get('accounting_period_id', [''])[0],
                accounting_period_start_date=hit['fields'].get('accounting_period_start_date', [''])[0]
            ))

        return PeriodSearchResults(
            features=search_results
        )
Leave a Comment