Sub topic 9
Clustering Duplicates and Picking a Preferred URL
This chapter assumes you can already normalize URLs, hash body text, and compute shingle similarity, and that you can read canonical, redirect, noindex and robots signals from a crawl export. Here you combine those results into clusters and choose one preferred URL for each.
From pairs to clusters
Exact hashes and similarity scores give you pairs of related pages. A fix applies to a group, so you need clusters. If page A matches B and B matches C, all three belong together even if A and C never matched directly.
A union-find structure (also called disjoint sets) merges pairs into groups. Each URL starts alone. Every matching pair joins two groups.
def build_clusters(urls, pairs):
parent = {u: u for u in urls}
def find(u):
while parent[u] != u:
parent[u] = parent[parent[u]]
u = parent[u]
return u
for a, b in pairs:
root_a, root_b = find(a), find(b)
if root_a != root_b:
parent[root_b] = root_a
clusters = {}
for u in urls:
clusters.setdefault(find(u), []).append(u)
return [sorted(group) for group in clusters.values() if len(group) > 1]
urls = ["/a", "/b", "/c", "/d", "/e"]
pairs = [("/a", "/b"), ("/b", "/c"), ("/d", "/e")]
print(build_clusters(urls, pairs))
# [['/a', '/b', '/c'], ['/d', '/e']]Feed it the pairs from three sources: URLs that share a normalized form, URLs that share a body hash, and URL pairs whose Jaccard score passes your threshold.
Watch for chaining
Chaining is the main failure of this approach. With near duplicates, A can resemble B at 0.9, and B can resemble C at 0.9, while A and C resemble each other at only 0.5. Union-find still puts all three in one cluster. Two practical defenses:
- Use a high threshold, such as 0.8 or above, for the near-duplicate pairs.
- Review the lowest-similarity pair inside each large cluster before you trust the cluster.
Choosing the preferred URL
The preferred URL is the one you want to appear in search results. Score each member of the cluster on the evidence in your crawl:
| Signal | Prefer |
|---|---|
| Status code | 200 over redirects and errors |
| URL form | Normalized: https, no tracking parameters, consistent slash |
| Canonical target | The URL that the other pages already name as canonical |
| Indexability | Indexable, not noindex and not blocked by robots.txt |
| Internal links | More internal links pointing to it |
| Sitemap | Listed in the XML sitemap |
| Traffic or links | Higher organic clicks or external links, if you have them |
A simple rule is to sort by these signals in order and take the first URL. When the signals disagree, treat the cluster as a conflict to investigate, not as something to settle automatically.
import pandas as pd
def pick_preferred(cluster_df):
ranked = cluster_df.sort_values(
["status", "is_normalized", "canonical_votes", "inlinks"],
ascending=[True, False, False, False],
)
return ranked.iloc[0]["url"]
cluster = pd.DataFrame({
"url": ["https://example.com/shoes", "https://example.com/shoes?sort=price"],
"status": [200, 200],
"is_normalized": [True, False],
"canonical_votes": [5, 0],
"inlinks": [120, 14],
})
print(pick_preferred(cluster))
# https://example.com/shoesHere canonical_votes is the count of other cluster URLs whose canonical element points at this URL.
Flag conflicts
Compare your chosen URL with what the site already declares. Mark a cluster as a conflict when:
- Pages in the cluster canonicalize to different URLs.
- The canonical target redirects, returns an error, or is noindex.
- The sitemap lists a non-preferred URL.
- Internal links mostly point to a variant.
Conflicts matter because search engines treat canonical elements as hints. Mixed signals let the engine pick a different URL than you intended.
Exercise
A cluster has three URLs. /a and /b are 0.92 similar, and /b and /c are 0.88 similar, but /a and /c are 0.45 similar. What does this show, and what should you do before choosing a preferred URL?
Show answer
Chaining joined /a and /c through /b even though they differ a lot. Review the cluster manually, and either split it, for example by raising the threshold, or treat /c as a separate page. Only then pick a preferred URL for each resulting group.