From 879869cb317ec8dcb4c847a5861af5ea9780a8e7 Mon Sep 17 00:00:00 2001 From: zeppi Date: Thu, 24 Mar 2022 19:33:32 -0400 Subject: [PATCH 1/2] fix shoulds in es --- scribe/elasticsearch/search.py | 47 ++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/scribe/elasticsearch/search.py b/scribe/elasticsearch/search.py index 6ba31ef..140a56e 100644 --- a/scribe/elasticsearch/search.py +++ b/scribe/elasticsearch/search.py @@ -396,18 +396,43 @@ def expand_query(**kwargs): if 'signature_valid' in kwargs: query['must'].append({"term": {"is_signature_valid": bool(kwargs["signature_valid"])}}) elif 'signature_valid' in kwargs: - query.setdefault('should', []) - query["minimum_should_match"] = 1 - query['should'].append({"bool": {"must_not": {"exists": {"field": "signature"}}}}) - query['should'].append({"term": {"is_signature_valid": bool(kwargs["signature_valid"])}}) + query['must'].append( + {"bool": + {"should": [ + {"bool": {"must_not": {"exists": {"field": "signature"}}}}, + {"bool" : {"must" : {"term": {"is_signature_valid": bool(kwargs["signature_valid"])}}}} + ]} + } + ) if 'has_source' in kwargs: - query.setdefault('should', []) - query["minimum_should_match"] = 1 - is_stream_or_repost = {"terms": {"claim_type": [CLAIM_TYPES['stream'], CLAIM_TYPES['repost']]}} - query['should'].append( - {"bool": {"must": [{"match": {"has_source": kwargs['has_source']}}, is_stream_or_repost]}}) - query['should'].append({"bool": {"must_not": [is_stream_or_repost]}}) - query['should'].append({"bool": {"must": [{"term": {"reposted_claim_type": CLAIM_TYPES['channel']}}]}}) + is_stream_or_repost_terms = {"terms": {"claim_type": [CLAIM_TYPES['stream'], CLAIM_TYPES['repost']]}} + query['must'].append( + {"bool": + {"should": [ + {"bool": # when is_stream_or_repost AND has_source + {"must": [ + {"match": {"has_source": kwargs['has_source']}}, + is_stream_or_repost_terms, + ] + }, + }, + {"bool": # when not is_stream_or_repost + {"must_not": is_stream_or_repost_terms} + }, + {"bool": # when reposted_claim_type wouldn't have source + {"must_not": + [ + {"term": {"reposted_claim_type": CLAIM_TYPES['stream']}} + ], + "must": + [ + {"term": {"claim_type": CLAIM_TYPES['repost']}} + ] + } + } + ]} + } + ) if kwargs.get('text'): query['must'].append( {"simple_query_string": From 444d1f51cce214caf1eb29703acd1d0bd28a59c7 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Thu, 24 Mar 2022 21:13:08 -0400 Subject: [PATCH 2/2] fix https://github.com/lbryio/lbry-sdk/issues/3561 --- scribe/elasticsearch/search.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/scribe/elasticsearch/search.py b/scribe/elasticsearch/search.py index 140a56e..7b77801 100644 --- a/scribe/elasticsearch/search.py +++ b/scribe/elasticsearch/search.py @@ -352,13 +352,37 @@ def expand_query(**kwargs): query['must'].append({"range": {key: {ops[operator]: value}}}) elif key in RANGE_FIELDS and isinstance(value, list) and all(v[0] in ops for v in value): range_constraints = [] + release_times = [] for v in value: operator_length = 2 if v[:2] in ops else 1 operator, stripped_op_v = v[:operator_length], v[operator_length:] if key == 'fee_amount': stripped_op_v = str(Decimal(stripped_op_v)*1000) - range_constraints.append((operator, stripped_op_v)) - query['must'].append({"range": {key: {ops[operator]: v for operator, v in range_constraints}}}) + if key == 'release_time': + release_times.append((operator, stripped_op_v)) + else: + range_constraints.append((operator, stripped_op_v)) + if key != 'release_time': + query['must'].append({"range": {key: {ops[operator]: v for operator, v in range_constraints}}}) + else: + query['must'].append( + {"bool": + {"should": [ + {"bool": { + "must_not": { + "exists": { + "field": "release_time" + } + } + }}, + {"bool": { + "must": [ + {"exists": {"field": "release_time"}}, + {'range': {key: {ops[operator]: v for operator, v in release_times}}}, + ]}}, + ]} + } + ) elif many: query['must'].append({"terms": {key: value}}) else: