Example #1
0
 def test_set_children_raises(self):
     test = self._test_set_children_raises
     test(Word("foo"), [Word("foo")])
     test(Phrase('"foo"'), [Word("foo")])
     test(Regex("/foo/"), [Word("foo")])
     test(SearchField("foo", Word("bar")), [])
     test(SearchField("foo", Word("bar")), [Word("bar"), Word("baz")])
     test(Group(Word("foo")), [])
     test(Group(Word("foo")), [Word("foo"), Word("bar")])
     test(FieldGroup(Word("foo")), [])
     test(FieldGroup(Word("foo")), [Word("foo"), Word("bar")])
     test(Range(Word("20"), Word("30")), [])
     test(Range(Word("20"), Word("30")),
          [Word("20"), Word("30"), Word("40")])
     test(Proximity(Word("foo")), [])
     test(Proximity(Word("foo")), [Word("foo"), Word("bar")])
     test(Fuzzy(Word("foo")), [])
     test(Fuzzy(Word("foo")), [Word("foo"), Word("bar")])
     test(Boost(Word("foo"), force=1), [])
     test(Boost(Word("foo"), force=1), [Word("foo"), Word("bar")])
     test(Plus(Word("foo")), [])
     test(Plus(Word("foo")), [Word("foo"), Word("bar")])
     test(Not(Word("foo")), [])
     test(Not(Word("foo")), [Word("foo"), Word("bar")])
     test(Prohibit(Word("foo")), [])
     test(Prohibit(Word("foo")), [Word("foo"), Word("bar")])
Example #2
0
    def test_equality_boost(self):
        b1 = Boost(expr=Word('foo'), force=5)
        b2 = Boost(expr=Word('bar'), force=5)
        b3 = Boost(expr=Word('foo'), force=5)
        b4 = Boost(expr=Word('foo'), force=.5)

        self.assertNotEqual(b1, b2)
        self.assertEqual(b1, b3)
        self.assertNotEqual(b1, b4)
Example #3
0
    def test_boost(self):
        orig = Boost(Word("bar"), force=3.2, pos=3, head="\n", tail="\t")
        copy = orig.clone_item()
        self.assert_equal_tail_head_pos(orig, copy)
        self.assertEqual(orig.force, copy.force)
        self.assertEqual(copy.expr, NONE_ITEM)

        self.assertNotEqual(orig, copy)
        copy.children = [Word("bar")]
        self.assertEqual(orig, copy)
Example #4
0
 def test_boost(self):
     tree = (UnknownOperation(
         Boost(Phrase('"foo bar"'), Decimal("3.0"), tail=" "),
         Boost(
             Group(
                 AndOperation(Word('baz', tail=" "), Word('bar',
                                                          head=" "))),
             Decimal("2.1"))))
     parsed = parser.parse('"foo bar"^3 (baz AND bar)^2.1')
     self.assertEqual(str(parsed), str(tree))
     self.assertEqual(parsed, tree)
Example #5
0
 def test_visit_complex(self):
     tree = AndOperation(
         Group(
             OrOperation(Word("foo"), Word("bar"),
                         Boost(Fuzzy(Word("baz")), force=2))),
         Proximity(Phrase('"spam ham"')),
         SearchField("fizz", Regex("/fuzz/")),
     )
     transformed = self.transform(tree)
     expected = AndOperation(
         Group(
             OrOperation(
                 Word("foo@0-0-0"),
                 Word("bar@0-0-1"),
                 Boost(Fuzzy(Word("baz@0-0-2-0-0")), force=2),
             )),
         Proximity(Phrase('"spam ham@1-0"')),
         SearchField("fizz", Regex("/fuzz@2-0/")),
     )
     self.assertEqual(transformed, expected)
