diff --git a/plugins/search-backend-module-pg/api-report.md b/plugins/search-backend-module-pg/api-report.md index 0d03a83d34..cd47df837c 100644 --- a/plugins/search-backend-module-pg/api-report.md +++ b/plugins/search-backend-module-pg/api-report.md @@ -12,9 +12,7 @@ import { PluginDatabaseManager } from '@backstage/backend-common'; import { SearchEngine } from '@backstage/plugin-search-backend-node'; import { SearchQuery } from '@backstage/plugin-search-common'; -// Warning: (ae-missing-release-tag) "ConcretePgSearchQuery" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// -// @public (undocumented) +// @public export type ConcretePgSearchQuery = { pgQuery: PgSearchQuery; pageSize: number; @@ -97,15 +95,13 @@ export class PgSearchEngine implements SearchEngine { // (undocumented) query(query: SearchQuery): Promise; // (undocumented) - setTranslator( - translator: (query: SearchQuery) => ConcretePgSearchQuery, - ): void; + setTranslator(translator: PgSearchQueryTranslator): void; // (undocumented) static supported(database: PluginDatabaseManager): Promise; // (undocumented) translator( query: SearchQuery, - options: PgSearchHighlightOptions, + options: PgSearchQueryTranslatorOptions, ): ConcretePgSearchQuery; } @@ -168,6 +164,17 @@ export interface PgSearchQuery { types?: string[]; } +// @public +export type PgSearchQueryTranslator = ( + query: SearchQuery, + options: PgSearchQueryTranslatorOptions, +) => ConcretePgSearchQuery; + +// @public +export type PgSearchQueryTranslatorOptions = { + highlightOptions: PgSearchHighlightOptions; +}; + // Warning: (ae-missing-release-tag) "RawDocumentRow" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) diff --git a/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.test.ts b/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.test.ts index 5ad9d12897..23f8983074 100644 --- a/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.test.ts +++ b/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.test.ts @@ -15,12 +15,12 @@ */ import { ConfigReader } from '@backstage/config'; import { DatabaseStore } from '../database'; -import { PgSearchHighlightOptions } from '../types'; import { ConcretePgSearchQuery, decodePageCursor, encodePageCursor, PgSearchEngine, + PgSearchHighlightOptions, } from './PgSearchEngine'; import { PgSearchEngineIndexer } from './PgSearchEngineIndexer'; @@ -86,7 +86,7 @@ describe('PgSearchEngine', () => { term: 'testTerm', filters: {}, }, - highlightOptions, + { highlightOptions }, ); }); @@ -96,7 +96,7 @@ describe('PgSearchEngine', () => { term: 'Hello', pageCursor: 'MQ==', }, - highlightOptions, + { highlightOptions }, ); expect(actualTranslatedQuery).toMatchObject({ @@ -114,7 +114,7 @@ describe('PgSearchEngine', () => { { term: 'Hello World', }, - highlightOptions, + { highlightOptions }, ); expect(actualTranslatedQuery).toMatchObject({ @@ -133,7 +133,7 @@ describe('PgSearchEngine', () => { term: 'H&e|l!l*o W\0o(r)l:d', pageCursor: '', }, - highlightOptions, + { highlightOptions }, ) as ConcretePgSearchQuery; expect(actualTranslatedQuery).toMatchObject({ @@ -151,7 +151,7 @@ describe('PgSearchEngine', () => { filters: { kind: 'testKind' }, types: ['my-filter'], }, - highlightOptions, + { highlightOptions }, ); expect(actualTranslatedQuery).toMatchObject({ diff --git a/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts b/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts index b6e57552ba..3f4d419fab 100644 --- a/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts +++ b/plugins/search-backend-module-pg/src/PgSearchEngine/PgSearchEngine.ts @@ -28,13 +28,58 @@ import { } from '../database'; import { v4 as uuid } from 'uuid'; import { Config } from '@backstage/config'; -import { PgSearchHighlightOptions, PgSearchOptions } from '../types'; +/** + * Search query that the Postgres search engine understands. + * @public + */ export type ConcretePgSearchQuery = { pgQuery: PgSearchQuery; pageSize: number; }; +/** + * Options available for the Postgres specific query translator. + * @public + */ +export type PgSearchQueryTranslatorOptions = { + highlightOptions: PgSearchHighlightOptions; +}; + +/** + * Postgres specific query translator. + * @public + */ +export type PgSearchQueryTranslator = ( + query: SearchQuery, + options: PgSearchQueryTranslatorOptions, +) => ConcretePgSearchQuery; + +/** + * Options to instantiate PgSearchEngine + * @public + */ +export type PgSearchOptions = { + config: Config; + database: PluginDatabaseManager; +}; + +/** + * Options for highlighting search terms + * @public + */ +export type PgSearchHighlightOptions = { + useHighlight?: boolean; + maxWords?: number; + minWords?: number; + shortWord?: number; + highlightAll?: boolean; + maxFragments?: number; + fragmentDelimiter?: string; + preTag: string; + postTag: string; +}; + export class PgSearchEngine implements SearchEngine { private readonly highlightOptions: PgSearchHighlightOptions; constructor(private readonly databaseStore: DatabaseStore, config: Config) { @@ -85,7 +130,7 @@ export class PgSearchEngine implements SearchEngine { translator( query: SearchQuery, - options: PgSearchHighlightOptions, + options: PgSearchQueryTranslatorOptions, ): ConcretePgSearchQuery { const pageSize = 25; const { page } = decodePageCursor(query.pageCursor); @@ -105,15 +150,13 @@ export class PgSearchEngine implements SearchEngine { types: query.types, offset, limit, - options, + options: options.highlightOptions, }, pageSize, }; } - setTranslator( - translator: (query: SearchQuery) => ConcretePgSearchQuery, - ): void { + setTranslator(translator: PgSearchQueryTranslator) { this.translator = translator; } @@ -126,7 +169,9 @@ export class PgSearchEngine implements SearchEngine { } async query(query: SearchQuery): Promise { - const { pgQuery, pageSize } = this.translator(query, this.highlightOptions); + const { pgQuery, pageSize } = this.translator(query, { + highlightOptions: this.highlightOptions, + }); const rows = await this.databaseStore.transaction(async tx => this.databaseStore.query(tx, pgQuery), diff --git a/plugins/search-backend-module-pg/src/PgSearchEngine/index.ts b/plugins/search-backend-module-pg/src/PgSearchEngine/index.ts index 7f8e297648..e9ea04e5c4 100644 --- a/plugins/search-backend-module-pg/src/PgSearchEngine/index.ts +++ b/plugins/search-backend-module-pg/src/PgSearchEngine/index.ts @@ -14,7 +14,13 @@ * limitations under the License. */ export { PgSearchEngine } from './PgSearchEngine'; -export type { ConcretePgSearchQuery } from './PgSearchEngine'; +export type { + ConcretePgSearchQuery, + PgSearchQueryTranslatorOptions, + PgSearchQueryTranslator, + PgSearchOptions, + PgSearchHighlightOptions, +} from './PgSearchEngine'; export type { PgSearchEngineIndexer, PgSearchEngineIndexerOptions, diff --git a/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts b/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts index f4b3e6fb0b..355e41352d 100644 --- a/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts +++ b/plugins/search-backend-module-pg/src/database/DatabaseDocumentStore.test.ts @@ -15,7 +15,7 @@ */ import { TestDatabaseId, TestDatabases } from '@backstage/backend-test-utils'; import { IndexableDocument } from '@backstage/plugin-search-common'; -import { PgSearchHighlightOptions } from '../types'; +import { PgSearchHighlightOptions } from '../PgSearchEngine'; import { DatabaseDocumentStore } from './DatabaseDocumentStore'; const highlightOptions: PgSearchHighlightOptions = { diff --git a/plugins/search-backend-module-pg/src/database/types.ts b/plugins/search-backend-module-pg/src/database/types.ts index 43bda32050..151be7d13f 100644 --- a/plugins/search-backend-module-pg/src/database/types.ts +++ b/plugins/search-backend-module-pg/src/database/types.ts @@ -15,7 +15,7 @@ */ import { IndexableDocument } from '@backstage/plugin-search-common'; import { Knex } from 'knex'; -import { PgSearchHighlightOptions } from '../types'; +import { PgSearchHighlightOptions } from '../PgSearchEngine'; export interface PgSearchQuery { fields?: Record; diff --git a/plugins/search-backend-module-pg/src/index.ts b/plugins/search-backend-module-pg/src/index.ts index 86184d1e4e..fbc52735dd 100644 --- a/plugins/search-backend-module-pg/src/index.ts +++ b/plugins/search-backend-module-pg/src/index.ts @@ -22,4 +22,3 @@ export * from './database'; export * from './PgSearchEngine'; -export type { PgSearchOptions, PgSearchHighlightOptions } from './types'; diff --git a/plugins/search-backend-module-pg/src/types.ts b/plugins/search-backend-module-pg/src/types.ts deleted file mode 100644 index 5e32c69e7a..0000000000 --- a/plugins/search-backend-module-pg/src/types.ts +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2022 The Backstage Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { PluginDatabaseManager } from '@backstage/backend-common'; -import { Config } from '@backstage/config'; - -/** - * Options to instantiate PgSearchEngine - * @public - */ -export type PgSearchOptions = { - config: Config; - database: PluginDatabaseManager; -}; - -/** - * Options for highlighting search terms - * @public - */ -export type PgSearchHighlightOptions = { - useHighlight?: boolean; - maxWords?: number; - minWords?: number; - shortWord?: number; - highlightAll?: boolean; - maxFragments?: number; - fragmentDelimiter?: string; - preTag: string; - postTag: string; -};