From 3be2c770086b17dcc030a39892e0038393bce509 Mon Sep 17 00:00:00 2001 From: yizhi <946185759@qq.com> Date: Fri, 15 Nov 2024 10:35:20 +0800 Subject: [PATCH] =?UTF-8?q?tsvector=E5=85=B3=E9=94=AE=E5=AD=97=E6=8A=BD?= =?UTF-8?q?=E5=8F=96=E6=97=B6=EF=BC=8C=E4=BC=9A=E4=BB=8E=E7=BB=99=E5=AE=9A?= =?UTF-8?q?=E7=9A=84=E5=85=B3=E9=94=AE=E5=AD=97=E4=B8=AD=E6=8F=90=E5=8F=96?= =?UTF-8?q?=E4=B8=AD=E8=8B=B1=E6=96=87=E5=8F=8A=E6=95=B0=E5=AD=97=EF=BC=8C?= =?UTF-8?q?=E5=B1=8F=E8=94=BD=E5=85=B6=E4=BB=96=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/type/tsvector.ts | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index c516c87..b813e4d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@yizhi/postgres", - "version": "1.0.7", + "version": "1.0.8", "main": "dist/index.js", "types": "typing/index.d.ts", "scripts": {}, diff --git a/src/type/tsvector.ts b/src/type/tsvector.ts index 04dc112..7e0e7f2 100644 --- a/src/type/tsvector.ts +++ b/src/type/tsvector.ts @@ -84,7 +84,7 @@ export class TSVector { } //处理关键词 - keywords = keywords.map(s => s.trim()).filter(s => !!s); + keywords = this.#resolveKeywords(keywords);// keywords.map(s => s.trim()).filter(s => !!s); //屏蔽纯字母数字的关键字 keywords = keywords.filter(kw => { @@ -121,6 +121,24 @@ export class TSVector { //完成 return new this(items.map(s => s.replaceAll("'", ""))); } + + //处理关键字 + static #resolveKeywords(keywords: string[]): string[] { + let result: string[] = []; + for (const kw of keywords) { + for (const s of kw.matchAll(/([a-zA-Z0-9\u4e00-\u9fa5]+)/g)) { + if (result.includes(s[1])) continue; + result.push(s[1]); + } + } + //排序 + result = result.sort((a, b) => b.length - a.length); + + //长度处理: 如果存在长度大于1的,则只保留长度大于1的关键字 + if (result.length > 1) result = result.filter(s => s.length > 1); + + return result; + } }