Exemple #1
0
 def test_and_filter2(self):
     exp = GreaterEqual("a", 2) & Less("b", 3) & Equal("c", 4)
     print(exp.build())
     assert exp.build() == {
         "bool": {
             "must": [
                 {
                     "range": {
                         "a": {
                             "gte": 2
                         }
                     }
                 },
                 {
                     "range": {
                         "b": {
                             "lt": 3
                         }
                     }
                 },
                 {
                     "term": {
                         "c": 4
                     }
                 },
             ]
         }
     }
Exemple #2
0
 def test_and_or_filter(self):
     exp = GreaterEqual("a", 2) & (Less("b", 3) | Equal("c", 4))
     assert exp.build() == {
         "bool": {
             "must": [
                 {
                     "range": {
                         "a": {
                             "gte": 2
                         }
                     }
                 },
                 {
                     "bool": {
                         "should": [{
                             "range": {
                                 "b": {
                                     "lt": 3
                                 }
                             }
                         }, {
                             "term": {
                                 "c": 4
                             }
                         }]
                     }
                 },
             ]
         }
     }
Exemple #3
0
 def test_must_or_must_not_filter(self):
     exp = (GreaterEqual("a", 2)
            | GreaterEqual("b", 2)) | ~(IsIn("ids", [1, 2, 3]))
     a = exp.build()
     b = {
         "bool": {
             "should": [
                 {
                     "range": {
                         "a": {
                             "gte": 2
                         }
                     }
                 },
                 {
                     "range": {
                         "b": {
                             "gte": 2
                         }
                     }
                 },
                 {
                     "bool": {
                         "must_not": {
                             "ids": {
                                 "values": [1, 2, 3]
                             }
                         }
                     }
                 },
             ]
         }
     }
     assert a == b
Exemple #4
0
 def test_or_filter1(self):
     exp = GreaterEqual("a", 2) | Less("b", 3)
     assert exp.build() == {
         "bool": {
             "should": [{"range": {"a": {"gte": 2}}}, {"range": {"b": {"lt": 3}}}]
         }
     }
Exemple #5
0
 def test_not_and_filter(self):
     exp = ~(GreaterEqual("a", 2) & Less("b", 3))
     assert exp.build() == {
         "bool": {
             "must_not": {
                 "bool": {
                     "must": [
                         {
                             "range": {
                                 "a": {
                                     "gte": 2
                                 }
                             }
                         },
                         {
                             "range": {
                                 "b": {
                                     "lt": 3
                                 }
                             }
                         },
                     ]
                 }
             }
         }
     }
Exemple #6
0
 def test_leaf_boolean_filter(self):
     assert GreaterEqual("a", 2).build() == {"range": {"a": {"gte": 2}}}
     assert LessEqual("a", 2).build() == {"range": {"a": {"lte": 2}}}
     assert Less("a", 2).build() == {"range": {"a": {"lt": 2}}}
     assert Equal("a", 2).build() == {"term": {"a": 2}}
     exp = Equal("a", 2)
     assert (~exp).build()["bool"], {"must_not": {"term": {"a": 2}}}
     assert Greater("a", 2).build() == {"range": {"a": {"gt": 2}}}
     assert IsIn("a", [1, 2, 3]).build() == {"terms": {"a": [1, 2, 3]}}
     assert Like("a", "a*b").build() == {"wildcard": {"a": "a*b"}}
     assert Rlike("a", "a*b").build() == {"regexp": {"a": "a*b"}}
     assert Startswith("a", "jj").build() == {"prefix": {"a": "jj"}}
     assert IsNull("a").build() == {"missing": {"field": "a"}}
     assert NotNull("a").build() == {"exists": {"field": "a"}}
     assert ScriptFilter(
         'doc["num1"].value > params.param1', lang="painless", params={"param1": 5}
     ).build() == {
         "script": {
             "script": {
                 "lang": "painless",
                 "source": 'doc["num1"].value > params.param1',
                 "params": {"param1": 5},
             }
         }
     }
     assert IsIn("ids", [1, 2, 3]).build() == {"ids": {"values": [1, 2, 3]}}
Exemple #7
0
 def __ge__(self, other: Union[int, float, "Series"]) -> BooleanFilter:
     if isinstance(other, Series):
         # Need to use scripted query to compare to values
         painless = f"doc['{self.name}'].value >= doc['{other.name}'].value"
         return ScriptFilter(painless, lang="painless")
     elif isinstance(other, (int, float)):
         return GreaterEqual(field=self.name, value=other)
     else:
         raise NotImplementedError(other, type(other))
Exemple #8
0
 def test_not_filter(self):
     exp = ~GreaterEqual("a", 2)
     assert exp.build() == {
         "bool": {
             "must_not": {
                 "range": {
                     "a": {
                         "gte": 2
                     }
                 }
             }
         }
     }