views:

75

answers:

2

I have one database with column - where is datas like

'Very big News'
'News'
'something else'
'New Nes'
'Fresh News'
'Something else'

I need match with my database.I Need go through every row in this database and find if i have string field which is inside of this string field.

for example if i have field 'News' which is inside.

I need implement it in django.

How to mutch if i have field with name 'News' in my database?

I mean if i have value in my row which is contained in this particular string?

I can drop this string by list of words and than check every word. But it is not the best way. Isn't it?

Again:

I have string 'super very news' I need look in my database if i have name field which contain word 'super' or 'very' or 'new' or 'super very' or 'very news'.

+2  A: 

Update based on comments. See the query set docs here.

your_search_query = 'super very news'

qset = Q()
for term in your_search_query.split():
    qset |= Q(name__contains=term)

matching_results = YourModel.objects.filter(qset)

This creates the equivalent of:

matching_result = YourModel.objects.filter(Q(name__contains='super') |
                                           Q(name__contains='very') |   
                                           Q(name__contains='news'))

which produces (roughly) the following SQL in a single query:

 select * from your_model where name like '%super%' or name like '%very%' or name like '%news%'
sdolan
No. You did not understand. I want to look not in remote database.I want to look in my database if i have data which is inside string for example 'news about something' i need look if i have field 'news' or 'about' or 'something'
Pol
You want to do the search in your database model that holds the remote data.
sdolan
i think he wants to do a `LIKE *news*` search -- keyword within a string -- but within any of the fields, not just one...
eruciform
Yes! You right How can I do it wittout making request for every word?
Pol
No! No LIKE *news*. I have string 'super very news' I need look in my database if i have name field which contain word 'super' or 'very' or new' or 'super very' or 'very news'. Got it?
Pol
@Pol: Did my update solve your issue?
sdolan
I'm assuming since you accepted it, that's a yes.
sdolan
Yes! But it is pretty difficult for me.
Pol
I cant even imagin how it could be implement in SQL.
Pol
@Pol: Which part is difficult? Do you need more explanation on what's going on in my answer?
sdolan
In real it will make few request to database?
Pol
@Pol: I've updated the answer with some more info. Also, I'd read through the link I provided at the top for more information.
sdolan
You are the man. I could spend 1 day to find out how it work. Now I understend it totaly. If I could get book writen by you about django :)
Pol
A: 

If your word list is small and simple you can write your own lookup table and indexer, otherwise you want a full text database like OpenFTS:

http://openfts.sourceforge.net/

joel3000