Implementing Django Search

271 views
Skip to first unread message

DF

unread,
Jun 18, 2012, 2:23:27 PM6/18/12
to django...@googlegroups.com
I'm working on my first project and I'm attempting to implement a basic search function where users can search for a specific terms.

There are many options available, most a bit too heavy for what I require. I found this posting which illustrates how to implement a basic search function that sounds ideal: 


The problem: the documentation is a bit incomplete, especially for a newbie. And I could use some help from those with experience on how to implement this.

The first action is to create a file within the project – say search.py – with the following code:

import re

from django.db.models import Q

def normalize_query(query_string,
                    findterms=re.compile(r'"([^"]+)"|(\S+)').findall,
                    normspace=re.compile(r'\s{2,}').sub):
    ''' Splits the query string in invidual keywords, getting rid of unecessary spaces
        and grouping quoted words together.
        Example:
        
        >>> normalize_query('  some random  words "with   quotes  " and   spaces')
        ['some', 'random', 'words', 'with quotes', 'and', 'spaces']
    
    '''
    return [normspace(' ', (t[0] or t[1]).strip()) for t in findterms(query_string)] 

def get_query(query_string, search_fields):
    ''' Returns a query, that is a combination of Q objects. That combination
        aims to search keywords within a model by testing the given search fields.
    
    '''
    query = None # Query to search for every search term        
    terms = normalize_query(query_string)
    for term in terms:
        or_query = None # Query to search for a given term in each field
        for field_name in search_fields:
            q = Q(**{"%s__icontains" % field_name: term})
            if or_query is None:
                or_query = q
            else:
                or_query = or_query | q
        if query is None:
            query = or_query
        else:
            query = query & or_query
    return query

Then the next step would be to import this file into the views – import search (not sure if the app name should proceed this). Then add this view, with the object detail changed to match my model:

def search(request):
    query_string = ''
    found_entries = None
    if ('q' in request.GET) and request.GET['q'].strip():
        query_string = request.GET['q']
        
        entry_query = get_query(query_string, ['title', 'body',])
        
        found_entries = Entry.objects.filter(entry_query).order_by('-pub_date')

    return render_to_response('search/search_results.html',
                          { 'query_string': query_string, 'found_entries': found_entries },
                          context_instance=RequestContext(request))

After this, I'm a bit stumped.

I assume this requires a basic url for the urls.py file, which seems straightforward enough. But I'm not sure how to place this within a template, what tags to use, etc. I have a search bar form on my main template page to which I would like to attach this. But returning the search/search_results.html template with the appropriate tags is a bit head scratching.

This is a fairly long post but if anyone could provide some insight on implementing this search function, it would be much appreciated. Thanks.



Nikolas Stevenson-Molnar

unread,
Jun 18, 2012, 2:40:58 PM6/18/12
to django...@googlegroups.com
I've used Haystack with Whoosh: http://haystacksearch.org/. It's
straight-forward, well documented, and mimics the Django ORM. No need to
parse the query yourself or anything like that, just pass the raw input
to Haystack and enjoy delicious search results :)

_Nik

On 6/18/2012 11:23 AM, DF wrote:
> I'm working on my first project and I'm attempting to implement a
> basic search function where users can search for a specific terms.
>
> There are many options available, most a bit too heavy for what I
> require. I found this posting which illustrates how to implement a
> basic search function that sounds ideal:
>
> http://julienphalip.com/post/2825034077/adding-search-to-a-django-site-in-a-snap#disqus_thread
>
> The problem: the documentation is a bit incomplete, especially for a
> newbie. And I could use some help from those with experience on how to
> implement this.
>
> The first action is to create a file within the project � say
> search.py � with the following code:
> Then the next step would be to import this file into the views �
> import search (not sure if the app name should proceed this). Then add
> this view, with the object detail changed to match my model:
>
> def search(request):
> query_string = ''
> found_entries = None
> if ('q' in request.GET) and request.GET['q'].strip():
> query_string = request.GET['q']
>
> entry_query = get_query(query_string, ['title', 'body',])
>
> found_entries =
> Entry.objects.filter(entry_query).order_by('-pub_date')
>
> return render_to_response('search/search_results.html',
> { 'query_string': query_string,
> 'found_entries': found_entries },
> context_instance=RequestContext(request))
>
> After this, I'm a bit stumped.
>
> I assume this requires a basic url for the urls.py file, which seems
> straightforward enough. But I'm not sure how to place this within a
> template, what tags to use, etc. I have a search bar form on my main
> template page to which I would like to attach this. But returning the
> search/search_results.html template with the appropriate tags is a bit
> head scratching.
>
> This is a fairly long post but if anyone could provide some insight on
> implementing this search function, it would be much appreciated. Thanks.
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/6jWgePRRzQYJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

