Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Content Management] Cross Type Search (savedObjects.find() based) #154464

Merged
merged 9 commits into from
Apr 6, 2023

Conversation

Dosant
Copy link
Contributor

@Dosant Dosant commented Apr 5, 2023

Summary

Partially addresses #152224
Tech Doc (private)

Introduce mSearch - temporary cross-content type search layer for content management backed by savedObjects.find

Until we have a dedicated search layer in CM services, we want to provide a temporary solution to replace client-side or naive server proxy usages of savedObjects.find() across multiple types with Content Management API to prepare these places for backward compatibility compliance.

Later we plan to use the new API together with shared components that work across multiple types like SavedObjectFinder or TableListView

The api would only work with content types that use saved objects as a backend.

To opt-in a saved object backed content type to mSearch need to provide MSearchConfig on ContentStorage:

export class MapsStorage implements ContentStorage<Map> {
  // ... 
  mSearch: {
          savedObjectType: 'maps',
          toItemResult: (ctx: StorageContext, mapsSavedObject: SavedObject<MapsAttributes>) => toMap(ctx,mapsSavedObject), // transform, validate, version
          additionalSearchFields: ['something-maps-specific'],
        }
}

Out of scope of this PR:

  • tags search (will follow up shortly)
  • pagination (as described in [the doc](Tech Doc) server-side pagination is not needed for the first consumers, but will follow up shortly)
  • end-to-end / integration testing (don't want to introduce a dummy saved object for testing this, instead plan to wait for maps CM onboard and test using maps [Content Management] Maps onboard #153304)
  • Documentation (will add into [CM] Add content onboarding docs #154453)
  • Add rxjs and hook method

@Dosant Dosant changed the title D/2023 04 05 msearch 1 [Content Management] Cross Type Search (savedObjects.find() based) Apr 5, 2023
@Dosant Dosant added Team:SharedUX Team label for AppEx-SharedUX (formerly Global Experience) Feature:Content Management User generated content (saved objects) management labels Apr 5, 2023
@Dosant Dosant marked this pull request as ready for review April 5, 2023 17:59
@Dosant Dosant requested a review from a team as a code owner April 5, 2023 17:59
@elasticmachine
Copy link
Contributor

Pinging @elastic/appex-sharedux (Team:SharedUX)

@Dosant Dosant requested a review from sebelga April 5, 2023 18:00
@Dosant Dosant added the release_note:skip Skip the PR/issue when compiling release notes label Apr 5, 2023
Copy link
Contributor

@sebelga sebelga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work @Dosant ! I left a small comment about readability but otherwise looks good. Looking forward to see some integration 👍

contentTypes: input.contentTypes.map((contentType) =>
addVersion(contentType, this.contentTypeRegistry)
),
}) as Promise<MSearchResult<T>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can avoid this casting by typing the CrudClient

export interface CrudClient {
  ...
  delete(input: DeleteIn): Promise<unknown>;
  search(input: SearchIn): Promise<unknown>;
  mSearch?<T = unknown>(input: MSearchIn): Promise<MSearchResult<T>>; // here
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, thanks. I plan to clean up all the client-side types in the separate pr

}
) {}

async search(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I had to put a few comments and spaces to make it slightly more readable (quite a few Map around!).

That's how I left it in local

async search(
    contentTypes: Array<{ contentTypeId: string; ctx: StorageContext }>,
    query: SearchQuery
  ): Promise<MSearchResult> {
    // Map: contentTypeId -> StorageContext
    const contentTypeToCtx = new Map(contentTypes.map((ct) => [ct.contentTypeId, ct.ctx]));

    // Map: contentTypeId -> MSearchConfig
    const contentTypeToMSearchConfig = new Map(
      contentTypes.map((ct) => {
        const mSearchConfig = this.deps.contentRegistry.getDefinition(ct.contentTypeId).storage
          .mSearch;
        if (!mSearchConfig) {
          throw new Error(`Content type ${ct.contentTypeId} does not support mSearch`);
        }
        return [ct.contentTypeId, mSearchConfig];
      })
    );

    // Map: Saved object type -> [contentTypeId, MSearchConfig]
    const soTypeToMSearchConfig = new Map(
      Array.from(contentTypeToMSearchConfig.entries()).map(([ct, mSearchConfig]) => {
        return [mSearchConfig.savedObjectType, [ct, mSearchConfig] as const];
      })
    );

    const mSearchConfigs = Array.from(contentTypeToMSearchConfig.values());
    const soSearchTypes = mSearchConfigs.map((mSearchConfig) => mSearchConfig.savedObjectType);

    const additionalSearchFields = new Set<string>();
    mSearchConfigs.forEach((mSearchConfig) => {
      (mSearchConfig.additionalSearchFields ?? []).forEach(additionalSearchFields.add);
    });

    const savedObjectsClient = await this.deps.getSavedObjectsClient();
    const soResult = await savedObjectsClient.find({
      type: soSearchTypes,
      search: query.text,
      searchFields: [`title^3`, `description`, ...additionalSearchFields],
      defaultSearchOperator: 'AND',
      // TODO: tags
      // TODO: pagination
      // TODO: sort
    });
   
   ...

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
contentManagement 112 124 +12

Public APIs missing exports

Total count of every type that is part of your API that should be exported but is not. This will cause broken links in the API documentation system. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats exports for more detailed information.

id before after diff
contentManagement 5 6 +1

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
contentManagement 7.2KB 7.5KB +291.0B
Unknown metric groups

API count

id before after diff
contentManagement 130 143 +13

ESLint disabled line counts

id before after diff
securitySolution 433 436 +3

Total ESLint disabled count

id before after diff
securitySolution 513 516 +3

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@Dosant Dosant merged commit 0936601 into elastic:main Apr 6, 2023
@kibanamachine kibanamachine added v8.8.0 backport:skip This commit does not require backporting labels Apr 6, 2023
@Dosant Dosant self-assigned this Apr 6, 2023
Dosant added a commit that referenced this pull request Apr 13, 2023
…54819)

## Summary

Follow up to #154464
Partially resolve #152224 -
implement pagination and tags filtering
Dosant added a commit that referenced this pull request Apr 13, 2023
…onboarding docs (#154896)

## Summary

Follow up to #154464
Partially resolve #152224 - Add
a `msearch` section to CM onboarding docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting Feature:Content Management User generated content (saved objects) management release_note:skip Skip the PR/issue when compiling release notes Team:SharedUX Team label for AppEx-SharedUX (formerly Global Experience) v8.8.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants