Skip to content

Commit

Permalink
Handle tsvector type columns when forming the db query (#459)
Browse files Browse the repository at this point in the history
* Support full text search queries

* Handle text search for all query types
  • Loading branch information
prathamesh0 authored Nov 9, 2023
1 parent 483a73f commit 6860f1a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
36 changes: 35 additions & 1 deletion packages/util/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export const OPERATOR_MAP = {
contains_nocase: 'ILIKE',
starts_nocase: 'ILIKE',
ends_nocase: 'ILIKE',
nested: ''
nested: '',
match: '@@'
};

const INSERT_EVENTS_BATCH = 100;
Expand All @@ -66,6 +67,8 @@ export interface QueryOptions {
skip?: number;
orderBy?: string;
orderDirection?: OrderDirection;
tsRankBy?: string;
tsRankValue?: string;
}

export interface Filter {
Expand Down Expand Up @@ -943,6 +946,8 @@ export class Database {
value = this._transformBigValues(value);
if (operator === 'in') {
whereClause += '(:...';
} else if (columnMetadata.type === 'tsvector' && operator === 'match') {
whereClause += 'to_tsquery(:';
} else {
whereClause += ':';
}
Expand All @@ -958,6 +963,10 @@ export class Database {
}
}

if (operator === 'match') {
whereClause += ')';
}

if (!columnIsArray) {
if (['contains', 'contains_nocase', 'ends', 'ends_nocase'].some(el => el === operator)) {
value = `%${value}`;
Expand Down Expand Up @@ -1173,6 +1182,31 @@ export class Database {
);
}

orderTsQuery<Entity extends ObjectLiteral> (
repo: Repository<Entity>,
selectQueryBuilder: SelectQueryBuilder<Entity>,
tsOrderOptions: { tsRankBy?: string, tsRankValue?: string },
columnPrefix = '',
alias?: string
): SelectQueryBuilder<Entity> {
if (!alias) {
alias = selectQueryBuilder.alias;
}

const { tsRankBy, tsRankValue } = tsOrderOptions;
assert(tsRankBy);

const columnMetadata = repo.metadata.findColumnWithPropertyName(tsRankBy);
assert(columnMetadata);

const tsOrderBy = `ts_rank("${alias}"."${columnPrefix}${columnMetadata.databaseName}", to_tsquery('${tsRankValue ?? ''}'))`;

return selectQueryBuilder.addOrderBy(
tsOrderBy,
'ASC'
);
}

async applyBlockHeightFilter<Entity> (
queryRunner: QueryRunner,
queryBuilder: SelectQueryBuilder<Entity>,
Expand Down
20 changes: 20 additions & 0 deletions packages/util/src/graph/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ export class GraphDatabase {

selectQueryBuilder = this._baseDatabase.buildQuery(repo, selectQueryBuilder, where, relationsMap.get(entityType), block);

if (queryOptions.tsRankBy) {
selectQueryBuilder = this._baseDatabase.orderTsQuery(repo, selectQueryBuilder, queryOptions);
}

if (queryOptions.orderBy) {
selectQueryBuilder = await this._baseDatabase.orderQuery(repo, selectQueryBuilder, queryOptions, relationsMap.get(entityType), block);
}
Expand Down Expand Up @@ -508,6 +512,10 @@ export class GraphDatabase {
.from(`(${subQuery.getQuery()})`, 'latestEntities')
.setParameters(subQuery.getParameters()) as SelectQueryBuilder<Entity>;

if (queryOptions.tsRankBy) {
selectQueryBuilder = this._baseDatabase.orderTsQuery(repo, selectQueryBuilder, queryOptions, 'subTable_');
}

if (queryOptions.orderBy) {
selectQueryBuilder = await this._baseDatabase.orderQuery(repo, selectQueryBuilder, queryOptions, relationsMap.get(entityType), block, 'subTable_');
if (queryOptions.orderBy !== 'id') {
Expand Down Expand Up @@ -582,6 +590,10 @@ export class GraphDatabase {

selectQueryBuilder = this._baseDatabase.buildQuery(repo, selectQueryBuilder, where, relationsMap.get(entityType), block);

if (queryOptions.tsRankBy) {
selectQueryBuilder = this._baseDatabase.orderTsQuery(repo, selectQueryBuilder, queryOptions);
}

if (queryOptions.orderBy) {
selectQueryBuilder = await this._baseDatabase.orderQuery(repo, selectQueryBuilder, queryOptions, relationsMap.get(entityType), block);
}
Expand Down Expand Up @@ -644,6 +656,10 @@ export class GraphDatabase {

selectQueryBuilder = this._baseDatabase.buildQuery(repo, selectQueryBuilder, where, relationsMap.get(entityType), {}, 'latest');

if (queryOptions.tsRankBy) {
selectQueryBuilder = this._baseDatabase.orderTsQuery(repo, selectQueryBuilder, queryOptions, '', 'latest');
}

if (queryOptions.orderBy) {
selectQueryBuilder = await this._baseDatabase.orderQuery(repo, selectQueryBuilder, queryOptions, relationsMap.get(entityType), {}, '', 'latest');
}
Expand Down Expand Up @@ -700,6 +716,10 @@ export class GraphDatabase {

selectQueryBuilder = this._baseDatabase.buildQuery(latestEntityRepo, selectQueryBuilder, where, relationsMap.get(entityType), block, 'latest');

if (queryOptions.tsRankBy) {
selectQueryBuilder = this._baseDatabase.orderTsQuery(latestEntityRepo, selectQueryBuilder, queryOptions, '', 'latest');
}

if (queryOptions.orderBy) {
selectQueryBuilder = await this._baseDatabase.orderQuery(latestEntityRepo, selectQueryBuilder, queryOptions, relationsMap.get(entityType), block, '', 'latest');
}
Expand Down

0 comments on commit 6860f1a

Please sign in to comment.