Solr Search Not Returning Expected Results in Django with Haystack
I'm working on a Django project using Haystack for search functionality with Solr as the backend. I've set up everything, but when I perform a search, the results are not returned as expected.
Problem Description:
I have a search view that queries Solr for products based on a search term. While the search query executes without errors, the returned results do not contain any products, even though I have verified that products exist in the database.
views.py:
from haystack import indexes
from haystack.query import SearchQuerySet
from django.shortcuts import render
def search_view(request):
query = request.GET.get('q')
results = SearchQuerySet().filter(content=query) if query else []
print(f"Search Query: {query}, Results: {results}")
context = {
'results':results,
'query':query,
}
return render(request, 'core/search.html', context)
Search_indexes.py:
from haystack import indexes
from core.models import *
class Solr_Model_Index (indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
def get_model(self):
return Product
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.all()
Steps Taken:
- Rebuilt the Index: I ran
python manage.py rebuild_index
to ensure all products are indexed. - Checked Solr Admin Interface: I confirmed that products are visible in Solr under the "Documents" tab.
- Debugging: Added print statements in the search view to check the query and results.
Current Output:
Search Query: macbook, Results: <SearchQuerySet: query=<haystack.backends.solr_backend.SolrSearchQuery object at 0x0000029EE2DBBE60>, using=None>
Questions:
- Why are the search results empty despite having indexed products?
- Are there any additional configurations I might need in Solr or Haystack to ensure proper indexing and searching?
Any guidance or insights would be greatly appreciated!