Skip to content

Commit

Permalink
Allow F and Q shortcuts to accept proxied query/filter objects
Browse files Browse the repository at this point in the history
  • Loading branch information
honzakral committed Mar 7, 2015
1 parent eecceb8 commit 6994764
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions elasticsearch_dsl/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def F(name_or_filter, filters=None, **params):
raise ValueError('F() cannot accept parameters when passing in a Filter object.')
return name_or_filter

# s.filter = ~F(s.filter)
if hasattr(name_or_filter, '_proxied'):
return name_or_filter._proxied

# 'term', tag='python', ...
return Filter.get_dsl_class(name_or_filter)(**params)

Expand Down
4 changes: 4 additions & 0 deletions elasticsearch_dsl/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def Q(name_or_query, **params):
raise ValueError('Q() cannot accept parameters when passing in a Query object.')
return name_or_query

# s.query = Q('filtered', query=s.query)
if hasattr(name_or_query, '_proxied'):
return name_or_query._proxied

# "match", title="python"
return Query.get_dsl_class(name_or_query)(**params)

Expand Down
27 changes: 27 additions & 0 deletions test_elasticsearch_dsl/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,33 @@ def test_query_can_be_assigned_to():

assert s.query._proxied is q

def test_query_can_be_wrapped():
s = search.Search().query('match', title='python')

s.query = Q('function_score', query=s.query, field_value_factor={'field': 'rating'})

assert {
'query': {
'function_score': {
'functions': [{'field_value_factor': {'field': 'rating'}}],
'query': {'match': {'title': 'python'}}
}
}
}== s.to_dict()

def test_filter_can_be_overriden():
s = search.Search().filter('term', tag='python')
s.filter = ~F(s.filter)

assert {
"query": {
"filtered": {
"query": {"match_all": {}},
"filter": {"bool": {"must_not": [{"term": {"tag": "python"}}]}}
}
}
} == s.to_dict()

def test_using():
o = object()
o2 = object()
Expand Down

0 comments on commit 6994764

Please sign in to comment.