Django-Haystack Search based on multiple index fields

二次信任 提交于 2019-12-11 23:50:36

问题


I am quite a newbbie with django-haystack. Following documentation and tutorials I was able to create a search App based on document content (DataBase SQLITE). As next step, I have updated my HTML template to request for more info (e.g.: Model, desired DB link, etc.)

I do not know how to approach this. Below the files I am using. My knowledge is still quite basic, so any help would be very much appreciated.

Thanks.

search.html

{% extends 'base.html' %}

{% block content %}


    <h2> Doc Search Interface</h2>

    <form method="get" action=".">
        <table>
            <P>Select Car Model:</P>
            <P><LABEL ACCESSKEY=5><INPUT TYPE=checkbox NAME="CarModel" VALUE="A5"> A5</LABEL><BR>
            <LABEL ACCESSKEY=8><INPUT TYPE=checkbox NAME="CarModel" VALUE="A8"> A8</LABEL><BR>
            <LABEL ACCESSKEY=3><INPUT TYPE=checkbox NAME="CarModel" VALUE="A3"> A3</LABEL></P> 

            <tr>
            <P>Type and click enter for Search!</P>
                <input type="search" id="id_q" name="q" placeholder="Search" >
            </tr>
        </table>

        {% if query %}
            <h3>Let´s see if we have got here the document you were looking for...</h3>

            {% for result in page.object_list %}
                <p>
                    <a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>
                </p>
            {% empty %}
                <p>No results found.</p>
            {% endfor %}

            {% if page.has_previous or page.has_next %}
                <div>
                    {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
                    |
                    {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
                </div>
            {% endif %}
        {% else %}
            {# Show some example queries to run, maybe query syntax, something else? #}
        {% endif %}
    </form>
{% endblock %}

models.py

from django.db import models
from django.contrib.auth.models import User
from django.template import RequestContext
from django.http import HttpResponse

class Document(models.Model):

user_id = models.CharField(max_length=6, default='admin')
pub_date = models.DateTimeField()
title = models.CharField(max_length=200)
Link= models.URLField()
content = models.TextField()
CarModel= models.TextField()

    def get_absolute_url(self):
        return self.Link
    def __unicode__(self):
        return self.title

search_indexes.py

from haystack import indexes
from test3.models import Document

class DocumentIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.EdgeNgramField(document=True, use_template=True)
    content_auto=indexes.EdgeNgramField(model_attr='content')
    CarModel=indexes.EdgeNgramField(model_attr='CarModel')
    def get_model(self):
        return Document        
    def index_queryset(self, using=None):
        """Used when the entire index for model is updated. Typically to avoid some results showing when admin do not want to""" 
        return self.get_model().objects

Following this other post Using django haystack search with global search bar in template I have updated the following files

urls.py

from django.conf.urls import url
from test3.views import MySearchView


# urls.py

urlpatterns =  [url(r'^/search/?$', MySearchView.as_view(), name='My_search_view'),]

views.py

from haystack.forms import HighlightedSearchForm
from haystack.query import SearchQuerySet
from haystack.generic_views import SearchView
from haystack.views import search_view_factory

class MySearchView(SearchView):
    """My custom search view."""
    def search_posts(request):
        post_type = str(request.GET.get('CarModel')).lower()
        print (str(request.GET.get('CarModel')).lower())
        sqs = SearchQuerySet().filter(CarModel__contains=post_type)
        clean_query = sqs.query.clean(post_type)
        result = sqs.filter(content=clean_query)
        view = search_view_factory(
            view_class=SearchView,
            template='search/search.html',
            searchqueryset=result,
            form_class=HighlightedSearchForm
            )
        return view(request)

However, I still did not make it work...Search results are still not filtered by 'Model' field. I guess I have to add extra code, but I do not know where...


回答1:


I'll try respond to this. This is how I got it working:

In yourapp/urls.py

url(r'^search/$', views.search_notes, name='search_notes'),

In yourapp/views.py import your NotesSearchForm (further below) and call that on search:

from .forms import NotesSearchForm

def search_notes(request):
   form = NotesSearchForm(request.GET)
   notes = form.search()
   return render_to_response('notes/index.html', {'notes': notes, 'form': form})

   context = { 'latest_notes_list': latest_notes_list}
   return render(request, 'notes/index.html', context)

Then in your yourapp/forms.py is where the magic happens:

from haystack.forms import SearchForm

class NotesSearchForm(SearchForm):

   # First we add the form fields, in your case BooleanFields.
   sports = forms.BooleanField()
   fiction = forms.BooleanField()
   non_fiction = forms.BooleanField()


   def search(self):
      # Here we store the SearchQuerySet received from other processing.
      sqs = super(NotesSearchForm, self).search()

      if not self.is_valid():
         return self.no_query_found()

      # Then filter your results when the search come back

      if self.cleaned_data['sports']:
         sqs = sqs.filter(genre='sports')
      if self.cleaned_data['fiction']:
         sqs = sqs.filter(genre='fiction')
      if self.cleaned_data['non_fiction']:
         sqs = sqs.filter(genre='non_fiction')

      return sqs   

   def no_query_found(self):
      # This we add to make all results show if nothing is selected or searched for (optional)
      return self.searchqueryset.all()

Note that here I put 'genre' as what the field is named in your model. This field also need to be present in your search_indexes.py, like this:

genre = indexes.CharField(model_attr='genre')

Finally, let me show you the yourapp/templates/notes/index.html:

<!-- Form here -->
{{ form }}

<!-- Search results here -->
{% for note in notes %}

   {{ note.object.title }}
   {{ notes.object.body|linebreaks }}">

{% endfor %}

Hope it works for you! Let me know if you get stuck.



来源:https://stackoverflow.com/questions/39753648/django-haystack-search-based-on-multiple-index-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!