Example #6
0
    def test_simple_propagation(self):
        # propagation in nodes with only one children
        tree = Boost(Group(SearchField("foo", FieldGroup(Plus(Word("bar"))))),
                     force=2)

        paths_ok, paths_ko = self.propagate_matching(tree, set(), {()})
        self.assertEqual(paths_ok, set())
        self.assertEqual(paths_ko, {(), (0, ), (0, 0), (0, 0, 0), (0, 0, 0, 0),
                                    (0, 0, 0, 0, 0)})

        paths_ok, paths_ko = self.propagate_matching(tree, {()}, set())
        self.assertEqual(paths_ok, {(), (0, ), (0, 0), (0, 0, 0), (0, 0, 0, 0),
                                    (0, 0, 0, 0, 0)})
        self.assertEqual(paths_ko, set())
Example #7
0
 def test_boosting(self):
     tree = parser.parse("\rfoo\t^2\n")
     self.assertEqual(tree, Boost(Word("foo"), 2))
     self.assertEqual(tree.head, "")
     self.assertEqual(tree.tail, "\n")
     self.assertEqual(tree.pos, 0)
     self.assertEqual(tree.size, 7)
     foo, = tree.children
     self.assertEqual(foo.head, "\r")
     self.assertEqual(foo.tail, "\t")
     self.assertEqual(foo.pos, 1)
     self.assertEqual(foo.size, 3)
     self.assertEqual(str(tree), "\rfoo\t^2")
     self.assertEqual(tree.__str__(head_tail=True), "\rfoo\t^2\n")
Example #8
0
 def test_boost(self):
     item = Boost(Word("foo"), force="3")
     self.assertEqual(str(item), "foo^3")
     self.assertEqual(repr(item), "Boost(Word('foo'), 3)")
     item = Boost(Word("foo"), force=str(1 / 3))
     self.assertEqual(str(item), "foo^0.3333333333333333")
     # head tail
     item = Boost(Word("foo", head="\t", tail="\n"),
                  force=2,
                  head="\r",
                  tail="  ")
     self.assertEqual(str(item), "\tfoo\n^2")
     self.assertEqual(item.__str__(head_tail=True), "\r\tfoo\n^2  ")
Example #9
0
 def test_check_ok(self):
     query = (AndOperation(
         SearchField(
             "f",
             FieldGroup(
                 AndOperation(
                     Boost(Proximity(Phrase('"foo bar"'), 4), "4.2"),
                     Prohibit(Range("100", "200"))))),
         Group(OrOperation(Fuzzy(Word("baz"), ".8"), Plus(Word("fizz"))))))
     check = LuceneCheck()
     self.assertTrue(check(query))
     self.assertEqual(check.errors(query), [])
     check = LuceneCheck(zeal=1)
     self.assertTrue(check(query))
     self.assertEqual(check.errors(query), [])
Example #10
0
 def test_named_queries_boost(self):
     tree = SearchField("text", Boost(Phrase('"foo bar"'), force=2))
     set_name(tree.children[0], "a")
     result = self.transformer(tree)
     self.assertEqual(
         result,
         {
             "term": {
                 "text": {
                     "value": "foo bar",
                     "_name": "a",
                     'boost': 2.0
                 }
             }
         },
     )
Example #11
0
 def test_visit_complex(self):
     tree = AndOperation(
         Group(
             OrOperation(Word("foo"), Word("bar"),
                         Boost(Fuzzy(Word("baz")), force=2))),
         Proximity(Phrase('"spam ham"')),
         SearchField("fizz", Regex("/fuzz/")),
     )
     paths = self.visit(tree)
     self.assertEqual(sorted(paths), [
         ((0, 0, 0), "foo"),
         ((0, 0, 1), "bar"),
         ((0, 0, 2, 0, 0), "baz"),
         ((1, 0), '"spam ham"'),
         ((2, 0), '/fuzz/'),
     ])
