private async executeQueries()

in src/elasticSearchService.ts [352:397]


    private async executeQueries(searchQueries: Query[], request: TypeSearchRequest): Promise<{ hits: any[] }> {
        if (searchQueries.length === 0) {
            return {
                hits: [],
            };
        }

        const searchQueriesWithAlias = searchQueries.map((searchQuery) => ({
            ...searchQuery.queryRequest,
            index: getAliasName(searchQuery.resourceType, request.tenantId),
        }));

        if (logger.isDebugEnabled()) {
            logger.debug(`Elastic msearch query: ${JSON.stringify(searchQueriesWithAlias, null, 2)}`);
        }
        const apiResponse = await this.esClient.msearch({
            body: searchQueriesWithAlias.flatMap((query) => [
                { index: query.index, ...(request.sessionId && { preference: request.sessionId }) },
                { query: query.body!.query },
            ]),
        });

        return (apiResponse.body.responses as any[])
            .filter((response) => {
                if (response.error) {
                    if (response.error.type === 'index_not_found_exception') {
                        // Indexes are created the first time a resource of a given type is written to DDB.
                        logger.info(
                            `Search index for ${response.error.index} does not exist. Returning an empty search result`,
                        );
                        return false;
                    }
                    throw response.error;
                }
                return true;
            })
            .reduce(
                (acc, response) => {
                    acc.hits.push(...response.hits.hits);
                    return acc;
                },
                {
                    hits: [],
                },
            );
    }