Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 72 additions & 40 deletions src/services/api/search/v2/extract/build.search.query.ts
Original file line number Diff line number Diff line change
@@ -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,
},
};
};
9 changes: 6 additions & 3 deletions src/services/api/search/v2/extract/search.extract.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class SearchExtractService {

public async search(
searchData: SearchInput,
onlyPublicResults: boolean
excludeDemoSpaces: boolean
): Promise<ISearchResult[] | never> {
if (!this.client) {
throw new Error('Elasticsearch client not initialized');
Expand All @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion src/services/api/search/v2/result/search.result.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
});

Expand Down
4 changes: 2 additions & 2 deletions src/services/api/search/v2/search2.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Search2Service {
searchData: SearchInput,
agentInfo: AgentInfo
): Promise<ISearchResults> {
const onlyPublicResults = !agentInfo.email;
const excludeDemoSpaces = !agentInfo.email;
if (
searchData.searchInSpaceFilter &&
!isUUID(searchData.searchInSpaceFilter)
Expand All @@ -45,7 +45,7 @@ export class Search2Service {
}
const searchResults = await this.searchExtractService.search(
searchData,
onlyPublicResults
excludeDemoSpaces
);
return this.searchResultService.resolveSearchResults(
searchResults,
Expand Down