DF

unread,
Jun 18, 2012, 2:46:34 PM6/18/12
to django...@googlegroups.com
Does it provide instructions on connecting it to a search form? Dumb question, but sometimes documentation can be lacking.
> django-users+unsubscribe@googlegroups.com.

Nikolas Stevenson-Molnar

unread,
Jun 18, 2012, 3:00:08 PM6/18/12
to django...@googlegroups.com
There are a number of ways you can use it. Must straight-forward is to use built-in views with a customized template: http://django-haystack.readthedocs.org/en/latest/tutorial.html#setting-up-the-views

You can get a bit more advanced by using the various search forms with your own view: http://django-haystack.readthedocs.org/en/latest/views_and_forms.html

Or, you can use the basic components (SearchQuerySet) and do everything else yourself: http://django-haystack.readthedocs.org/en/latest/searchqueryset_api.html

Even the last case is pretty easy to use. Connecting it to a search form is as easy as getting the query string from GET or POST data. E.g.:

def my_view(request):
    results = SearchQuerySet().filter(content=AutoQuery(results.GET['q']))

_Nik
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/B-DUxhwUwhAJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.

Daniel Sokolowski

unread,
Jun 19, 2012, 12:08:44 AM6/19/12
to django...@googlegroups.com
I also found that piece of code when I needed to implement search - it
was confusing so I ended up with my own approach that was easier to
understand and simpler at least to me - I pasted an example of live code
here: http://dpaste.org/KYhUq/

The searching piece of code is in the form_valid() method:

query = (query.filter(name__icontains=bit) |
query.filter(description__icontains=bit))

This filters the query down on each loop of the word user typed, you
would adjust that according to your model fields.

Hope that helps.

On 18/06/2012 14:23, DF wrote:
> I'm working on my first project and I'm attempting to implement a
> basic search function where users can search for a specific terms.
>
> There are many options available, most a bit too heavy for what I
> require. I found this posting which illustrates how to implement a
> basic search function that sounds ideal:
>
> http://julienphalip.com/post/2825034077/adding-search-to-a-django-site-in-a-snap#disqus_thread
>
> The problem: the documentation is a bit incomplete, especially for a
> newbie. And I could use some help from those with experience on how to
> implement this.
>
> The first action is to create a file within the project � say
> search.py � with the following code:
> Then the next step would be to import this file into the views �
> import search (not sure if the app name should proceed this). Then add
> this view, with the object detail changed to match my model:
>
> def search(request):
> query_string = ''
> found_entries = None
> if ('q' in request.GET) and request.GET['q'].strip():
> query_string = request.GET['q']
> entry_query = get_query(query_string, ['title', 'body',])
> found_entries =
> Entry.objects.filter(entry_query).order_by('-pub_date')
>
> return render_to_response('search/search_results.html',
> { 'query_string': query_string,
> 'found_entries': found_entries },
> context_instance=RequestContext(request))
>
> After this, I'm a bit stumped.
>
> I assume this requires a basic url for the urls.py file, which seems
> straightforward enough. But I'm not sure how to place this within a
> template, what tags to use, etc. I have a search bar form on my main
> template page to which I would like to attach this. But returning the
> search/search_results.html template with the appropriate tags is a bit
> head scratching.
>
> This is a fairly long post but if anyone could provide some insight on
> implementing this search function, it would be much appreciated. Thanks.
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/6jWgePRRzQYJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
--
Daniel Sokolowski
Web Engineer
Danols Web Engineering
http://webdesign.danols.com/

DF

unread,
Jun 19, 2012, 3:26:50 PM6/19/12
to django...@googlegroups.com
Thank you. I ended up using Haystack but I appreciate your insight.
> django-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages