Commits

Matt Chaput committed 9b5f17b

Fixed and explained the logic of how the boolean field turns objects into query values.
See issue #336.

  • Participants
  • Parent commits 80dd7ea

Comments (0)

Files changed (2)

File src/whoosh/fields.py

         self.format = formats.Existence(field_boost=field_boost)
 
     def _obj_to_bool(self, x):
-        if isinstance(x, string_type):
-            x = x.lower() in self.trues
+        # We special case strings such as "true", "false", "yes", "no", but
+        # otherwise call bool() on the query value. This lets you pass objects
+        # as query values and do the right thing.
+
+        if isinstance(x, string_type) and x.lower() in self.trues:
+            x = True
+        elif isinstance(x, string_type) and x.lower() in self.falses:
+            x = False
         else:
             x = bool(x)
         return x

File tests/test_fields.py

         assert not any(reader.is_deleted(hit.docnum) for hit in r)
 
 
+def test_boolean_multifield():
+    schema = fields.Schema(name=fields.TEXT(stored=True),
+                           bit=fields.BOOLEAN(stored=True))
+    ix = RamStorage().create_index(schema)
+    with ix.writer() as w:
+        w.add_document(name=u('audi'), bit=True)
+        w.add_document(name=u('vw'), bit=False)
+        w.add_document(name=u('porsche'), bit=False)
+        w.add_document(name=u('ferrari'), bit=True)
+        w.add_document(name=u('citroen'), bit=False)
+
+    with ix.searcher() as s:
+        qp = qparser.MultifieldParser(["name", "bit"], schema)
+        q = qp.parse(u("boop"))
+
+        r = s.search(q)
+        assert sorted(hit["name"] for hit in r) == ["audi", "ferrari"]
+        assert len(r) == 2
+
+
 def test_missing_field():
     schema = fields.Schema()
     ix = RamStorage().create_index(schema)
                        (b('FF'), 1, 0.5, b('\x00\x00\x00\x01')),
                        (b('SPRS'), 1, 1.0, b('\x00\x00\x00\x01')),
                        ]
+