Skip to content

Commit

Permalink
updating search engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Fantomas42 committed Feb 22, 2011
1 parent a5472df commit 9d71ca6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 29 deletions.
4 changes: 2 additions & 2 deletions docs/search_engines.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ Here a list of examples and possibilities :
'berlin' with the term 'love'.

* Complex example :
*((paris or berlin) and (tag:love or category:meeting) girl -money*
*((paris or berlin) and (tag:love or category:meet\*) girl -money*

This will returns all the entries containing the terms 'paris' or
'berlin' with the tag 'love' or filled under the category 'meeting'
'berlin' with the tag 'love' or filled under the categories starting by 'meet'
also containing the term 'girl' excluding entries with the term 'money'.
41 changes: 14 additions & 27 deletions zinnia/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ def createQ(token):
query = getattr(token, 'query', '')
wildcards = None

# unicode -> Quoted string
if isinstance(query, basestring):
if isinstance(query, basestring): # Unicode -> Quoted string
search = query

# list -> No quoted string (possible wildcards)
else:
else: # List -> No quoted string (possible wildcards)
if len(query) == 1:
search = query[0]
elif len(query) == 3:
Expand All @@ -44,8 +41,13 @@ def createQ(token):
wildcards = 'END'
search = query[0]

if not meta:
return Q(content__icontains=search) | \
Q(excerpt__icontains=search) | \
Q(title__icontains=search)

if meta == 'category':
if wildcards == "BOTH":
if wildcards == 'BOTH':
return Q(categories__title__icontains=search) | \
Q(categories__slug__icontains=search)
elif wildcards == 'START':
Expand All @@ -57,15 +59,6 @@ def createQ(token):
else:
return Q(categories__title__iexact=search) | \
Q(categories__slug__iexact=search)
elif meta == 'tag': # TODO: tags ignore wildcards
if wildcards == 'BOTH':
return Q(tags__icontains=search)
elif wildcards == 'START':
return Q(tags__icontains=search)
elif wildcards == 'END':
return Q(tags__icontains=search)
else:
return Q(tags__icontains=search)
elif meta == 'author':
if wildcards == 'BOTH':
return Q(authors__username__icontains=search)
Expand All @@ -75,10 +68,8 @@ def createQ(token):
return Q(authors__username__istartswith=search)
else:
return Q(authors__username__iexact=search)
else:
return Q(content__icontains=search) | \
Q(excerpt__icontains=search) | \
Q(title__icontains=search)
elif meta == 'tag': # TODO: tags ignore wildcards
return Q(tags__icontains=search)


def unionQ(token):
Expand All @@ -88,18 +79,14 @@ def unionQ(token):
negation = False

for t in token:
# See tokens recursively
if type(t) is ParseResults:
if type(t) is ParseResults: # See tokens recursively
query &= unionQ(t)
else:
# Set the new op and go to next token
if t in ('or', 'and'):
if t in ('or', 'and'): # Set the new op and go to next token
operation = t
# Next tokens needs to be negated
elif t == '-':
elif t == '-': # Next tokens needs to be negated
negation = True
# Append to query the token
else:
else: # Append to query the token
if negation:
t = ~t
if operation == 'or':
Expand Down
2 changes: 2 additions & 0 deletions zinnia/tests/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ def test_entry_published_manager_advanced_search(self):
self.assertEquals(Entry.published.advanced_search('author:*master or category:cate*').count(), 2)
self.assertEquals(Entry.published.advanced_search('category:*ate*').count(), 2)
self.assertEquals(Entry.published.advanced_search('author:"webmast*"').count(), 0)
self.assertEquals(Entry.published.advanced_search('tag:"zinnia*"').count(), 0)
self.assertEquals(Entry.published.advanced_search('tag:*inni*').count(), 2)


def test_entry_published_manager_advanced_search_with_punctuation(self):
Expand Down

0 comments on commit 9d71ca6

Please sign in to comment.