Skip to content

Commit

Permalink
feat: update search index
Browse files Browse the repository at this point in the history
  • Loading branch information
snaildarter committed Dec 1, 2024
1 parent 7fe6666 commit 0081ffb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
3 changes: 3 additions & 0 deletions script/full_text_search.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ ALTER TABLE "EIPs" ADD COLUMN title_ts tsvector
GENERATED ALWAYS AS (to_tsvector('english', title)) STORED;
ALTER TABLE "EIPs" ADD COLUMN content_ts tsvector
GENERATED ALWAYS AS (to_tsvector('english', content)) STORED;
ALTER TABLE "EIPs" ADD COLUMN author_ts tsvector
GENERATED ALWAYS AS (to_tsvector('english', author)) STORED;
CREATE INDEX title_index ON "EIPs" USING GIN (title_ts);
CREATE INDEX content_index ON "EIPs" USING GIN (content_ts);
CREATE INDEX author_index ON "EIPs" USING GIN (author_ts);
38 changes: 34 additions & 4 deletions src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,56 @@ export class AppService {
try {
const result = {};
// eip match
if (this.isNumeric(content)) {
const numbers = content.match(/\d+/g);
const texts = content.match(/\D+/g);
const txt = texts.length ? texts.join(' ').trim() : '';
const eips = [];
let eipCondition = '';
console.log(numbers);
if (numbers && numbers.length) {
numbers.forEach((item) => {
if (Number(item) < 10000) {
// eip number don't overt 10000
eips.push(`eip LIKE '%${item}%'`);
}
});
if (eips.length === 1) {
eipCondition = eips[0];
} else {
eipCondition = eips.join(' OR ');
}
}

if (eips.length && !txt) {
const eipRecords = await this.connection.query(
`SELECT eip, title, type, category FROM "EIPs" WHERE eip='${content}'`,
`SELECT eip, title, type, category FROM "EIPs" WHERE ${eipCondition}`,
);
if (eipRecords && eipRecords.length > 0) {
result['eip_list'] = eipRecords;
}
} else {
// title match
const conditions = eips.length ? `${eipCondition} AND` : ``;
const titleRecords = await this.connection.query(
`SELECT eip, type, category, ts_headline('english',title, q), rank FROM (SELECT eip, type, category, title, q, ts_rank_cd(title_ts, q) AS rank FROM "EIPs", phraseto_tsquery('english','${content}') q WHERE title_ts @@ q ORDER BY rank DESC LIMIT 20) AS foo;`,
`SELECT eip, type, category, ts_headline('english',title, q), rank FROM (SELECT eip, type, category, title, q, ts_rank_cd(title_ts, q) AS rank FROM "EIPs", phraseto_tsquery('english','${txt}') q WHERE ${conditions} title_ts @@ q ORDER BY rank DESC LIMIT 20) AS foo;`,
);

if (titleRecords && titleRecords.length > 0) {
result['title_list'] = titleRecords;
}

// author match
const authorRecords = await this.connection.query(
`SELECT eip, type, category, ts_headline('english', author, q), rank FROM (SELECT eip, type, category, author, q, ts_rank_cd(author_ts, q) AS rank FROM "EIPs", phraseto_tsquery('english','${txt}') q WHERE ${conditions} author_ts @@ q ORDER BY rank DESC LIMIT 20) AS foo;`,
);

if (authorRecords && authorRecords.length > 0) {
result['author_list'] = authorRecords;
}

// content match
const contentRecords = await this.connection.query(
`SELECT eip, type, category, title, ts_headline('english',content, q), rank FROM (SELECT eip, type, category, title, content, q, ts_rank_cd(content_ts, q) AS rank FROM "EIPs", phraseto_tsquery('english','${content}') q WHERE content_ts @@ q ORDER BY rank DESC LIMIT 20) AS foo;`,
`SELECT eip, type, category, title, ts_headline('english',content, q), rank FROM (SELECT eip, type, category, title, content, q, ts_rank_cd(content_ts, q) AS rank FROM "EIPs", phraseto_tsquery('english','${txt}') q WHERE ${conditions} content_ts @@ q ORDER BY rank DESC LIMIT 20) AS foo;`,
);
if (contentRecords && contentRecords.length > 0) {
result['content_list'] = contentRecords;
Expand Down

0 comments on commit 0081ffb

Please sign in to comment.