本文整理匯總了Python中elasticsearch_dsl.query.Q屬性的典型用法代碼示例。如果您正苦於以下問題:Python query.Q屬性的具體用法?Python query.Q怎麽用?Python query.Q使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類elasticsearch_dsl.query
的用法示例。
在下文中一共展示了query.Q屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: filter_permissions
# 需要導入模塊: from elasticsearch_dsl import query [as 別名]
# 或者: from elasticsearch_dsl.query import Q [as 別名]
def filter_permissions(self, search):
"""Filter given query based on permissions of the user in the request.
:param search: ElasticSearch query object
"""
user = self.request.user
if user.is_superuser:
return search
if user.is_anonymous:
user = get_anonymous_user()
filters = [Q("match", users_with_permissions=user.pk)]
filters.extend(
[
Q("match", groups_with_permissions=group.pk)
for group in user.groups.all()
]
)
filters.append(Q("match", public_permission=True))
# `minimum_should_match` is set to 1 by default
return search.query("bool", should=filters)
示例2: bollinger_band
# 需要導入模塊: from elasticsearch_dsl import query [as 別名]
# 或者: from elasticsearch_dsl.query import Q [as 別名]
def bollinger_band(index='cf_etf_hist_price', start_date='2018-12-26', end_date='2019-03-25', symbol='rfem'):
ESLowLevelClientByConnection.get_instance()
search = Search(index=index, using='high_level_client')[0:0]
search.query = Q(Bool(must=[Range(date={'gte': '2018-12-26', 'lte': '2019-03-25'}), Term(symbol='rfem')]))
aggs = A(DateHistogram(field='date', interval='1d', format='yyyy-MM-dd', min_doc_count=1))
aggs_tp = A(ScriptedMetric(init_script='state.totals=[]',
map_script='state.totals.add((doc.high.value+doc.low.value+doc.close.value)/3)',
combine_script='double total=0; for (t in state.totals) {total += t} return total',
reduce_script='double total=0; for (t in states) {total += t} return total'))
aggs_moving_avg = A(MovingAvg(model='simple', window=20, buckets_path='tp.value'))
aggs_bbu = A(BucketScript(buckets_path={'SMA':'20_trading_days_moving_avg'}, script='params.SMA + 0.5'))
aggs_bbl = A(BucketScript(buckets_path={'SMA': '20_trading_days_moving_avg'}, script='params.SMA - 0.5'))
search.aggs.bucket('Bollinger_band', aggs).metric('tp', aggs_tp).pipeline(
'20_trading_days_moving_avg', aggs_moving_avg).pipeline('BBU', aggs_bbu).pipeline('BBL', aggs_bbl)
response = search.execute()
print(response.to_dict())
示例3: test_inverted_query_with_must_and_must_not
# 需要導入模塊: from elasticsearch_dsl import query [as 別名]
# 或者: from elasticsearch_dsl.query import Q [as 別名]
def test_inverted_query_with_must_and_must_not():
q = query.Q('bool',
must=[query.Q('match', f=3), query.Q('match', f=4)],
must_not=[query.Q('match', f=1), query.Q('match', f=2)]
)
print((~q).to_dict())
assert ~q == query.Q('bool',
should=[
# negation of must
query.Q('bool', must_not=[query.Q('match', f=3)]),
query.Q('bool', must_not=[query.Q('match', f=4)]),
# negation of must_not
query.Q('match', f=1),
query.Q('match', f=2),
]
)
示例4: test_function_score_to_dict
# 需要導入模塊: from elasticsearch_dsl import query [as 別名]
# 或者: from elasticsearch_dsl.query import Q [as 別名]
def test_function_score_to_dict():
q = query.Q(
'function_score',
query=query.Q('match', title='python'),
functions=[
query.SF('random_score'),
query.SF('field_value_factor', field='comment_count', filter=query.Q('term', tags='python'))
]
)
d = {
'function_score': {
'query': {'match': {'title': 'python'}},
'functions': [
{'random_score': {}},
{
'filter': {'term': {'tags': 'python'}},
'field_value_factor': {
'field': 'comment_count',
}
}
],
}
}
assert d == q.to_dict()
示例5: apply
# 需要導入模塊: from elasticsearch_dsl import query [as 別名]
# 或者: from elasticsearch_dsl.query import Q [as 別名]
def apply(self, search, field, value):
"""Apply lookup expression to search query."""
if not isinstance(value, list):
value = [x for x in value.strip().split(",") if x]
filters = [Q("match", **{field: item}) for item in value]
return search.query("bool", should=filters)
示例6: default_value_when_missing_filter
# 需要導入模塊: from elasticsearch_dsl import query [as 別名]
# 或者: from elasticsearch_dsl.query import Q [as 別名]
def default_value_when_missing_filter(field, missing_val):
"""Create a custom exists filter.
:param field: Field name.
:missing_val
:returns: Function that returns the Terms query.
"""
def inner(values):
if missing_val in values:
return Bool(**{'must_not': {'exists': {'field': field}}})
else:
return Q('terms', **{field: values})
return inner
示例7: overdue_loans_filter
# 需要導入模塊: from elasticsearch_dsl import query [as 別名]
# 或者: from elasticsearch_dsl.query import Q [as 別名]
def overdue_loans_filter(field):
"""Create a custom filter for ongoing loans.
:param field: Field to filter.
:param range_query: Dictionary with available keys and their range options.
"""
def inner(values):
range_query = {
"Overdue": {"lt": str(arrow.utcnow().date())},
"Upcoming return": {
"lte": str(
current_app.config["CIRCULATION_POLICIES"][
"upcoming_return_range"]().date()),
"gte": str(arrow.utcnow().date())}
}
args = {}
for range_key, mappings in range_query.items():
if range_key in values:
for key, value in mappings.items():
args[key] = value
return Range(**{field: args}) & Q('terms', **{
'state': current_app.config["CIRCULATION_STATES_LOAN_ACTIVE"]})
return inner
示例8: test_match_phrase_query_via_connection
# 需要導入模塊: from elasticsearch_dsl import query [as 別名]
# 或者: from elasticsearch_dsl.query import Q [as 別名]
def test_match_phrase_query_via_connection(self):
ESLowLevelClientByConnection.get_instance()
search = Search(index='cf_etf', using='high_level_client')
# call the Q method
search.query = Q('match_phrase', fund_name='iShares MSCI ACWI ETF')
response = search.execute()
self.assertEqual(response['hits']['total']['value'], 1)
示例9: test_filter_can_be_instantiated_using_positional_args
# 需要導入模塊: from elasticsearch_dsl import query [as 別名]
# 或者: from elasticsearch_dsl.query import Q [as 別名]
def test_filter_can_be_instantiated_using_positional_args():
a = aggs.Filter(query.Q('term', f=42))
assert {
'filter': {
'term': {'f': 42}
}
} == a.to_dict()
assert a == aggs.A('filter', query.Q('term', f=42))
示例10: test_filter_aggregation_as_nested_agg
# 需要導入模塊: from elasticsearch_dsl import query [as 別名]
# 或者: from elasticsearch_dsl.query import Q [as 別名]
def test_filter_aggregation_as_nested_agg():
a = aggs.Terms(field='tags')
a.bucket('filtered', 'filter', query.Q('term', f=42))
assert {
'terms': {'field': 'tags'},
'aggs': {
'filtered': {
'filter': {
'term': {'f': 42}
},
}
}
} == a.to_dict()
示例11: test_filter_aggregation_with_nested_aggs
# 需要導入模塊: from elasticsearch_dsl import query [as 別名]
# 或者: from elasticsearch_dsl.query import Q [as 別名]
def test_filter_aggregation_with_nested_aggs():
a = aggs.Filter(query.Q('term', f=42))
a.bucket('testing', 'terms', field='tags')
assert {
'filter': {
'term': {'f': 42}
},
'aggs': {
'testing': {'terms': {'field': 'tags'}}
}
} == a.to_dict()
示例12: test_filters_correctly_identifies_the_hash
# 需要導入模塊: from elasticsearch_dsl import query [as 別名]
# 或者: from elasticsearch_dsl.query import Q [as 別名]
def test_filters_correctly_identifies_the_hash():
a = aggs.A('filters', filters={'group_a': {'term': {'group': 'a'}}, 'group_b': {'term': {'group': 'b'}}})
assert {
'filters': {
'filters': {
'group_a': {'term': {'group': 'a'}},
'group_b': {'term': {'group': 'b'}}
}
}
} == a.to_dict()
assert a.filters.group_a == query.Q('term', group='a')
示例13: test_empty_Q_is_match_all
# 需要導入模塊: from elasticsearch_dsl import query [as 別名]
# 或者: from elasticsearch_dsl.query import Q [as 別名]
def test_empty_Q_is_match_all():
q = query.Q()
assert isinstance(q, query.MatchAll)
assert query.MatchAll() == q
示例14: test_bool_and_other_sets_min_should_match_if_needed
# 需要導入模塊: from elasticsearch_dsl import query [as 別名]
# 或者: from elasticsearch_dsl.query import Q [as 別名]
def test_bool_and_other_sets_min_should_match_if_needed():
q1 = query.Q('term', category=1)
q2 = query.Q('bool', should=[
query.Q('term', name='aaa'),
query.Q('term', name='bbb')]
)
q = q1 & q2
assert q == query.Bool(
must=[q1],
should=[query.Q('term', name='aaa'), query.Q('term', name='bbb')],
minimum_should_match=1
)
示例15: test_bool_with_different_minimum_should_match_should_not_be_combined
# 需要導入模塊: from elasticsearch_dsl import query [as 別名]
# 或者: from elasticsearch_dsl.query import Q [as 別名]
def test_bool_with_different_minimum_should_match_should_not_be_combined():
q1 = query.Q('bool', minimum_should_match=2, should=[query.Q('term', field='aa1'), query.Q('term', field='aa2'), query.Q('term', field='aa3'), query.Q('term', field='aa4')])
q2 = query.Q('bool', minimum_should_match=3, should=[query.Q('term', field='bb1'), query.Q('term', field='bb2'), query.Q('term', field='bb3'), query.Q('term', field='bb4')])
q3 = query.Q('bool', minimum_should_match=4, should=[query.Q('term', field='cc1'), query.Q('term', field='cc2'), query.Q('term', field='cc3'), query.Q('term', field='cc4')])
q4 = q1 | q2
assert q4 == query.Bool(
should=[q1, q2]
)
q5 = q1 | q2 | q3
assert q5 == query.Bool(
should=[q1, q2, q3]
)