Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/searching.rst
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ field action Elasticsearch query type
====================== ================================
(no action specified) Term query
term Term query
terms Terms query
terms Terms query [5]_
in Terms query
text Text query (`DEPRECATED`)
match Match query [1]_
Expand Down Expand Up @@ -336,6 +336,10 @@ query_string Querystring query [3]_
the range at once. The range is inclusive on both sides and accepts
a tuple with the lower value first and upper value second.

.. [5] The ``terms`` field action support also the ``minimum_should_match``
parameter. If you pass a two elements list and the first element
is a list, the second element is treated as minimum_should_match.


.. seealso::

Expand Down
22 changes: 20 additions & 2 deletions elasticutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
None: 'term', # Default to term
'in': 'in',
'term': 'term',
'terms': 'terms',
'prefix': 'prefix',
'match': 'match',
'match_phrase': 'match_phrase',
Expand Down Expand Up @@ -1343,7 +1342,26 @@ def _process_query(self, query):
return {
'range': {field_name: _boosted_value(
field_action, field_action, key, val, boost)}
}
}

elif field_action == 'terms':
minimum_should_match = 1

if len(val) == 2 and isinstance(val[0], (tuple, list)):
minimum_should_match = val[1]
val = val[0]

value = {
'terms': _boosted_value(
field_name, field_action, key, val, boost
)
}

if minimum_should_match > 1:
value['terms']['minimum_should_match'] = minimum_should_match

return value


elif field_action == 'range':
lower, upper = val
Expand Down
11 changes: 11 additions & 0 deletions elasticutils/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,34 +190,39 @@ class QueryTest(ESTestCase):
'id': 1,
'foo': 'bar',
'tag': 'awesome',
'attributes': ['awesome', 'boring'],
'width': '2',
'height': 7
},
{
'id': 2,
'foo': 'bart',
'tag': 'boring',
'attributes': ['awesome', 'car'],
'width': '7',
'height': 11
},
{
'id': 3,
'foo': 'car',
'tag': 'awesome',
'attributes': ['awesome', 'duck'],
'width': '5',
'height': 5
},
{
'id': 4,
'foo': 'duck',
'tag': 'boat',
'attributes': ['train', 'boat'],
'width': '11',
'height': 7
},
{
'id': 5,
'foo': 'train car',
'tag': 'awesome',
'attributes': ['car', 'boring'],
'width': '7',
'height': 2
}
Expand Down Expand Up @@ -245,6 +250,12 @@ def test_q_terms(self):

eq_(len(self.get_s().query(Q(foo__terms=['car', 'duck']))), 3)

# minumum should match support

eq_(len(self.get_s().query(attributes__terms=(['awesome', 'boring'], 2))), 1)

eq_(len(self.get_s().query(Q(attributes__terms=(['awesome', 'boring'], 2)))), 1)

def test_q_in(self):
eq_(len(self.get_s().query(foo__in=['car', 'bar'])), 3)

Expand Down