Skip to content

Union of QuerySets from Different Models Not Including Both Models in SearchQuerySet().models() when Using Solr #2003

@DhavalGojiya

Description

@DhavalGojiya

Description:

Hello! 👋

I am encountering an issue when trying to combine querysets from different models using SearchQuerySet().models(). When I perform the union of two querysets, Solr only returns documents from the first model (the one from which the query is executed first) and ignores the second model. This causes the count to be incorrect and only shows the results for the first model.

Steps to Reproduce:

  1. Query for model Legislator:

    lg_results = SearchQuerySet().models(Legislator)
  2. Query for model Bill:

    bill_results = SearchQuerySet().models(Bill)
  3. Combine both querysets:

    final = lg_results | bill_results
  4. The resulting count from final.count() is incorrect — it only returns results from the first model (Legislator in this case).

Expected Behavior:

When performing the union (|) of two querysets, I expect the results to include documents from both models (Legislator and Bill), and the count should reflect the sum of both sets.

Actual Behavior:

The query only includes documents from the first model (Legislator), and the second model (Bill) is not included in the union. This is confirmed by the following logs:

Logs/Debug Output:

INFO pysolr _send_request Finished 'http://127.0.0.1:8983/solr/core2/select/?q=%2A%3A%2A&fl=%2A+score&df=text&start=0&rows=1&spellcheck=true&spellcheck.collate=true&spellcheck.count=1&fq=django_ct%3A%28congress.legislator%29&wt=json' (get) with body '' in 0.004 seconds, with status 200
  • Count of lg_results:

    lg_results.count()  # Result count = 11126
  • Count of bill_results:

    bill_results.count()  # Result count = 26994
  • However, the final count after union (lg_results | bill_results) is incorrect and is only showing results for the Legislator model (11126), while it should reflect results from both models.

What I Tried:

  1. I verified that the counts for lg_results and bill_results are correct individually.
  2. I tried combining the querysets with the union (|), but the union does not include documents from both models as expected.
  3. I checked the django_ct filters in Solr, but the union of querysets seems to ignore the second model filter.

Environment:

  • Django version: 4.2 LTS
  • Haystack version: 3.3.0
  • Solr version: 8.9.0
  • Python version: 3.12

Possible Solutions:

  • Ensure that the django_ct filter is properly applied to both querysets when combining them using |.
  • Clarify whether Haystack handles combining querysets from different models correctly when using models() in SearchQuerySet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions