Beispiel #1
0
    def test_nested(self):
        n1 = Nested(query=Term(field="some_nested_path.id", value=2),
                    path="some_nested_path")
        n2 = Nested(query={"term": {
            "some_nested_path.id": 2
        }},
                    path="some_nested_path")

        for i, n in enumerate((n1, n2)):
            self.assertEqual(
                n.to_dict(),
                {
                    "nested": {
                        "path": "some_nested_path",
                        "query": {
                            "term": {
                                "some_nested_path.id": {
                                    "value": 2
                                }
                            }
                        },
                    }
                },
            )
            self.assertEqual(
                n.__str__(),
                """<Query>
nested, path="some_nested_path"
└── query
    └── term, field=some_nested_path.id, value=2
""",
            )
Beispiel #2
0
    def test_nested(self):
        # test all possibles definitions
        n1 = Nested(query=Term(field="some_nested_path.id", value=2),
                    path="some_nested_path")
        n2 = Nested(query={"term": {
            "some_nested_path.id": 2
        }},
                    path="some_nested_path")
        n3 = Nested({
            "query": {
                "term": {
                    "some_nested_path.id": 2
                }
            },
            "path": "some_nested_path"
        })
        for i, n in enumerate((n1, n2, n3)):
            self.assertEqual(len(n._children), 2)
            self.assertEqual(n.line_repr(depth=None), "nested")
            self.assertEqual(n.path, "some_nested_path")

            q = next((c for c in n._children if isinstance(c, QueryP)))
            self.assertEqual(q.serialize(), {"query": {}})
            # ensure term query is present
            self.assertEqual(len(q._children), 1)
            self.assertIsInstance(q._children[0], Term, i)

            p = next((c for c in n._children if isinstance(c, Path)))
            self.assertEqual(p.body["value"], "some_nested_path")
Beispiel #3
0
    def test_boosting(self):
        b1 = Boosting(
            positive=Term(field="text", value="apple"),
            negative=Term(field="text", value="pie tart fruit crumble tree"),
            negative_boost=0.5,
        )

        b2 = Boosting(
            positive={"term": {
                "text": "apple"
            }},
            negative={"term": {
                "text": "pie tart fruit crumble tree"
            }},
            negative_boost=0.5,
        )

        for b in (b1, b2):
            self.assertQueryEqual(
                b.to_dict(),
                {
                    "boosting": {
                        "negative": {
                            "term": {
                                "text": {
                                    "value": "pie tart fruit crumble "
                                    "tree"
                                }
                            }
                        },
                        "negative_boost": 0.5,
                        "positive": {
                            "term": {
                                "text": {
                                    "value": "apple"
                                }
                            }
                        },
                    }
                },
            )
Beispiel #4
0
def test_term_clause():
    body = {"user": {"value": "Kimchy", "boost": 1}}
    expected = {"term": body}

    q1 = Term(field="user", value="Kimchy", boost=1)
    q2 = Term(user={"value": "Kimchy", "boost": 1})
    for q in (q1, q2):
        assert q.body == body
        assert q.to_dict() == expected
        assert q.line_repr(depth=None) == (
            "term",
            'field=user, boost=1, value="Kimchy"',
        )

    # other format
    q3 = Term(user="******")
    assert q3.body == {"user": {"value": "Kimchy"}}
    assert q3.to_dict() == {"term": {"user": {"value": "Kimchy"}}}
    assert q3.line_repr(depth=None) == ("term", 'field=user, value="Kimchy"')
    def test_term_clause(self):
        body = {"user": {"value": "Kimchy", "boost": 1}}
        expected = {"term": body}

        q1 = Term(field="user", value="Kimchy", boost=1)
        q2 = Term(user={"value": "Kimchy", "boost": 1})
        for q in (q1, q2):
            self.assertEqual(q.body, body)
            self.assertEqual(q.serialize(), expected)
            self.assertEqual(q.line_repr(depth=None),
                             'term, field=user, boost=1, value="Kimchy"')

        # other format
        q3 = Term(user="******")
        self.assertEqual(q3.body, {"user": {"value": "Kimchy"}})
        self.assertEqual(q3.serialize(),
                         {"term": {
                             "user": {
                                 "value": "Kimchy"
                             }
                         }})
        self.assertEqual(q3.line_repr(depth=None),
                         'term, field=user, value="Kimchy"')
Beispiel #6
0
    def test_bool(self):
        # test all possibles definitions
        b1 = Bool(
            filter=Term(some_field=2),
            should=[Range(other={"gte": 1}),
                    Term(some=3)],
            boost=1.2,
        )
        b2 = Bool(
            filter=[Term(some_field=2)],
            should=[Range(other={"gte": 1}),
                    Term(some=3)],
            boost=1.2,
        )
        b3 = Bool(
            filter={"term": {
                "some_field": 2
            }},
            should=[{
                "range": {
                    "other": {
                        "gte": 1
                    }
                }
            }, {
                "term": {
                    "some": 3
                }
            }],
            boost=1.2,
        )
        b4 = Bool(
            filter=[{
                "term": {
                    "some_field": 2
                }
            }],
            should=[{
                "range": {
                    "other": {
                        "gte": 1
                    }
                }
            }, {
                "term": {
                    "some": 3
                }
            }],
            boost=1.2,
        )

        for b in (b1, b2, b3, b4):
            self.assertQueryEqual(
                b.to_dict(),
                {
                    "bool": {
                        "boost":
                        1.2,
                        "filter": [{
                            "term": {
                                "some_field": {
                                    "value": 2
                                }
                            }
                        }],
                        "should": [
                            {
                                "range": {
                                    "other": {
                                        "gte": 1
                                    }
                                }
                            },
                            {
                                "term": {
                                    "some": {
                                        "value": 3
                                    }
                                }
                            },
                        ],
                    }
                },
            )
Beispiel #7
0

from pandagg.query import Nested, Bool, Query, Range, Term, Terms as TermsFilter

# warning, pandagg.query.Terms and pandagg.agg.Terms classes have same name, but one is a filter, the other an aggreggation

q = Query(
    Bool(
        must=[
            TermsFilter("genres", terms=["Action", "Thriller"]),
            Range("rank", gte=7),
            Nested(
                path="roles",
                query=Bool(
                    must=[
                        Term("roles.gender", value="F"),
                        Term("roles.role", value="Reporter"),
                    ]
                ),
            ),
        ]
    )
)


# In[31]:


# query computation
q.to_dict() == expected_query
Beispiel #8
0
    def test_bool(self):
        # test all possibles definitions
        b1 = Bool(
            filter=Term(some_field=2),
            should=[Range(other={"gte": 1}),
                    Term(some=3)],
            boost=1.2,
        )
        b2 = Bool(
            filter=[Term(some_field=2)],
            should=[Range(other={"gte": 1}),
                    Term(some=3)],
            boost=1.2,
        )
        b3 = Bool(
            filter={"term": {
                "some_field": 2
            }},
            should=[{
                "range": {
                    "other": {
                        "gte": 1
                    }
                }
            }, {
                "term": {
                    "some": 3
                }
            }],
            boost=1.2,
        )
        b4 = Bool(
            filter=[{
                "term": {
                    "some_field": 2
                }
            }],
            should=[{
                "range": {
                    "other": {
                        "gte": 1
                    }
                }
            }, {
                "term": {
                    "some": 3
                }
            }],
            boost=1.2,
        )
        b5 = Bool({
            "filter": {
                "term": {
                    "some_field": 2
                }
            },
            "should": [{
                "range": {
                    "other": {
                        "gte": 1
                    }
                }
            }, {
                "term": {
                    "some": 3
                }
            }],
            "boost":
            1.2,
        })
        b6 = Bool({
            "filter": [{
                "term": {
                    "some_field": 2
                }
            }],
            "should": [{
                "range": {
                    "other": {
                        "gte": 1
                    }
                }
            }, {
                "term": {
                    "some": 3
                }
            }],
            "boost":
            1.2,
        })
        b7 = Bool(
            {
                "should": [{
                    "range": {
                        "other": {
                            "gte": 1
                        }
                    }
                }, {
                    "term": {
                        "some": 3
                    }
                }],
                "boost": 1.2,
            },
            filter=[{
                "term": {
                    "some_field": 2
                }
            }],
        )
        for b in (b1, b2, b3, b4, b5, b6, b7):
            self.assertEqual(len(b._children), 3)
            self.assertEqual(b.line_repr(depth=None), "bool")

            boost = next((c for c in b._children if isinstance(c, Boost)))
            self.assertEqual(boost.serialize(), {"boost": 1.2})
            self.assertEqual(boost.line_repr(depth=None), "boost=1.2")

            filter_ = next((c for c in b._children if isinstance(c, Filter)))
            self.assertEqual(len(filter_._children), 1)
            self.assertEqual(filter_.line_repr(depth=None), "filter")
            t = filter_._children[0]
            self.assertIsInstance(t, Term)

            should = next((c for c in b._children if isinstance(c, Should)))
            self.assertEqual(len(should._children), 2)
            next((c for c in should._children if isinstance(c, Term)))
            next((c for c in should._children if isinstance(c, Range)))
