Browse files

Adding minimum_should_match to TextQuery and adding MatchQuery alias

  • Loading branch information...
1 parent 017c59c commit 89714b12608673c00bd64581f48af65bfe0f695e Taras Shkvarchuk committed Mar 10, 2013
Showing with 40 additions and 3 deletions.
  1. +10 −3 pyes/query.py
  2. +30 −0 tests/test_queries.py
View
13 pyes/query.py
@@ -895,16 +895,16 @@ class TextQuery(Query):
def __init__(self, field, text, type="boolean", slop=0, fuzziness=None,
prefix_length=0, max_expansions=2147483647, operator="or",
- analyzer=None, boost=1.0, **kwargs):
+ analyzer=None, boost=1.0, minimum_should_match=None, **kwargs):
super(TextQuery, self).__init__(**kwargs)
self.queries = {}
self.add_query(field, text, type, slop, fuzziness,
prefix_length, max_expansions,
- operator, analyzer, boost)
+ operator, analyzer, boost, minimum_should_match)
def add_query(self, field, text, type="boolean", slop=0, fuzziness=None,
prefix_length=0, max_expansions=2147483647,
- operator="or", analyzer=None, boost=1.0):
+ operator="or", analyzer=None, boost=1.0, minimum_should_match=None):
if type not in self._valid_types:
raise QueryError("Invalid value '%s' for type: allowed values are %s" % (type, self._valid_types))
if operator not in self._valid_operators:
@@ -926,11 +926,18 @@ def add_query(self, field, text, type="boolean", slop=0, fuzziness=None,
query["boost"] = boost
if analyzer:
query["analyzer"] = analyzer
+ if minimum_should_match:
+ query["minimum_should_match"] = minimum_should_match
self.queries[field] = query
def _serialize(self):
return self.queries
+class MatchQuery(TextQuery):
+ """
+ Replaces TextQuery
+ """
+ _internal_name = "match"
class MultiMatchQuery(Query):
"""
View
30 tests/test_queries.py
@@ -452,5 +452,35 @@ def test_Search_fields(self):
for field in ['name','position']:
self.assertTrue( result.get(field))
+ def test_MatchQuery(self):
+ q = MatchQuery("_all", "nice")
+ serialized = q.serialize()
+ self.assertTrue("match" in serialized)
+ self.assertTrue("_all" in serialized["match"])
+ self.assertTrue(serialized["match"]["_all"]["query"], "nice")
+
+ resultset = self.conn.search(query=q, indices=self.index_name, doc_types=[self.document_type])
+ self.assertEquals(resultset.total, 3)
+
+ q = MatchQuery("_all", "Baloney Testere pizza", operator="and")
+ resultset = self.conn.search(query=q, indices=self.index_name, doc_types=[self.document_type])
+ self.assertEquals(resultset.total, 0)
+
+ q = MatchQuery("_all", "Baloney Testere pizza", operator="or", minimum_should_match="70%")
+ resultset = self.conn.search(query=q, indices=self.index_name, doc_types=[self.document_type])
+ self.assertEquals(resultset.total, 1)
+
+ q = MatchQuery("parsedtext", "Bill guy", type="phrase", slop=2)
+ resultset = self.conn.search(query=q, indices=self.index_name, doc_types=[self.document_type])
+ self.assertEquals(resultset.total, 1)
+
+ q = MatchQuery("parsedtext", "guy Bill", type="phrase", slop=2)
+ resultset = self.conn.search(query=q, indices=self.index_name, doc_types=[self.document_type])
+ self.assertEquals(resultset.total, 0)
+
+ q = MatchQuery("name", "Tester")
+ resultset = self.conn.search(query=q, indices=self.index_name, doc_types=[self.document_type])
+ self.assertEquals(resultset.total, 1)
+
if __name__ == "__main__":
unittest.main()

0 comments on commit 89714b1

Please sign in to comment.