Example #12
0
 def test_set_children(self):
     test = self._test_set_children
     test(Word("foo"), [])
     test(Phrase('"foo"'), [])
     test(Regex("/foo/"), [])
     test(SearchField("foo", Word("bar")), [Word("baz")])
     test(Group(Word("foo")), [Word("foo")])
     test(FieldGroup(Word("foo")), [Word("foo")])
     test(Range(Word("20"), Word("30")), [Word("40"), Word("50")])
     test(Proximity(Word("foo")), [Word("foo")])
     test(Fuzzy(Word("foo")), [Word("foo")])
     test(Boost(Word("foo"), force=1), [Word("foo")])
     many_terms = tuple(Word(f"foo_{i}") for i in range(5))
     test(UnknownOperation(Word("foo"), Word("bar")),
          (Word("foo"), Word("bar")))
     test(UnknownOperation(*many_terms), many_terms)
     test(AndOperation(Word("foo"), Word("bar")),
          (Word("foo"), Word("bar")))
     test(AndOperation(*many_terms), many_terms)
     test(OrOperation(Word("foo"), Word("bar")), (Word("foo"), Word("bar")))
     test(OrOperation(*many_terms), many_terms)
     test(Plus(Word("foo")), [Word("foo")])
     test(Not(Word("foo")), [Word("foo")])
     test(Prohibit(Word("foo")), [Word("foo")])
Example #13
0
    def test_combination(self):
        tree = AndOperation(
            OrOperation(
                SearchField(
                    "mine",
                    FieldGroup(
                        Plus(AndOperation(
                            Word("foo"),
                            Regex("/fizz/"),
                        ), ), ),
                ),
                Boost(
                    Group(
                        AndOperation(
                            Phrase('"ham"'),
                            Word("spam"),
                            Prohibit(Fuzzy(Word("fuzz"))),
                        ), ),
                    force=2,
                ),
            ),
            Not(OrOperation(
                Word('"bar"'),
                Word('"baz"'),
            ), ),
        )
        to_path = simple_naming(tree)

        paths_ok, paths_ko = self.propagate_matching(tree, set())
        self.assertEqual(
            paths_to_names(tree, paths_ok),
            {"prohibit", "not"},
        )
        self.assertEqual(
            paths_to_names(tree, paths_ko),
            {
                "and", "or", "searchfield", "fieldgroup", "plus", "and2",
                "foo", "fizz", "boost", "group", "and3", "ham", "spam",
                "fuzzy", "or2", "bar", "baz"
            },
        )

        # adding matching just enough positive expressions, so that complete expression matches
        paths_ok, paths_ko = self.propagate_matching(
            tree,
            {to_path["foo"], to_path["fizz"], to_path["ham"]},
        )
        self.assertEqual(
            paths_to_names(tree, paths_ok),
            {
                "and", "or", "searchfield", "fieldgroup", "plus", "and2",
                "foo", "fizz", "ham", "prohibit", "not"
            },
        )
        self.assertEqual(
            paths_to_names(tree, paths_ko),
            {"boost", "group", "and3", "spam", "fuzzy", "or2", "bar", "baz"},
        )

        # making everything match
        paths_ok, paths_ko = self.propagate_matching(
            tree,
            {to_path["foo"], to_path["fizz"], to_path["ham"], to_path["spam"]},
        )
        self.assertEqual(
            paths_to_names(tree, paths_ok), {
                "and", "or", "searchfield", "fieldgroup", "plus", "and2",
                "foo", "fizz", "ham", "prohibit", "boost", "group", "and3",
                "spam", "not"
            })
        self.assertEqual(paths_to_names(tree, paths_ko),
                         {"fuzzy", "or2", "bar", "baz"})

        # making everything match, but some negative expression
        paths_ok, paths_ko = self.propagate_matching(
            tree,
            {
                to_path["foo"],
                to_path["fizz"],
                to_path["ham"],
                to_path["spam"],
                to_path["fuzzy"],
                to_path["bar"],
            },
        )
        self.assertEqual(
            paths_to_names(tree, paths_ok),
            {
                "or",
                "searchfield",
                "fieldgroup",
                "plus",
                "and2",
                "foo",
                "fizz",
                "ham",
                "spam",
                "fuzzy",
                "or2",
                "bar",
            },
        )
        self.assertEqual(
            paths_to_names(tree, paths_ko),
            {
                "and",
                "boost",
                "group",
                "and3",
                "prohibit",
                "boost",
                "group",
                "and3",
                "not",
                "baz",
            },
        )