0

I have a Django app that searches for data from a model Classified and displays them using simple querysets based on the input term. This works perfectly and I've got no complains with this method.

However, if someone enters a term that returns no data, I would like to provide an option with alternate/suggested search.

Eg: Someone searches 'Ambulance Services' which doesn't return data. I'd like to suggest 'Ambulance' or 'Services' as alternate search options which may return data from the model depending on the data present in the model.

I Googled suggested search in django and it gave me options of Haystack/elastic search, etc which I'm not sure are really required since the search is across just one model.

Note: SO tells me that my question may be closed as it is subjective. If thats the case, please suggest where I can move it to. Thank you!

Newtt
  • 6,050
  • 13
  • 68
  • 106

2 Answers2

1

Haystack is, indeed, a great option, here you will find how to give 'spelling suggestions', you can see an example in this OS question/answer

No matter you have only one model, this tool is really great, simple to install /set up/use, and very flexible.

Perhaps this helps as well.

Community
  • 1
  • 1
trinchet
  • 6,753
  • 4
  • 37
  • 60
1

This is just an idea, but might work for you:

  1. The user enters the search data: "Ambulance Services"

  2. If the query inside the view returns nothing, redirect to the same view using your selected alternative search data, lets say "Ambulance", and a flag value thats saids the view you're preforming a suggested search.

You must have two things in consideration:

  1. What if the alternate search don't returns anything either? You have to set a recursion limit here.

  2. How I'm going to select the data of the alternate search? Well, thats another question about a completly different topic.

This is this idea in code:

def search(request, data, alternate=False, recursion_level=3):

    result = Classified.objects.filter(some_filed=data)

    if 0 == result.count() and 0 != recursion_level: # Conditions needed for perform alternate search.

        new_data = select_new_data(data)             # The logic inside this function is up to you.

        r_level = recursion_level - 1        # Decreas recursion level.

        return redirect('search', alternate=True, recursion_level=r_level)  # Redirecting using view name, you can use others
                                                                    # forms of redirection see link below for visit 
                                                                    # the Djando redirect API doc.
    else:
        return_data = {}

        if alternate:
            # You can use this flag in order to reflect
            # to the user you have performed an alternate search. 
            # Example:
            return_data["alternate"] = True


        # Build your return data.
        # and render.

        #return render_to_template()

Django redirect doc: redirect

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60