- Clone it
git clone https://github.com/HaileabT/full-text-search-postgres-nodejs.git /your/desired/path
- Install dependencies
npm i
- Create postgres connection
const connectionObject = new PostgresConnection("postgres", "password@", 5432, "random_db", "localhost");
- Create a search manager
const searchManagerOptions: FullTextSearchManagerOptions = {
mainEntity: "human",
fieldsToBeSearched: [
{ entity: "skill", field: "title", relationField: "human_id", rank: "A" },
{ entity: "human", field: "name", relationField: "", rank: "A" },
],
};
const searchManager = FullTextSearchManager.init(connectionObject.conn, searchManagerOptions);
type FullTextSearchManagerOptions = {
mainEntity: string; // Entity to be searched
fieldsToBeSearched?: EntityField[]; // Fields to be searched picked from all entities
};
interface EntityField = {
entity: string; // Entity where the might-be-searched field exists
field: string; // Field name
relationField?: string; // The foreign key with which the main entity relates to this field's entity
rank?: "A" | "B" | "C" | "D"; // Rank of this field
}
- Search
const doSearch = async () => {
if (!searchManager) return;
const res = await searchManager.search("karate");
console.log(res);
};
doSearch();