Hi All,
I want to support multiple keywords search as well as web2py's built-in-search.
I've written one module which will check if in_built_search is called or not.
If in_built_search isn't called then search keywords in custom_module
def built_in_search(keywords):
"""This module will return True if opertor
in built_in_search is used in keywords"""
search_operators = ['=', '!=', '<', '>', '<=', '>=',
'starts with', 'contains', 'in', 'not in']
return any(operator in keywords for operator in search_operators)
Code of SQLFORM.GRID in controllers/default.py
keywords = request.vars.keywords
if keywords and not built_in_search(keywords):
new_query, usr_company_exist = _custom_search_query(keywords, field_dicts)
#field_dicts contains field_name and table attribute.
grid = SQLFORM.grid(query, create=allow_create,
orderby=~db.table_name.created_on,
showbuttontext=False, csv=False, deletable=False,
maxtextlengths=textlengths, searchable=True)
Here I am building query according to request.vars.keywords
I've written a custom module to search keywords on selected attributes as :
It also searches on reference key (company_name)
def _custom_search_query(keywords, field_dicts):
"""This module will build search each keyword in keywords
in predefined table attributes"""
keys = keywords.strip()
user_company_exist = False
import time
start_time = time.time()
filters = []
print "\n Keywords", keys
words = keys.split(' ')
for field_name, db_table_attr in field_dicts.iteritems():
#check for company name
if field_name == 'company_name':
company = db(db.company.name.contains(keys)).select().first()
if company is not None:
continue
all_words_filter = []
for word in words:
all_words_filter.append(db_table_attr.contains(word))
filters.append(reduce(lambda a, b: (a & b), all_words_filter))
return reduce(lambda a, b: a | b, filters)
If I tried to search 'XYZ' (First_name_of_company) in search box, then it gives me incorrect records.
Actually It shows only 3 records.
I've printed count of query in pdb.
It is showing exact count as in database.
If I tried to search 'XYZ Private' (first and middle name of company) in search box, then It gives me correct records.
I've also printed query count in pdb.
(Pdb) db(query).count()
139L
It also shows exact count as in database.
Does any one know why is it giving different results?