export async function queryIndexHandler()

in firestore-semantic-search/functions/src/functions/query_index.ts [34:80]


export async function queryIndexHandler(data: any) {
  const {query, neighbours} = data;

  if (!query || !queryValid(query)) {
    throw new functions.https.HttpsError(
      'invalid-argument',
      'The function must be called with ' +
        'one argument "query" containing an Array with at least one string.'
    );
  }

  const neighboursCount = parseInt(neighbours) || 10;

  const queryEmbeddings = await getEmbeddings(query);

  const metadataDoc = admin.firestore().doc(config.metadataDoc);
  const metadata = await metadataDoc.get();
  const {publicEndpointDomainName, indexEndpoint} = metadata.data() || {};

  if (!publicEndpointDomainName || !indexEndpoint) {
    throw new functions.https.HttpsError(
      'not-found',
      'Endpoint or index endpoint is not found.'
    );
  }

  try {
    const result = await queryIndex(
      [new Query('0', queryEmbeddings[0])],
      neighboursCount,
      publicEndpointDomainName,
      indexEndpoint.split('/').pop()
    );

    functions.logger.info('Query successful', result);

    return {status: 'ok', message: 'Query successful', data: result};
  } catch (error) {
    const axiosError = error as AxiosError;
    functions.logger.error('Error querying the index', axiosError.message);
    throw new functions.https.HttpsError(
      'internal',
      'Error querying the index',
      axiosError.message
    );
  }
}