Skip to content

Commit

Permalink
use catalog endpoint in resource list (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebrehault authored Feb 8, 2023
1 parent ec5b69b commit c1a7a70
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 26 deletions.
35 changes: 19 additions & 16 deletions apps/dashboard/src/app/resources/resource-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
resourceToAlgoliaFormat,
Search,
SortOrder,
SearchOptions,
} from '@nuclia/core';
import { BackendConfigurationService, SDKService, StateService, STFUtils } from '@flaps/core';
import { SisModalService, SisToastService } from '@nuclia/sistema';
Expand Down Expand Up @@ -584,21 +585,25 @@ export class ResourceListComponent implements AfterViewInit, OnInit, OnDestroy {
}),
switchMap(() => this.sdk.currentKb.pipe(take(1))),
switchMap((kb) => {
const searchFeatures =
hasQuery && !titleOnly
? [Search.Features.PARAGRAPH, Search.Features.VECTOR, Search.Features.DOCUMENT]
: [Search.Features.DOCUMENT];
const status = this.statusDisplayed.value;
const searchOptions: SearchOptions = {
page_number: page,
page_size: this.pageSize,
sort: { field: 'created' },
filters: status === RESOURCE_STATUS.PROCESSED ? undefined : [`/n/s/${status}`],
with_status: status === RESOURCE_STATUS.PROCESSED ? status : undefined,
};
return forkJoin([
of(kb),
kb.search(query, searchFeatures, {
inTitleOnly: titleOnly,
page_number: page,
page_size: this.pageSize,
sort: { field: 'created' },
filters: status === RESOURCE_STATUS.PROCESSED ? undefined : [`/n/s/${status}`],
with_status: status === RESOURCE_STATUS.PROCESSED ? status : undefined,
}),
titleOnly
? kb.catalog(query, searchOptions)
: kb.search(
query,
hasQuery
? [Search.Features.PARAGRAPH, Search.Features.VECTOR, Search.Features.DOCUMENT]
: [Search.Features.DOCUMENT],
searchOptions,
),
this.labelSets$.pipe(take(1)),
this.statusCount.pipe(take(1)),
]);
Expand Down Expand Up @@ -826,8 +831,7 @@ export class ResourceListComponent implements AfterViewInit, OnInit, OnDestroy {
return this.sdk.currentKb.pipe(
take(1),
switchMap((kb) =>
kb.search('', [Search.Features.DOCUMENT], {
inTitleOnly: true,
kb.catalog('', {
faceted: [statusFacet],
}),
),
Expand Down Expand Up @@ -884,8 +888,7 @@ export class ResourceListComponent implements AfterViewInit, OnInit, OnDestroy {
}

private getResourcesInError(kb: KnowledgeBox, page_number = 0): Observable<Search.Results> {
return kb.search('', [Search.Features.DOCUMENT], {
inTitleOnly: true,
return kb.catalog('', {
page_number,
page_size: 20,
sort: { field: 'created' },
Expand Down
5 changes: 3 additions & 2 deletions libs/sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 1.1.3 (unreleased)
# 1.1.3 (2023-02-08)

### Improvements

Expand All @@ -11,10 +11,11 @@
- Update training types: labeler split in two `resoure-labeler` and `paragraph-labeler`
- Add `field` property to `Search.SmartResult` interface
- Add support for `/train/executions` endpoint
- Add `SHORT_FIELD_TYPE` enum in search models and update `Paragraph` and `Sentence` interfaces accordingly
- Add `SHORT_FIELD_TYPE` enum in search models and update `Paragraph` and `Sentence` interfaces accordingly
- Add mappers to translate `SHORT_FIELD_TYPE` into `FIELD_TYPE` and _vice versa_
- Add mapper `getDataKeyFromFieldType` to translate `FIELD_TYPE` into key of `ResourceData`
- Don't load entities by default when loading entity families, add `withEntities` parameter to `getEntities` method to load entities as well
- Add the `/catalog` endpoint

# 1.1.2 (2023-01-20)

Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nuclia/core",
"version": "1.1.2",
"version": "1.1.3",
"description": "SDK allowing to integrate Nuclia services in your frontend application",
"license": "MIT",
"keywords": [
Expand Down
2 changes: 2 additions & 0 deletions libs/sdk-core/src/lib/db/kb/kb.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export interface IKnowledgeBox extends IKnowledgeBoxCreation {

search(query: string, features?: Search.Features[], options?: SearchOptions): Observable<Search.Results>;

catalog(query: string, options?: SearchOptions): Observable<Search.Results>;

suggest(query: string): Observable<Search.Suggestions>;

counters(): Observable<Counters>;
Expand Down
6 changes: 5 additions & 1 deletion libs/sdk-core/src/lib/db/kb/kb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { ICreateResource, IResource, LinkField, UserMetadata } from '../res
import { Resource } from '../resource';
import type { UploadResponse } from '../upload';
import { batchUpload, FileMetadata, FileWithMetadata, upload, UploadStatus } from '../upload';
import type { Search, SearchOptions } from '../search';
import { catalog, Search, SearchOptions } from '../search';
import { search } from '../search';
import { Training } from '../training';

Expand Down Expand Up @@ -129,6 +129,10 @@ export class KnowledgeBox implements IKnowledgeBox {
return search(this.nuclia, this.id, this.path, query, features, options);
}

catalog(query: string, options?: SearchOptions): Observable<Search.Results> {
return catalog(this.nuclia, this.id, query, options);
}

suggest(query: string, inTitleOnly = false): Observable<Search.Suggestions> {
const params = `query=${encodeURIComponent(query)}${inTitleOnly ? '&fields=a/title' : ''}`;
return this.nuclia.rest.get<Search.Suggestions | { detail: string }>(`${this.path}/suggest?${params}`).pipe(
Expand Down
32 changes: 26 additions & 6 deletions libs/sdk-core/src/lib/db/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,34 @@ export const search = (
);
};

export const catalog = (nuclia: INuclia, kbid: string, query: string, options?: SearchOptions) => {
const params: { [key: string]: string | string[] } = {};
params.query = query || '';
params.shards = nuclia.currentShards?.[kbid] || [];
const searchMethod = nuclia.rest.get<Search.Results | { detail: string }>(
`/kb/${kbid}/catalog?${options ? serialize(params, options) : ''}`,
);
return searchMethod.pipe(
catchError(() => of({ error: true } as Search.Results)),
map((res) => (Object.keys(res).includes('detail') ? ({ error: true } as Search.Results) : (res as Search.Results))),
tap((res) => {
if (res.shards) {
nuclia.currentShards = { ...nuclia.currentShards, [kbid]: res.shards };
}
}),
);
};

const serialize = (params: { [key: string]: string | string[] }, others: SearchOptions): string => {
Object.entries(others || {}).forEach(([key, value]) => {
if (Array.isArray(value)) {
params[key] = value.map((el) => `${el}`);
} else if (typeof value === 'object') {
Object.entries(value).forEach(([k, v]) => (params[`${key}_${k}`] = `${v}`));
} else {
params[key] = `${value}`;
if (value !== undefined && value !== null) {
if (Array.isArray(value)) {
params[key] = value.map((el) => `${el}`);
} else if (typeof value === 'object') {
Object.entries(value).forEach(([k, v]) => (params[`${key}_${k}`] = `${v}`));
} else {
params[key] = `${value}`;
}
}
});
const queryParams = new URLSearchParams();
Expand Down

0 comments on commit c1a7a70

Please sign in to comment.