Skip to content

search_service

django_spire.knowledge.entry.services.search_service

EntrySearchService

Bases: BaseDjangoModelService['Entry']

Source code in django_spire/contrib/constructor/constructor.py
def __init__(self, obj: Any = None):
    self._obj_type_name: str = str(
        next(iter(self.__class__.__annotations__.values()))
    ).split('.')[-1]

    if obj is None:
        return

    self._obj_mro_type_names = [cls.__name__ for cls in obj.__class__.__mro__]

    if self._obj_type_name not in self._obj_mro_type_names:
        message = f'{self.__class__.__name__} was instantiated with obj type "{obj.__class__.__name__}" and failed as it was expecting "{self._obj_type_name}".'
        raise ConstructorError(message)

    self._obj_type: type[TypeAny] = obj.__class__

    if self._obj_type is None or self._obj_type is ...:
        message = f'{self.__class__.__name__} top class attribute must have an annotated type.'
        raise ConstructorError(message)

    self.obj: TypeAny = obj

    if ABC not in self.__class__.__bases__:
        if not self._obj_is_valid:
            message = f'{self._obj_type_name} failed to validate on {self.__class__.__name__}'
            raise ConstructorError(message)

    self.__post_init__()

obj instance-attribute

search

Source code in django_spire/knowledge/entry/services/search_service.py
def search(
    self,
    query: str,
    use_llm_preprocessing: bool = True,
) -> QuerySet[Entry]:
    query = query.strip() if query else ''

    if not query:
        return self.obj_class.objects.none()

    if use_llm_preprocessing:
        preprocessed = preprocess_search_query(query=query)
        primary_query = preprocessed.primary_search_query
        all_terms = preprocessed.all_search_terms
    else:
        primary_query = query
        all_terms = set(query.split())

    if not primary_query:
        return self.obj_class.objects.none()

    word_filters = []

    for word in all_terms:
        if len(word) >= 2:
            word_filters.append(Q(name__icontains=word))
            word_filters.append(Q(_search_text__icontains=word))

    combined_word_filter = reduce(or_, word_filters) if word_filters else Q()

    if connection.vendor == 'postgresql':
        return self._postgres_search(primary_query, combined_word_filter)

    return self._fallback_search(combined_word_filter)