Permalink
Browse files

Adding parsing infrastructure for filtered queries.

  • Loading branch information...
1 parent 7799ccf commit cf242432c7cb16b37b2074472627033eb235c4fe @apendleton apendleton committed Apr 9, 2012
Showing with 24 additions and 1 deletion.
  1. +3 −1 requirements.txt
  2. +21 −0 sparerib/sparerib_api/query_parse.py
View
@@ -6,4 +6,6 @@ wsgiref==0.1.2
pymongo
djangorestframework
isoweek
--e git+https://github.com/aparo/pyes.git#egg=pyes
+thrift
+-e git+https://github.com/aparo/pyes.git#egg=pyes
+pyparsing
@@ -0,0 +1,21 @@
+from pyparsing import Word, Optional, ZeroOrMore, Or, QuotedString, Group, Literal, printables, alphas
+
+def SearchSyntax():
+ printables_without_colon = ''.join(letter for letter in printables if letter != ':')
+ Colon = Literal(":").suppress()
+
+ Filter = Group(Word(printables_without_colon) + Colon + Word(printables_without_colon) + Optional(Colon + (QuotedString('"', "\\") | Word(printables)))).setResultsName('filters')
+ Filter.modalResults = False
+
+ TextTerm = (QuotedString('"', "\\", unquoteResults=False) | Word(printables)).setResultsName('text_terms')
+ TextTerm.modalResults = False
+
+ Term = Filter | TextTerm
+ Query = ZeroOrMore(Term)
+ return Query
+
+_syntax = SearchSyntax()
+
+def parse_query(query):
+ parsed = _syntax.parseString(query)
+ return {'text': ' '.join(parsed.text_terms), 'filters': parsed.filters.asList() if parsed.filters else []}

0 comments on commit cf24243

Please sign in to comment.