Skip to content

Commit

Permalink
✨ feat(QueryFragment.java, QueryHelper.java, `AbstractEntity.java…
Browse files Browse the repository at this point in the history
…`): Add full-text search functionality with ranking support.
  • Loading branch information
vnobo committed Dec 23, 2024
1 parent f939263 commit 5733f14
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public abstract class AbstractEntity<T> implements BaseEntity<T> {
@InsertOnlyProperty
protected LocalDateTime createdTime;

/**
* Data full text search entity sort
*/
protected Double rank;

/**
* Support query for json column
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,8 @@ private static void processSecurityCodeKey(QueryFragment queryFragment, Map<Stri
*/
private static void processSearchKey(QueryFragment queryFragment, Map<String, Object> 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"));
}
}

Expand Down

0 comments on commit 5733f14

Please sign in to comment.