Cannot resolve keyword 'min_similarity' into field. Choices are: ... #4252
-
|
Hello guys, I am implementing Global search to my models using search (keyword) field with TrigramSimilarity @strawberry_django.filter_type(BaseModel)
class BaseModelFilter:
id: strawberry.Maybe[GlobalID]
is_active: auto
min_similarity: Optional[float] = strawberry_django.filter_field(default=0.3)
@strawberry_django.filter_field(name='keyword')
def search(
self,
info: Info,
queryset: QuerySet,
value: str,
prefix: str,
) -> tuple[QuerySet, Q]:
if not value:
return queryset, Q()
threshold = 0.3 if self.min_similarity is strawberry.UNSET else self.min_similarity
if not is_postgres():
queryset = get_search_alias(queryset)
return queryset, Q(**{f'{prefix}searchable_text__icontains': value})
search_fields = get_search_fields(queryset.model)
if not search_fields:
return queryset, Q()
# We use the first field as the primary ranking field
rank_field = f'{prefix}{search_fields[0]}'
similarity_alias = 'search_similarity'
# Annotate similarity
queryset = queryset.annotate(**{similarity_alias: TrigramSimilarity(rank_field, value)})
q = Q(**{f'{similarity_alias}__gte': threshold})
for field in search_fields[1:]:
field_path = f'{prefix}{field}'
field_sim_alias = f'sim_{field}'
queryset = queryset.annotate(**{field_sim_alias: TrigramSimilarity(field_path, value)})
q |= Q(**{f'{field_sim_alias}__gte': threshold})
return queryset.order_by(F(similarity_alias).desc()), q
|
Beta Was this translation helpful? Give feedback.
Answered by
bellini666
Feb 28, 2026
Replies: 1 comment 8 replies
-
|
Hi @daudln , I think it is ok, I've done something similar in some projects using strawberry + strawberry_django, by having a
Where exactly are you getting this? Can you share the full traceback? |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, this was an actual thing we didn't have support for 😅
Added support to exclude that field from queryset filter on https://github.com/strawberry-graphql/strawberry-django/releases/tag/0.78.0
Documentation about it will also be available in some minutes at https://strawberry.rocks/docs/django/guide/filters
Let me know if that fixes the issue for you :)