Fuzzy Search for ENS Names
Performing SQL queries on the ENS Unigraph requires that you have the unigraph plugin activated in your ENSNode instance. Learn more
Fetch Domains with names similar to a query string, ranked by similarity. This example uses PostgreSQL’s pg_trgm extension, which provides the % operator for fuzzy matching and the similarity() function for ranking results. See Connect for setup.
It is the name of an ENSDb Writer Schema, which is a database schema within an ENSDb instance, used to store indexed ENS data from a given ENSDb Writer instance. We use ensindexer_0 as the ENSDb Writer Schema Name in examples on this page, but your ENSDb Writer instance may be configured to use a different schema name. Make sure to replace
ensindexer_0 with the actual schema name used by your ENSDb Writer instance
when querying the ENSDb instance directly. For example, ENSIndexer allows configuring its own ENSDb Writer Schema Name with the ENSINDEXER_SCHEMA_NAME environment variable.
SELECT type, canonical_name, canonical_node, owner_id, similarity(canonical_name, 'reverse') as name_similarity, idFROM "ensindexer_0".domainsWHERE __canonicalNamePrefix % 'reverse'AND canonical = trueORDER BY name_similarity DESCLIMIT 5;| # | type | canonical_name | canonical_node | owner_id | name_similarity | id |
|---|---|---|---|---|---|---|
| 1 | ENSv1Domain | reverse | 0xa097f6721ce401e757d1223a763fef49b8b5f90bb18567ddb86fd205dff71d34 | 0xffffffffff52d316b7bd028358089bc8066b8f80 | 1 | |
| 2 | ENSv2Domain | reverse | 0xa097f6721ce401e757d1223a763fef49b8b5f90bb18567ddb86fd205dff71d34 | 0xffffffffff52d316b7bd028358089bc8066b8f80 | 1 | |
| 3 | ENSv1Domain | addr.reverse | 0x91d1777781884d03a6757a803996e38de2a42967fb37eeaca72729271025a9e2 | 0x26e5e80e8f36607ef401443fb34eea363c86e8f7 | 0.61538464 | |
Output matches a point in time snapshot of ENSDb result from our "V2 Sepolia" Hosted ENSNode instance. Live output depends on the configuration of your ENSNode instance and also changes that may have happened in ENS since this point in time snapshot example response was captured.
ensDb query builder and ensIndexerSchema
schema definition in the Connect example if you haven't already.
import { and, asc, eq, sql } from "drizzle-orm";
const q = "vitalik";const limit = 5;
const domains = await ensDb .select({ type: ensIndexerSchema.domain.type, canonicalName: ensIndexerSchema.domain.canonicalName, canonicalNode: ensIndexerSchema.domain.canonicalNode, ownerId: ensIndexerSchema.domain.ownerId, nameSimilarity: sql<number>`similarity(${ensIndexerSchema.domain.canonicalName}, ${q})`.as( "name_similarity", ), id: ensIndexerSchema.domain.id, }) .from(ensIndexerSchema.domain) .where( and( sql`${ensIndexerSchema.domain.__canonicalNamePrefix} % ${q}`, eq(ensIndexerSchema.domain.canonical, true), ), ) .orderBy(sql`name_similarity DESC`) .limit(limit);
console.log(domains);| # | type | canonicalName | canonicalNode | ownerId | nameSimilarity | id |
|---|---|---|---|---|---|---|
| 1 | ENSv1Domain | reverse | 0xa097f6721ce401e757d1223a763fef49b8b5f90bb18567ddb86fd205dff71d34 | 0xffffffffff52d316b7bd028358089bc8066b8f80 | 1 | |
| 2 | ENSv2Domain | reverse | 0xa097f6721ce401e757d1223a763fef49b8b5f90bb18567ddb86fd205dff71d34 | 0xffffffffff52d316b7bd028358089bc8066b8f80 | 1 | |
| 3 | ENSv1Domain | addr.reverse | 0x91d1777781884d03a6757a803996e38de2a42967fb37eeaca72729271025a9e2 | 0x26e5e80e8f36607ef401443fb34eea363c86e8f7 | 0.61538464 | |
Output matches a point in time snapshot of ENSDb result from our "V2 Sepolia" Hosted ENSNode instance. Live output depends on the configuration of your ENSNode instance and also changes that may have happened in ENS since this point in time snapshot example response was captured.