diff --git a/src/services/api/search/v2/extract/build.search.query.ts b/src/services/api/search/v2/extract/build.search.query.ts index 252c1ef73e..3491db5949 100644 --- a/src/services/api/search/v2/extract/build.search.query.ts +++ b/src/services/api/search/v2/extract/build.search.query.ts @@ -1,55 +1,87 @@ +import { SpaceVisibility } from '@common/enums/space.visibility'; import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types'; export const buildSearchQuery = ( terms: string, - spaceIdFilter?: string -): QueryDslQueryContainer => ({ - bool: { - must: [ - { - // match the terms in any TEXT field - // accumulate the score from all fields - more matches on more fields will result in a higher score - multi_match: { - query: terms, - type: 'most_fields', - fields: ['*'], - }, - }, - ], - // filter the results by the spaceID - filter: buildFilter(spaceIdFilter), - }, -}); - -const buildFilter = ( - spaceIdFilter?: string -): QueryDslQueryContainer | undefined => { - if (!spaceIdFilter) { - return undefined; + options?: { + spaceIdFilter?: string; + excludeDemoSpaces?: boolean; } - +): QueryDslQueryContainer => { + const { spaceIdFilter, excludeDemoSpaces } = options ?? {}; return { bool: { - minimum_should_match: 1, - should: [ - // the spaceID field is not applicable for some entities, - // so we want them included in the results + must: [ { - bool: { - must_not: { - exists: { - field: 'spaceID', + // Match the terms in any TEXT field + // Accumulate the score from all fields - more matches on more fields will result in a higher score + multi_match: { + query: terms, + type: 'most_fields', + fields: ['*'], + }, + }, + ], + // Filter the results by the spaceID and visibility + filter: buildFilter({ + spaceIdFilter, + excludeDemoSpaces, + }), + }, + }; +}; + +const buildFilter = (opts?: { + spaceIdFilter?: string; + excludeDemoSpaces?: boolean; +}): QueryDslQueryContainer | undefined => { + const { spaceIdFilter, excludeDemoSpaces } = opts ?? {}; + + const filters: QueryDslQueryContainer[] = []; + + if (spaceIdFilter) { + filters.push({ + bool: { + // match either of the two conditions + minimum_should_match: 1, + should: [ + // the spaceID field is not applicable for some entities, + // so we want them included in the results + { + bool: { + must_not: { + exists: { + field: 'spaceID', + }, }, }, }, - }, - // if the spaceID field exists, we want to filter by it - { - term: { - spaceID: spaceIdFilter, + // if the spaceID field exists, we want to filter by it + { + term: { + spaceID: spaceIdFilter, + }, }, - }, - ], + ], + }, + }); + } + + if (excludeDemoSpaces) { + filters.push({ + term: { + visibility: SpaceVisibility.ACTIVE, + }, + }); + } + + if (filters.length === 0) { + return undefined; + } + + return { + bool: { + must: filters, }, }; }; diff --git a/src/services/api/search/v2/extract/search.extract.service.ts b/src/services/api/search/v2/extract/search.extract.service.ts index 2d2a3b095c..de76888acc 100644 --- a/src/services/api/search/v2/extract/search.extract.service.ts +++ b/src/services/api/search/v2/extract/search.extract.service.ts @@ -70,7 +70,7 @@ export class SearchExtractService { public async search( searchData: SearchInput, - onlyPublicResults: boolean + excludeDemoSpaces: boolean ): Promise { if (!this.client) { throw new Error('Elasticsearch client not initialized'); @@ -82,10 +82,13 @@ export class SearchExtractService { const terms = filteredTerms.join(' '); const indicesToSearchOn = this.getIndices( searchData.typesFilter, - onlyPublicResults + excludeDemoSpaces ); // the main search query built using query DSL - const query = buildSearchQuery(terms, searchData.searchInSpaceFilter); + const query = buildSearchQuery(terms, { + spaceIdFilter: searchData.searchInSpaceFilter, + excludeDemoSpaces, + }); // used with function_score to boost results based on visibility const functions = functionScoreFunctions; diff --git a/src/services/api/search/v2/result/search.result.service.ts b/src/services/api/search/v2/result/search.result.service.ts index 359c44ca14..f7471fdc37 100644 --- a/src/services/api/search/v2/result/search.result.service.ts +++ b/src/services/api/search/v2/result/search.result.service.ts @@ -182,7 +182,9 @@ export class SearchResultService { const subspaceIds = rawSearchResults.map(hit => hit.result.id); const subspaces = await this.entityManager.find(Space, { - where: { id: In(subspaceIds) }, + where: { + id: In(subspaceIds), + }, relations: { parentSpace: true }, }); diff --git a/src/services/api/search/v2/search2.service.ts b/src/services/api/search/v2/search2.service.ts index 9110c3239d..217e2a625d 100644 --- a/src/services/api/search/v2/search2.service.ts +++ b/src/services/api/search/v2/search2.service.ts @@ -23,7 +23,7 @@ export class Search2Service { searchData: SearchInput, agentInfo: AgentInfo ): Promise { - const onlyPublicResults = !agentInfo.email; + const excludeDemoSpaces = !agentInfo.email; if ( searchData.searchInSpaceFilter && !isUUID(searchData.searchInSpaceFilter) @@ -45,7 +45,7 @@ export class Search2Service { } const searchResults = await this.searchExtractService.search( searchData, - onlyPublicResults + excludeDemoSpaces ); return this.searchResultService.resolveSearchResults( searchResults,