AutoSuggest with Django-Haystack – Real Life Implementation

by Applied Informatics

In the last post we implemented Autocomplete on to one of our web apps. Here I will discuss how Autocomplete was implemented into one of our own websites, the MMRF Clinical Trials Finder site.

The criteria required for this particular site, was for Autosuggest to take a user input and return suggestions divided into 3 categories:

  1. Condition term matches such as “myeloma”
  2. Treatment term matches such as “velcade”
  3. Sponsors name matches such as “Onyx”

To build this feature, the above mentioned criteria were indexed into EdgeNgrams as noted in the previous post.

sitenames_auto=EdgeNgramField()
lead_sponsor_auto=EdgeNgramField()
display_conditions_auto=EdgeNgramField(model_attr='display_conditions')
treatment_name_auto=EdgeNgramField()

The fields that did not correspond to an existing model, had to be prepared.

NEXT, moving on to fetching this indexed data, we filtered data based on user’s query input and limited each input to 5 results. The following example is for “conditions”:

condition_auto =SearchQuerySet().autocomplete(display_conditions_auto=request.GET.get('q', ''))[:5]

condition_suggestions = [result.display_conditions_auto for result in condition_auto]

THEN, we did some house cleaning by removing characters we did not want such as extra brackets and whitespace. The resulting data was appended to an empty list and encoded it as a JSON object for easy retrieval in the front end.

Our JSON looks as such:

[
	{
		"category": "Sponsor", 
		"label": "M.D. Anderson Cancer Center"
	}, 
	{	
		"category": "Sponsor", 
		"label": "Boston Medical Center"
	}
]

NEXT, using JQuery UI’s convenient autocomplete plug in and with the help of their handy example to filter categories, autocomplete feature was set up!

Screen Shot 2015-08-14 at 3.24.36 AM

Make sure to drop me a comment on your experience with Autosuggest.

Leave a Reply

Your email address will not be published. Required fields are marked *

Tools & Practices

Tools and Technologies we use at Applied

Contact us now

Popular Posts