vectors.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """Enumerate the search queries to run per broker, across ALL of a subject's identifiers.
  2. People-search sites index a person under every name, phone, email, and address they
  3. have. A subject with two names (maiden/married) and three past cities can have many
  4. distinct listings on one broker, each found via a different search. `search_vectors`
  5. expands the dossier into the concrete searches to run, filtered by what each broker
  6. supports (`broker.search.by`, default ["name"]).
  7. """
  8. from __future__ import annotations
  9. import dossier as dossier_mod
  10. # What a broker can be searched by; default if a record doesn't declare it.
  11. DEFAULT_BY = ["name"]
  12. def supported_by(broker: dict) -> list[str]:
  13. return list((broker.get("search") or {}).get("by") or DEFAULT_BY)
  14. def search_vectors(subject_dossier: dict, broker: dict) -> list[dict]:
  15. """List of {by, query} searches to run for this subject on this broker."""
  16. by = set(supported_by(broker))
  17. ident = subject_dossier.get("identity", {})
  18. vectors: list[dict] = []
  19. if "name" in by:
  20. names = dossier_mod.all_names(subject_dossier)
  21. locations = dossier_mod.all_locations(subject_dossier)
  22. if locations:
  23. for name in names:
  24. for loc in locations:
  25. vectors.append({"by": "name",
  26. "query": {"full_name": name, "city": loc.get("city"), "state": loc.get("state")}})
  27. else:
  28. for name in names:
  29. vectors.append({"by": "name", "query": {"full_name": name}})
  30. if "phone" in by:
  31. for phone in ident.get("phones") or []:
  32. vectors.append({"by": "phone", "query": {"phone": phone}})
  33. if "email" in by:
  34. for email in ident.get("emails") or []:
  35. vectors.append({"by": "email", "query": {"email": email}})
  36. if "address" in by:
  37. for a in dossier_mod.all_addresses(subject_dossier):
  38. if a.get("line1"):
  39. vectors.append({"by": "address",
  40. "query": {k: a.get(k) for k in ("line1", "city", "state", "postal")}})
  41. return vectors