Look up items in list of dataclasses by value

Viewed 223

I'm using python to filter data in elasticsearch based on request params provided. I've got a working example, but know it can be improved and am trying to think of a better way. The current code is like this:

@dataclass(frozen=True)
class Filter:
    param: str
    custom_es_field: Optional[str] = None
    is_bool_query: bool = False
    is_date_query: bool = False
    is_range_query: bool = False

    def es_field(self) -> str:
        if self.custom_es_field:
            field = self.custom_es_field
        elif "." in self.param:
            field = self.param.replace(".", "__")
        else:
            field = self.param
        return field

filters = [
    Filter(param="publication_year", is_range_query=True),
    Filter(param="publication_date", is_date_query=True),
    Filter(param="venue.issn").
    ...
]

def filter_records(filter_params, s):
    for filter in filters:

        # range query
        if filter.param in filter_params and filter.is_range_query:
            param = filter_params[filter.param]
            if "<" in param:
                param = param[1:]
                validate_range_param(filter, param)
                kwargs = {filter.es_field(): {"lte": int(param)}}
                s = s.filter("range", **kwargs)

        elif filter.param in filter_params and filter.is_bool_query:
            ....

The thing I think is slow is I am looping through all of the filters in order to use the one that came in as a request variable. I'm tempted to convert this to a dictionary so I can do filter["publication_year"], but I like having the extra methods available via the dataclass. Would love to hear any thoughts.

0 Answers
Related