Beispiel #9
0
    def test_boosting(self):
        b1 = Boosting(
            positive=Term(field="text", value="apple"),
            negative=Term(field="text", value="pie tart fruit crumble tree"),
            negative_boost=0.5,
        )

        b2 = Boosting(
            positive={"term": {
                "text": "apple"
            }},
            negative={"term": {
                "text": "pie tart fruit crumble tree"
            }},
            negative_boost=0.5,
        )

        b3 = Boosting({
            "positive": {
                "term": {
                    "text": "apple"
                }
            },
            "negative": {
                "term": {
                    "text": "pie tart fruit crumble tree"
                }
            },
            "negative_boost": 0.5,
        })
        for b in (b1, b2, b3):
            self.assertEqual(len(b._children), 3)
            self.assertEqual(b.line_repr(depth=None), "boosting")

            negative_boosting = next(
                (c for c in b._children if isinstance(c, NegativeBoost)))
            self.assertEqual(negative_boosting.line_repr(depth=None),
                             "negative_boost=0.5")
            self.assertEqual(negative_boosting.body, {"value": 0.5})

            positive = next(
                (c for c in b._children if isinstance(c, Positive)))
            self.assertEqual(len(positive._children), 1)
            self.assertEqual(positive.serialize(), {"positive": {}})
            self.assertEqual(positive.line_repr(depth=None), "positive")
            positive_term = positive._children[0]
            self.assertIsInstance(positive_term, Term)
            self.assertEqual(positive_term.field, "text")
            self.assertEqual(positive_term.body, {"text": {"value": "apple"}})
            self.assertEqual(positive_term.serialize(),
                             {"term": {
                                 "text": {
                                     "value": "apple"
                                 }
                             }})
            self.assertEqual(positive_term.line_repr(depth=None),
                             'term, field=text, value="apple"')

            negative = next(
                (c for c in b._children if isinstance(c, Negative)))
            self.assertEqual(len(negative._children), 1)
            self.assertEqual(negative.serialize(), {"negative": {}})
            self.assertEqual(negative.line_repr(depth=None), "negative")
            negative_term = negative._children[0]
            self.assertIsInstance(negative_term, Term)
            self.assertEqual(negative_term.field, "text")
            self.assertEqual(
                negative_term.body,
                {"text": {
                    "value": "pie tart fruit crumble tree"
                }})
            self.assertEqual(
                negative_term.serialize(),
                {"term": {
                    "text": {
                        "value": "pie tart fruit crumble tree"
                    }
                }},
            )
            self.assertEqual(
                negative_term.line_repr(depth=None),
                'term, field=text, value="pie tart fruit crumble tree"',
            )