Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
hero101 committed Nov 18, 2024
1 parent b19e0d2 commit dd71d0f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
67 changes: 38 additions & 29 deletions src/services/api/search/v2/extract/build.search.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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,
Expand All @@ -58,10 +67,10 @@ const buildFilter = (
});
}

if (visibilityFilter) {
if (excludeDemoSpaces) {
filters.push({
term: {
visibility: visibilityFilter,
visibility: SpaceVisibility.ACTIVE,
},
});
}
Expand Down
13 changes: 6 additions & 7 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,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;

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

0 comments on commit dd71d0f

Please sign in to comment.