Sunday, February 3, 2013

Re: search across models

On Sun, 3 Feb 2013 13:11:55 +0530, Ashwin Kumar <aswin.1231@gmail.com>
declaimed the following in gmane.comp.python.django.user:

> thank you joey,
>
> but i want to build search for queries like
>
>
> 1. restaurants in city1
> 2. city1 restaurants
> 3. Chinese restaurants
> 4. Chinese restaurants in city1
> 5. vegetarian restaurants in city1
> 6. vegetarian american restaurants
>
> the above are exact search queries we get from template.
> i wrote
> restas = restaurant.objects.filter(Q(name__contains =
> request.REQUEST.get('title')) & ~Q(city__name='') )
> this works fine to search with restaurant name.
>
> i really have no idea on how to get results if the search phrase is
> complex.
>
I've not been following, and don't know the schema, but for such
searches I suspect you'll have to bypass Django's ORM and go to direct
SQL...

You'd split the search on words, skip the noise terms ("in", "and",
"the", etc.), and then create a select statement in which you've created
a test for each remaining word:

select ... from ...
where field contains <word1> and field contains <word2> and ...

If you have two (or more) fields that need to be matched, it gets
more complex (unless you build a separate index containing terms from
all relevant fields).
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment