I have also wrestled with this in the past. I have ripped some unclean
code below out of an app I wrote. It may give someone inspiration but it
is hardly a finished product. It would have poor performance but I must
say the concept works well for me, I just hope it is remotely
understandable :)
I believe that shlex allows me to do searches on multiple words in
quotations, eg: apple banana "cherry pie"
def _search_form(self,url=''):
form = FORM('',
INPUT(_name='searchwords',_value=request.get_vars.searchwords,
_style='width:200px;',
_id='web2py_searchwords'),
INPUT(_type='submit',_value=T('Search')),
INPUT(_type='submit',_value=T('Clear'),
_onclick="jQuery('#web2py_searchwords').val('');"),
_method="GET",_action=url)
return form
def _search_query(tableid, search_text, fields):
import shlex
try:
words = [x for x in shlex.split( search_text.upper() )] ifsearch_text
else []
except:
response.flash = 'Invalid Query'
words = []
query = None
if words:
for field in fields:
for word in words:
if query == None: query = field.upper().contains(word)
else: query = query | field.upper().contains(word)
# find any tags
rows = db(db.tag.name.upper().belongs(words)).select(db.tag.id)
ids = []
for row in rows:
ids.append(row.id)
for i in ids:
if query == None: query = db.filestore.tag_id.contains(i)
else: query = query | db.filestore.tag_id.contains(i)
return query or tableid > 0
def myfunction():
...
search_text = request.get_vars.searchwords
query = _search_query(db.filestore.id, search_text, [db.filestore.title
])
grid = SQLFORM.grid(query, search_widget=_search_form, ....
--