Skip to content
This repository was archived by the owner on Nov 21, 2017. It is now read-only.

Commit

Permalink
Schemas cleanup: Handle IntNodes properly
Browse files Browse the repository at this point in the history
IntNodes were getting caught by the StandardAnalyzer for small
values. This could probably be avoided by using whoosh.fields.INT
in the schema declaration as well (and that should get done
eventually). However, a SimpleAnalyzer as the default is better for
l2cs' case anyway, because the CloudSearch server will handle
stop words and query optimizations.

Additionally, IntNodes are now properly generating unicode
elements.
  • Loading branch information
kemitche committed Sep 28, 2012
1 parent b9a2a88 commit ec2f866
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
19 changes: 12 additions & 7 deletions l2cs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import sys

import whoosh.analysis
import whoosh.fields
import whoosh.qparser.default
import whoosh.qparser.plugins
Expand All @@ -17,7 +18,7 @@
import whoosh.query


__version__ = "2.0.1"
__version__ = "2.0.2"


HANDLERS = {}
Expand Down Expand Up @@ -102,7 +103,7 @@ def walk_clause(clause):
class IntNode(whoosh.qparser.syntax.WordNode):
def __init__(self, value):
self.__int_value = int(value)
whoosh.qparser.syntax.WordNode.__init__(self, unicode(self.__int_value))
whoosh.qparser.syntax.WordNode.__init__(self, value)

def query(self, parser):
q = whoosh.qparser.syntax.WordNode.query(self, parser)
Expand Down Expand Up @@ -144,10 +145,10 @@ def modify_node(self, fieldname, node):
class YesNoPlugin(PseudoFieldPlugin):
def modify_node(self, fieldname, node):
if node.has_text:
if node.text in ("yes", "y", "1"):
new_node = IntNode(1)
if node.text in (u"yes", u"y", u"1"):
new_node = IntNode(u'1')
else:
new_node = IntNode(0)
new_node = IntNode(u'0')
new_node.set_fieldname(fieldname)
return new_node
else:
Expand Down Expand Up @@ -276,11 +277,15 @@ def make_schema(fields, datefields=()):
additionally create DATETIME fields with those names
'''
fields = dict.fromkeys(fields, whoosh.fields.TEXT)
text_field = whoosh.fields.TEXT(analyzer=whoosh.analysis.SimpleAnalyzer())
fields = dict.fromkeys(fields, text_field)
if datefields:
datefields = dict.fromkeys(datefields, whoosh.fields.DATETIME)
fields.update(datefields)
return whoosh.fields.Schema(**fields)
schema = whoosh.fields.Schema()
for fieldname in fields:
schema.add(fieldname, fields[fieldname])
return schema


def convert(query, parser):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use_setuptools()
from setuptools import setup

version = "2.0.1"
version = "2.0.2"

setup(
name='l2cs',
Expand Down
3 changes: 3 additions & 0 deletions test_l2cs.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ def test_unicode_intnodes1(self):
self._run_test(u"count:1", u"count:1", self.schema_parser)
except AssertionError as e:
self.fail(e)

def test_intnode_empty1(self):
self._run_test(u"count:''", u"")


if __name__ == '__main__':
Expand Down

0 comments on commit ec2f866

Please # to comment.