etianen / django-watson

Full-text multi-table search application for Django. Easy to install and use, with good performance.
BSD 3-Clause "New" or "Revised" License
1.21k stars 129 forks source link

Optionnal parameter added to return all objects for a blank search #190

Closed ppython closed 8 years ago

ppython commented 8 years ago

I need to be able to return all results even if the query is blank.

This patch adds an optional bool parameter 'blank_search_results' to search(). When set to True, it will return all the objects.

Ex. of usage:

from watson import search as watson

query = ''

# Will return all Watson registered models objects.
search_results = watson.search(query, blank_search_results=True)

# Will return all Your_models objects.
search_results = watson.filter(Your_model, query, blank_search_results=True)
etianen commented 8 years ago

Thanks for the pull request!

This sounds like something that can be trivially implemented in your own code, without requiring a change to django-watson. What advantage is there in adding this parameter to django-watson?

ppython commented 8 years ago

Sure,

Some exemples, the first one being the one for my case:

Now, I could have my own django-watson custom SearchEngine Class and override it's search method to achieve what I need; or might even not use watson and pass by Django ORM directly (or in the same override as well, wich would simplifies the code on the search end) with it's filters; I needed a start in achieving this 'blank query' thing and thought this would be one more little thing watson could achieve :)

etianen commented 8 years ago

I think you can do this much more simply in your own code:

if query == "":
    return SearchEntry.objects.all()
else:
    return watson.search(query)

Given how simple the logic is, I struggle to see the benefit of adding it to django-watson.

ppython commented 8 years ago

Exact, this simple case make the modification to the package useless :)

Just to add if someones stumble upon this; If we want to do it per model:

from django.contrib.contenttypes.models import ContentType
SearchEntry.objects.filter(content_type=ContentType.objects.get_for_model(Your_model))

I'll close this PR, thanks for the discussion anyway!

etianen commented 8 years ago

Cool, this will be nice documentation if anyone else has the same issue. Thanks, Google! :P

Happy coding! 😄