Every search engine has to decide what to do with pages that are the same page, or nearly. Mine had two separate problems with them in July. Paginated variants of one page showed up as separate results, and pages that were copies of each other with small edits weren't recognised as copies at all. The fixes used two different tools: the page's own rel="canonical" for the first, and SimHash with a small amount of arithmetic for the second.
Problem one: ten results for one page
The crawler already recorded each page's <link rel="canonical"> when indexing it. The search code just never used it. So a site's section pages, ?page=3, ?page=9 and so on, all declaring the same canonical URL, came back as separate, independent search results.
The fix used the search index's collapse feature: group the results by canonical_url and keep the best-scoring one from each group. There was a catch that turned out not to be one. The field was mapped with index: false, meaning it wasn't searchable. Collapse doesn't need that. Elasticsearch's documentation, which OpenSearch forked from, says the collapse field must be a single-valued keyword or numeric field with doc values enabled, and doc values are a separate structure from the searchable index. So collapse worked on the existing field with no reindex. The whole change was one key in the search request:
{
"query": { "bool": { "must": { "match": { "body": "kitchen" } } } },
"collapse": { "field": "canonical_url" }
}Two things were checked before trusting it. About 17% of the corpus had no canonical URL at all, and those pages must not end up merged into one big group; a collapsed query over everything still returned distinct, unrelated URLs. And the documentation's warning applies: the total hit count still counts every matching document, not the number of groups.
The issue that triggered the fix also included a case that looked like duplication and wasn't: a chain's store-locator pages, one per town, built from the same template. Each declared its own canonical URL and had genuinely different content. They were different pages. Showing fewer results per site would have been a separate ranking decision, and it was deliberately not slipped into a duplicate fix.
Problem two: copies that weren't identical
Near-duplicate detection already existed, based on SimHash: a 63-bit fingerprint of a page's text, where similar texts get fingerprints that differ in only a few bits. But the first version only matched identical fingerprints. Syndicated content with small edits got slightly different fingerprints and slipped through, along with any page that differed by a reformatted paragraph.
Finding fingerprints that are close but not equal sounds expensive: you can't look up "within 3 bits of this value" in an ordinary index. The design note mentions the classic answer, a scheme of multiple permuted tables built for billions of pages, and rejects it as more than this corpus needed. What it used instead is banding.
The fingerprint is split into four non-overlapping groups of bits, 15, 16, 16 and 16, each stored as its own generated, indexed column. Then a pigeonhole argument does the work. The threshold for "near duplicate" was 3 differing bits. Three differing bits spread across four separate groups can touch at most three of them, so at least one group must be identical. That means every near-duplicate shares at least one band exactly, and four ordinary indexed equality lookups, one per band, find every candidate. The band columns and the candidate query, as they work in both MySQL and PostgreSQL:
-- Four bands of the 63-bit fingerprint (15 + 16 + 16 + 16 bits), stored and indexed.
ALTER TABLE url
ADD simhash_band1 bigint GENERATED ALWAYS AS (simhash & 32767) STORED,
ADD simhash_band2 bigint GENERATED ALWAYS AS ((simhash >> 15) & 65535) STORED,
ADD simhash_band3 bigint GENERATED ALWAYS AS ((simhash >> 31) & 65535) STORED,
ADD simhash_band4 bigint GENERATED ALWAYS AS ((simhash >> 47) & 65535) STORED;
CREATE INDEX idx_url_simhash_band1 ON url (simhash_band1);
CREATE INDEX idx_url_simhash_band2 ON url (simhash_band2);
CREATE INDEX idx_url_simhash_band3 ON url (simhash_band3);
CREATE INDEX idx_url_simhash_band4 ON url (simhash_band4);
-- Candidates for a new page: anything sharing at least one band exactly.
-- $1..$4 are the new page's bands, computed the same way.
SELECT id, simhash FROM url
WHERE simhash_band1 = $1 OR simhash_band2 = $2
OR simhash_band3 = $3 OR simhash_band4 = $4;Sharing a band is necessary but not sufficient: the other three bands can still differ by more than the budget. So every candidate is checked with a real Hamming distance on the full fingerprint before it counts. In PHP, where a 63-bit fingerprint fits in a normal integer:
// The same four bands as the generated columns.
function bands(int $simhash): array
{
return [
$simhash & 0x7FFF,
($simhash >> 15) & 0xFFFF,
($simhash >> 31) & 0xFFFF,
($simhash >> 47) & 0xFFFF,
];
}
// Number of differing bits. A shared band only makes a candidate:
// confirm with the full distance before treating it as a near-duplicate.
function hammingDistance(int $a, int $b): int
{
$xor = $a ^ $b;
$count = 0;
while ($xor !== 0) {
$xor &= $xor - 1; // clear the lowest set bit
++$count;
}
return $count;
}
function isNearDuplicate(int $a, int $b, int $threshold = 3): bool
{
$shared = array_intersect_assoc(bands($a), bands($b)) !== [];
return $shared && hammingDistance($a, $b) <= $threshold;
}
As a check of the arithmetic, 200,000 random fingerprints with three bits flipped were all found by isNearDuplicate(), and none with four bits flipped was accepted. A fuzzy match then went through exactly the same canonical decision as an exact one.
Choosing which copy is the canonical one
The same work changed which copy wins. Before, the first copy indexed won, permanently. After the change, an explicit rel="canonical" that checked out was trusted first, then quality and authority scores when they were fresh, and indexing order was only the final tiebreak.
The cost that wasn't theoretical
Adding the four band columns was not free. They were stored generated columns, so the database had to compute a value for every existing row. On a local copy with 18 million rows, the migration took about five minutes and stalled about 20 concurrent crawler and indexer writers behind a metadata lock for the whole time. In production, the workers were stopped first, and the migration ran against a quiet table.
What generalises
Use the canonical URL sites already give you before writing clever duplicate detection; it's free and usually right. When you do need "close enough" matching on fingerprints, banding plus a pigeonhole argument turns it into a few exact lookups and a verification step. And a schema change that has to touch every row is a maintenance window, even when the change itself is one line.
Leave a comment
Comments are reviewed before they appear.