Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search for a subset by filtering on related model fields #291

Open
ranebo opened this issue Nov 30, 2021 · 2 comments
Open

Search for a subset by filtering on related model fields #291

ranebo opened this issue Nov 30, 2021 · 2 comments

Comments

@ranebo
Copy link

ranebo commented Nov 30, 2021

Hello,

Thanks for creating and maintaining this great package!

I've run into a situation where I would like to search a subset of models by filtering on a related model's field, like this:

watson.search("Search terms", models=(MyModel.objects.filter(related__is_public=True),))

When I try this, I get a django.db.utils.ProgrammingError: column reference "id" is ambiguous error.

Not sure if i'm missing a configuration, or if this isn't feasible at the moment. Would be great to know a way how to do this.

Thanks!

P.S. Here is the actual db error I'm working with, incase something else is going on:

ERROR:  column reference "id" is ambiguous at character 578
STATEMENT:  SELECT (ts_rank_cd(watson_searchentry.search_tsv, to_tsquery('pg_catalog.simple', '$$c$$:*'))) AS "watson_rank", "watson_searchentry"."id", "watson_searchentry"."engine_slug", "watson_searchentry"."content_type_id", "watson_searchentry"."object_id", "watson_searchentry"."object_id_int", "watson_searchentry"."title", "watson_searchentry"."description", "watson_searchentry"."content", "watson_searchentry"."url", "watson_searchentry"."meta_encoded" FROM "watson_searchentry" WHERE ("watson_searchentry"."engine_slug" = 'bread' AND "watson_searchentry"."object_id" IN (SELECT ("id"::text) AS "watson_pk_str" FROM "services_servicefirm" U0 INNER JOIN "professionals_company" U1 ON (U0."company_id" = U1."id") WHERE (U1."claimed_on" IS NOT NULL AND U1."deleted_on" IS NULL AND U1."is_public" = true AND U0."deleted_on" IS NULL AND U0."is_public" = true AND U0."membership_is_active" = true)) AND "watson_searchentry"."content_type_id" = 124 AND (search_tsv @@ to_tsquery('pg_catalog.simple', '$$c$$:*'))) ORDER BY "watson_rank" DESC  LIMIT 21

from

filters = dict(
    company__claimed_on__isnull=False,
    company__deleted_on=None,
    deleted_on=None,
    company__is_public=True,
    is_public=True,
    membership_is_active=True,
)

watson.search("Search terms", models=(ServiceFirm.objects.filter(**filters),))
@etianen
Copy link
Owner

etianen commented Dec 1, 2021 via email

@ranebo
Copy link
Author

ranebo commented Dec 2, 2021

Thanks for the response!

'watson.filter' worked just fine, so has what I need for now. Later down the line I might need it for multiple models, so I will consider taking the time for the MR then.

Hopefully it gets fixed before then.
Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants