Skip to content

Commit 196f880

Browse files
committed
#21 - Improve search output
1 parent e7f5f8d commit 196f880

File tree

2 files changed

+28
-79
lines changed

2 files changed

+28
-79
lines changed

Python/Tasks/Searching/Search5kFutures.py

+12-35
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
import os
77
import sys
88
import time
9-
from collections import Counter
9+
1010
from senzing import (
1111
G2BadInputException,
1212
G2Engine,
13+
G2EngineFlags,
1314
G2Exception,
1415
G2RetryableException,
1516
G2UnrecoverableException,
@@ -26,7 +27,11 @@ def mock_logger(level, exception, error_rec=None):
2627

2728
def search_record(engine, rec_to_search):
2829
search_response = bytearray()
29-
engine.searchByAttributes(rec_to_search, search_response)
30+
engine.searchByAttributes(
31+
rec_to_search,
32+
search_response,
33+
G2EngineFlags.G2_SEARCH_BY_ATTRIBUTES_MINIMAL_ALL,
34+
)
3035
return search_response
3136

3237

@@ -52,43 +57,15 @@ def record_stats(success, error, prev_time):
5257

5358

5459
def search_results(result, record, out_file):
55-
response_dict = json.loads(result.decode())
60+
response_str = result.decode()
61+
response_dict = json.loads(response_str)
5662
response_entities = response_dict.get("RESOLVED_ENTITIES", None)
5763

64+
out_file.write("-" * 100 + "\n")
5865
if response_entities:
59-
results_str = []
60-
results_count = Counter(
61-
k
62-
for entity in response_entities
63-
for k in entity.keys()
64-
if k.startswith("MATCH_INFO")
65-
)
66-
results_str.append(f'\n{results_count["MATCH_INFO"]} results for {record}')
67-
68-
for idx, entity in enumerate(response_entities, start=1):
69-
results_str.append(f"\n Result {idx}")
70-
results_str.append(
71-
"\n Entity ID: "
72-
f" {entity['ENTITY']['RESOLVED_ENTITY']['ENTITY_ID']}"
73-
)
74-
results_str.append(
75-
"\n Entity name: "
76-
f" {entity['ENTITY']['RESOLVED_ENTITY']['ENTITY_NAME']}"
77-
)
78-
results_str.append(
79-
f'\n Match key: {entity["MATCH_INFO"]["MATCH_KEY"]}'
80-
)
81-
results_str.append("\n Records summary: ")
82-
for record_summary in entity["ENTITY"]["RESOLVED_ENTITY"]["RECORD_SUMMARY"]:
83-
results_str.append(
84-
f'{record_summary["DATA_SOURCE"]}: {record_summary["RECORD_COUNT"]}'
85-
+ " "
86-
)
87-
results_str.append("\n")
88-
89-
out_file.write("".join(results_str))
66+
out_file.write(f"Result for {record.rstrip()}:\n\n{response_str}\n\n")
9067
else:
91-
out_file.write(f"\nNo result for {record}\n")
68+
out_file.write(f"No result for {record}\n\n")
9269

9370

9471
def futures_search(engine, input_file, output_file):

Python/Tasks/Searching/SearchRecords.py

+16-44
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import json
44
import os
55
import sys
6-
from collections import Counter
6+
77
from senzing import (
88
G2BadInputException,
99
G2Engine,
10+
G2EngineFlags,
1011
G2Exception,
1112
G2RetryableException,
1213
G2UnrecoverableException,
@@ -42,59 +43,30 @@ def mock_logger(level, exception, error_rec=None):
4243
def searcher(engine):
4344
for rec_to_search in search_records:
4445
try:
46+
rec_str = json.dumps(rec_to_search)
4547
search_response = bytearray()
46-
engine.searchByAttributes(json.dumps(rec_to_search), search_response)
48+
engine.searchByAttributes(
49+
rec_str,
50+
search_response,
51+
G2EngineFlags.G2_SEARCH_BY_ATTRIBUTES_MINIMAL_ALL,
52+
)
4753
except (G2BadInputException, json.JSONDecodeError) as err:
48-
mock_logger("ERROR", err, rec_to_search)
54+
mock_logger("ERROR", err, rec_str)
4955
except G2RetryableException as err:
50-
mock_logger("WARN", err, rec_to_search)
56+
mock_logger("WARN", err, rec_str)
5157
except (G2UnrecoverableException, G2Exception) as err:
52-
mock_logger("CRITICAL", err, rec_to_search)
58+
mock_logger("CRITICAL", err, rec_str)
5359
raise
5460
else:
55-
response_dict = json.loads(search_response.decode())
61+
response_str = search_response.decode()
62+
response_dict = json.loads(response_str)
5663
response_entities = response_dict.get("RESOLVED_ENTITIES", None)
5764

65+
print("-" * 100)
5866
if response_entities:
59-
results_str = []
60-
results_count = Counter(
61-
k
62-
for entity in response_entities
63-
for k in entity.keys()
64-
if k.startswith("MATCH_INFO")
65-
)
66-
results_str.append(
67-
f'\n{results_count["MATCH_INFO"]} results for'
68-
f" {json.dumps(rec_to_search)}\n"
69-
)
70-
71-
for idx, result in enumerate(response_entities, start=1):
72-
results_str.append(f"\n Result {idx}")
73-
results_str.append(
74-
"\n Entity ID: "
75-
f" {result['ENTITY']['RESOLVED_ENTITY']['ENTITY_ID']}"
76-
)
77-
results_str.append(
78-
"\n Entity name: "
79-
f" {result['ENTITY']['RESOLVED_ENTITY']['ENTITY_NAME']}"
80-
)
81-
results_str.append(
82-
f'\n Match key: {result["MATCH_INFO"]["MATCH_KEY"]}'
83-
)
84-
results_str.append("\n Records summary: ")
85-
for record_summary in result["ENTITY"]["RESOLVED_ENTITY"][
86-
"RECORD_SUMMARY"
87-
]:
88-
results_str.append(
89-
f'{record_summary["DATA_SOURCE"]}:'
90-
f' {record_summary["RECORD_COUNT"]}'
91-
+ " "
92-
)
93-
results_str.append("\n")
94-
95-
print("".join(results_str))
67+
print(f"Result for {rec_str}:\n\n{response_str}\n")
9668
else:
97-
print(f"\nNo result for {json.dumps(rec_to_search)}\n")
69+
print(f"No result for {rec_str}\n")
9870

9971

10072
try:

0 commit comments

Comments
 (0)