From 5733f14124dbff573a3b23c3e0b480a15e8d40ed Mon Sep 17 00:00:00 2001 From: AlexBob Date: Mon, 23 Dec 2024 11:24:00 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(`QueryFragment.java`,=20`Query?= =?UTF-8?q?Helper.java`,=20`AbstractEntity.java`):=20Add=20full-text=20sea?= =?UTF-8?q?rch=20functionality=20with=20ranking=20support.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/plate/boot/commons/base/AbstractEntity.java | 5 +++++ .../plate/boot/commons/utils/query/QueryFragment.java | 11 +++++++++++ .../plate/boot/commons/utils/query/QueryHelper.java | 6 +----- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/boot/platform/src/main/java/com/plate/boot/commons/base/AbstractEntity.java b/boot/platform/src/main/java/com/plate/boot/commons/base/AbstractEntity.java index a1a7ff44..35616d93 100644 --- a/boot/platform/src/main/java/com/plate/boot/commons/base/AbstractEntity.java +++ b/boot/platform/src/main/java/com/plate/boot/commons/base/AbstractEntity.java @@ -80,6 +80,11 @@ public abstract class AbstractEntity implements BaseEntity { @InsertOnlyProperty protected LocalDateTime createdTime; + /** + * Data full text search entity sort + */ + protected Double rank; + /** * Support query for json column */ diff --git a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryFragment.java b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryFragment.java index 406d0eb0..0590b4ca 100644 --- a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryFragment.java +++ b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryFragment.java @@ -1,5 +1,6 @@ package com.plate.boot.commons.utils.query; +import com.google.common.base.CaseFormat; import com.plate.boot.commons.exception.QueryException; import lombok.Getter; @@ -182,6 +183,16 @@ public QueryFragment limit(int size, long offset) { return this; } + public QueryFragment ts(String column, Object value) { + String lowerCamelCol = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, column); + String queryTable = "ts_" + lowerCamelCol; + columns("TS_RANK_CD(" + lowerCamelCol + ", " + queryTable + ") AS rank"); + query(",TO_TSQUERY('chinese',:" + column + ") AS " + queryTable); + where(queryTable + " @@ " + lowerCamelCol); + put(column, value); + return this; + } + public String whereSql() { if (this.where.length() > 0) { return " WHERE " + this.where; diff --git a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryHelper.java b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryHelper.java index 1e87a4a8..32a4d5fc 100644 --- a/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryHelper.java +++ b/boot/platform/src/main/java/com/plate/boot/commons/utils/query/QueryHelper.java @@ -271,12 +271,8 @@ private static void processSecurityCodeKey(QueryFragment queryFragment, Map objectMap, String prefix) { if (objectMap.containsKey("search") && !ObjectUtils.isEmpty(objectMap.get("search"))) { - var textSearch = (String) objectMap.get("search"); var column = StringUtils.hasLength(prefix) ? prefix + ".text_search" : "text_search"; - queryFragment.columns("TS_RANK_CD(" + column + ", queryTextSearch) AS rank"); - queryFragment.query(",TO_TSQUERY('chinese',:textSearch) queryTextSearch"); - queryFragment.where(column + "@@TO_TSQUERY('chinese',:textSearch)"); - queryFragment.put("textSearch", textSearch); + queryFragment.ts(column, objectMap.get("search")); } }