Skip to content

Commit

Permalink
[Search] Lunr search engine support (backstage#5290)
Browse files Browse the repository at this point in the history
* add lunr package

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* add search translator type and search engine interface

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* (wip) add support for lunr search engine

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* lunr search engine support

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* clean up todo comments

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* typing and cleanups

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* move lunr type package from dev deps to deps

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* check if documents exist to index

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* test fixup

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* changeset

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* move LunrSearchEngine.ts to /engines and add tests

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* update imports

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* update error message

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* add comment to index rotation

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* Update plugins/search-backend-node/src/types.ts

Signed-off-by: Fredrik Adelöw freben@gmail.com

Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* Update plugins/search-backend-node/src/engines/LunrSearchEngine.ts

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

Co-authored-by: Fredrik Adelöw <freben@gmail.com>

* Update plugins/search-backend-node/src/engines/LunrSearchEngine.ts

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

Co-authored-by: Fredrik Adelöw <freben@gmail.com>

* fix imports

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* use type assertion to specify more specific ConcreteLunrQuery type

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* fix imports

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* consistent naming

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* change search engine to be parameter of constructor in indexBuilder

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* make engine required in router options and pass it through in createRouter used in standalone server

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* fix tests

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* delete import

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* add types to SearchQuery interface to make it possible to scope to specific index + test

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* clean up tests

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

* handle case when a filter is added on a field that does not exist on all documents + test

Signed-off-by: Emma Indal <emma.indahl@gmail.com>

Co-authored-by: Fredrik Adelöw <freben@gmail.com>
  • Loading branch information
emmaindal and freben authored Apr 22, 2021
1 parent 47d511b commit b9b2b4b
Show file tree
Hide file tree
Showing 16 changed files with 582 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changeset/dry-carrots-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@backstage/plugin-search-backend': patch
'@backstage/plugin-search-backend-node': patch
---

Lunr Search Engine support
9 changes: 7 additions & 2 deletions packages/backend/src/plugins/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
*/
import { useHotCleanup } from '@backstage/backend-common';
import { createRouter } from '@backstage/plugin-search-backend';
import { IndexBuilder } from '@backstage/plugin-search-backend-node';
import {
IndexBuilder,
LunrSearchEngine,
} from '@backstage/plugin-search-backend-node';
import { PluginEnvironment } from '../types';
import { DefaultCatalogCollator } from '@backstage/plugin-catalog-backend';

export default async function createPlugin({
logger,
discovery,
}: PluginEnvironment) {
const indexBuilder = new IndexBuilder({ logger });
const searchEngine = new LunrSearchEngine({ logger });
const indexBuilder = new IndexBuilder({ logger, searchEngine });

indexBuilder.addCollator({
type: 'software-catalog',
Expand All @@ -38,6 +42,7 @@ export default async function createPlugin({
useHotCleanup(module, () => clearInterval(timerId));

return await createRouter({
engine: indexBuilder.getSearchEngine(),
logger,
});
}
1 change: 1 addition & 0 deletions packages/search-common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { JsonObject } from '@backstage/config';
export interface SearchQuery {
term: string;
filters?: JsonObject;
types?: string[];
pageCursor: string;
}

Expand Down
4 changes: 3 additions & 1 deletion plugins/search-backend-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
},
"dependencies": {
"@backstage/search-common": "^0.1.1",
"winston": "^3.2.1"
"winston": "^3.2.1",
"lunr": "^2.3.9",
"@types/lunr": "^2.3.3"
},
"devDependencies": {
"@backstage/backend-common": "^0.6.0",
Expand Down
19 changes: 17 additions & 2 deletions plugins/search-backend-node/src/IndexBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

import { DocumentCollator, DocumentDecorator } from '@backstage/search-common';
import { Logger } from 'winston';

import {
RegisterCollatorParameters,
RegisterDecoratorParameters,
SearchEngine,
} from './types';

interface CollatorEnvelope {
Expand All @@ -27,18 +29,25 @@ interface CollatorEnvelope {
}

type IndexBuilderOptions = {
searchEngine: SearchEngine;
logger: Logger;
};

export class IndexBuilder {
private collators: Record<string, CollatorEnvelope>;
private decorators: Record<string, DocumentDecorator[]>;
private searchEngine: SearchEngine;
private logger: Logger;

constructor({ logger }: IndexBuilderOptions) {
constructor({ logger, searchEngine }: IndexBuilderOptions) {
this.collators = {};
this.decorators = {};
this.logger = logger;
this.searchEngine = searchEngine;
}

getSearchEngine(): SearchEngine {
return this.searchEngine;
}

/**
Expand Down Expand Up @@ -106,7 +115,13 @@ export class IndexBuilder {
documents = await decorators[i].execute(documents);
}

// TODO: push documents to a configured search engine.
if (!documents || documents.length === 0) {
this.logger.info(`No documents for type "${type}" to index`);
return;
}

// pushing documents to index to a configured search engine.
this.searchEngine.index(type, documents);
}),
);
}
Expand Down
Loading

0 comments on commit b9b2b4b

Please sign in to comment.