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

filter out demo spaces for unauthenticated users search in elastic #4712

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
77 changes: 50 additions & 27 deletions src/services/api/search/v2/extract/build.search.query.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,78 @@
import { SpaceVisibility } from '@common/enums/space.visibility';
import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';

export const buildSearchQuery = (
terms: string,
spaceIdFilter?: 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
// 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),
// Filter the results by the spaceID and visibility
filter: buildFilter(
spaceIdFilter,
onlyPublicResults === true ? SpaceVisibility.ACTIVE : undefined
),
},
});

const buildFilter = (
spaceIdFilter?: string
spaceIdFilter?: string,
visibilityFilter?: string
): QueryDslQueryContainer | undefined => {
if (!spaceIdFilter) {
return undefined;
}
const filters: QueryDslQueryContainer[] = [];

return {
bool: {
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 (spaceIdFilter) {
filters.push({
bool: {
minimum_should_match: 1,
should: [
// Include entities without spaceID
{
bool: {
must_not: {
exists: {
field: 'spaceID',
},
},
},
},
},
// if the spaceID field exists, we want to filter by it
{
term: {
spaceID: spaceIdFilter,
// Filter entities with the specified spaceID
{
term: {
spaceID: spaceIdFilter,
},
},
},
],
],
},
});
}

if (visibilityFilter) {
filters.push({
term: {
visibility: visibilityFilter,
},
});
}

if (filters.length === 0) {
return undefined;
}

return {
bool: {
must: filters,
},
};
};
6 changes: 5 additions & 1 deletion src/services/api/search/v2/extract/search.extract.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ export class SearchExtractService {
onlyPublicResults
);
// the main search query built using query DSL
const query = buildSearchQuery(terms, searchData.searchInSpaceFilter);
const query = buildSearchQuery(
terms,
searchData.searchInSpaceFilter,
onlyPublicResults
);
// 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