From dd71d0fd49452a4591252f5c268f9713d8f504a6 Mon Sep 17 00:00:00 2001 From: Svetoslav Date: Mon, 18 Nov 2024 11:50:16 +0200 Subject: [PATCH] improvements --- .../search/v2/extract/build.search.query.ts | 67 +++++++++++-------- .../v2/extract/search.extract.service.ts | 13 ++-- src/services/api/search/v2/search2.service.ts | 4 +- 3 files changed, 46 insertions(+), 38 deletions(-) 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 481b363d25..3491db5949 100644 --- a/src/services/api/search/v2/extract/build.search.query.ts +++ b/src/services/api/search/v2/extract/build.search.query.ts @@ -3,41 +3,50 @@ import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types'; export const buildSearchQuery = ( terms: string, - spaceIdFilter?: string, - onlyPublicResults: boolean = false -): 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: ['*'], + options?: { + spaceIdFilter?: string; + excludeDemoSpaces?: boolean; + } +): QueryDslQueryContainer => { + const { spaceIdFilter, excludeDemoSpaces } = options ?? {}; + return { + 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 and visibility - filter: buildFilter( - spaceIdFilter, - onlyPublicResults === true ? SpaceVisibility.ACTIVE : undefined - ), - }, -}); + ], + // 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 buildFilter = ( - spaceIdFilter?: string, - visibilityFilter?: string -): QueryDslQueryContainer | undefined => { const filters: QueryDslQueryContainer[] = []; if (spaceIdFilter) { filters.push({ bool: { + // match either of the two conditions minimum_should_match: 1, should: [ - // Include entities without spaceID + // the spaceID field is not applicable for some entities, + // so we want them included in the results { bool: { must_not: { @@ -47,7 +56,7 @@ const buildFilter = ( }, }, }, - // Filter entities with the specified spaceID + // if the spaceID field exists, we want to filter by it { term: { spaceID: spaceIdFilter, @@ -58,10 +67,10 @@ const buildFilter = ( }); } - if (visibilityFilter) { + if (excludeDemoSpaces) { filters.push({ term: { - visibility: visibilityFilter, + visibility: SpaceVisibility.ACTIVE, }, }); } 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 f363c554e6..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,14 +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, - onlyPublicResults - ); + 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/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,