From a24fbf81d7f04d48ede065a45aba78d1f26b802d Mon Sep 17 00:00:00 2001 From: zeppi Date: Thu, 24 Mar 2022 18:07:22 -0400 Subject: [PATCH] es should fix --- lbry/wallet/server/db/elasticsearch/search.py | 44 ++++++++++++++----- .../integration/claims/test_claim_commands.py | 17 +++++++ 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/lbry/wallet/server/db/elasticsearch/search.py b/lbry/wallet/server/db/elasticsearch/search.py index 3111155a9..5ea73b765 100644 --- a/lbry/wallet/server/db/elasticsearch/search.py +++ b/lbry/wallet/server/db/elasticsearch/search.py @@ -620,18 +620,39 @@ 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']}} + ] + } + } + ]} + } + ) if kwargs.get('text'): query['must'].append( {"simple_query_string": @@ -669,6 +690,7 @@ def expand_query(**kwargs): "sort": query["sort"] } } + print('query', query) return query diff --git a/tests/integration/claims/test_claim_commands.py b/tests/integration/claims/test_claim_commands.py index 576886376..7cfb48e4c 100644 --- a/tests/integration/claims/test_claim_commands.py +++ b/tests/integration/claims/test_claim_commands.py @@ -405,6 +405,23 @@ class ClaimSearchCommand(ClaimTestCase): not_channel_ids=[chan2_id], has_channel_signature=True, valid_channel_signature=True) await match([], not_channel_ids=[chan1_id, chan2_id], has_channel_signature=True, valid_channel_signature=True) + async def test_no_source_and_valid_channel_signature_and_media_type(self): + await self.channel_create('@spam2', '1.0') + newstream = await self.stream_create('barrrrrr', '1.0', channel_name='@spam2', file_path=self.video_file_name) + print('news', newstream) + all_claims = await self.claim_search() ## why this is missing "newstream" above + no_source_claims = await self.claim_search(has_no_source=True, valid_channel_signature=True, + media_type="video/mp4") + mp4_claims = await self.claim_search(media_type="video/mp4") + no_source_claims_no_media = await self.claim_search(has_no_source=True, valid_channel_signature=True) + print('ALL', all_claims) + print('NOSRC', no_source_claims) + print('MP4', mp4_claims) + print('NSRC NO MEDIA', no_source_claims_no_media) + # self.assertEqual(1, len(no_source_claims_no_media)) + self.assertEqual(1, len(no_source_claims)) + self.assertEqual(1, len(mp4_claims)) + async def test_limit_claims_per_channel(self): match = self.assertFindsClaims chan1_id = self.get_claim_id(await self.channel_create('@chan1'))