Merge pull request #2345 from mempool/simon/node-alias-fulltext-search

Node alias fulltext search
This commit is contained in:
wiz 2022-08-21 22:59:50 +09:00 committed by GitHub
commit 766dcddd36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 6 deletions

View File

@ -4,7 +4,7 @@ import logger from '../logger';
import { Common } from './common'; import { Common } from './common';
class DatabaseMigration { class DatabaseMigration {
private static currentVersion = 38; private static currentVersion = 39;
private queryTimeout = 120000; private queryTimeout = 120000;
private statisticsAddedIndexed = false; private statisticsAddedIndexed = false;
private uniqueLogs: string[] = []; private uniqueLogs: string[] = [];
@ -337,6 +337,11 @@ class DatabaseMigration {
await this.$executeQuery('ALTER TABLE `lightning_stats` CHANGE `added` `added` timestamp NULL'); await this.$executeQuery('ALTER TABLE `lightning_stats` CHANGE `added` `added` timestamp NULL');
await this.$executeQuery('ALTER TABLE `node_stats` CHANGE `added` `added` timestamp NULL'); await this.$executeQuery('ALTER TABLE `node_stats` CHANGE `added` `added` timestamp NULL');
} }
if (databaseSchemaVersion < 39 && isBitcoin === true) {
await this.$executeQuery('ALTER TABLE `nodes` ADD alias_search TEXT NULL DEFAULT NULL AFTER `alias`');
await this.$executeQuery('ALTER TABLE nodes ADD FULLTEXT(alias_search)');
}
} }
/** /**

View File

@ -259,9 +259,10 @@ class NodesApi {
public async $searchNodeByPublicKeyOrAlias(search: string) { public async $searchNodeByPublicKeyOrAlias(search: string) {
try { try {
const searchStripped = search.replace('%', '') + '%'; const publicKeySearch = search.replace('%', '') + '%';
const query = `SELECT nodes.public_key, nodes.alias, node_stats.capacity FROM nodes LEFT JOIN node_stats ON node_stats.public_key = nodes.public_key WHERE nodes.public_key LIKE ? OR nodes.alias LIKE ? GROUP BY nodes.public_key ORDER BY node_stats.capacity DESC LIMIT 10`; const aliasSearch = search.replace(/[-_.]/g, ' ').replace(/[^a-zA-Z ]/g, '').split(' ').map((search) => '+' + search + '*').join(' ');
const [rows]: any = await DB.query(query, [searchStripped, searchStripped]); const query = `SELECT nodes.public_key, nodes.alias, node_stats.capacity FROM nodes LEFT JOIN node_stats ON node_stats.public_key = nodes.public_key WHERE nodes.public_key LIKE ? OR MATCH nodes.alias_search AGAINST (? IN BOOLEAN MODE) GROUP BY nodes.public_key ORDER BY node_stats.capacity DESC LIMIT 10`;
const [rows]: any = await DB.query(query, [publicKeySearch, aliasSearch]);
return rows; return rows;
} catch (e) { } catch (e) {
logger.err('$searchNodeByPublicKeyOrAlias error: ' + (e instanceof Error ? e.message : e)); logger.err('$searchNodeByPublicKeyOrAlias error: ' + (e instanceof Error ? e.message : e));
@ -501,21 +502,24 @@ class NodesApi {
first_seen, first_seen,
updated_at, updated_at,
alias, alias,
alias_search,
color, color,
sockets, sockets,
status status
) )
VALUES (?, NOW(), FROM_UNIXTIME(?), ?, ?, ?, 1) VALUES (?, NOW(), FROM_UNIXTIME(?), ?, ?, ?, ?, 1)
ON DUPLICATE KEY UPDATE updated_at = FROM_UNIXTIME(?), alias = ?, color = ?, sockets = ?, status = 1`; ON DUPLICATE KEY UPDATE updated_at = FROM_UNIXTIME(?), alias = ?, alias_search = ?, color = ?, sockets = ?, status = 1`;
await DB.query(query, [ await DB.query(query, [
node.pub_key, node.pub_key,
node.last_update, node.last_update,
node.alias, node.alias,
this.aliasToSearchText(node.alias),
node.color, node.color,
sockets, sockets,
node.last_update, node.last_update,
node.alias, node.alias,
this.aliasToSearchText(node.alias),
node.color, node.color,
sockets, sockets,
]); ]);
@ -549,6 +553,10 @@ class NodesApi {
logger.err('$setNodesInactive() error: ' + (e instanceof Error ? e.message : e)); logger.err('$setNodesInactive() error: ' + (e instanceof Error ? e.message : e));
} }
} }
private aliasToSearchText(str: string): string {
return str.replace(/[-_.]/g, ' ').replace(/[^a-zA-Z ]/g, '');
}
} }
export default new NodesApi(); export default new NodesApi();