Skip to content

Commit

Permalink
Merge pull request #2046 from Courey/courey/get_synonyms_by_ids_serve…
Browse files Browse the repository at this point in the history
…r_only

adding synonym getters
  • Loading branch information
dakota002 authored Mar 7, 2024
2 parents 0600c73 + 58f6eea commit c2a0ff7
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions app/routes/_gcn.synonyms/synonyms.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,101 @@
*/
import { tables } from '@architect/functions'
import type { DynamoDBDocument } from '@aws-sdk/lib-dynamodb/dist-types/DynamoDBDocument'
import { search as getSearchClient } from '@nasa-gcn/architect-functions-search'
import crypto from 'crypto'

import type { Synonym } from './synonyms.lib'

export async function getSynonymsByUuid(uuid: string) {
const db = await tables()

const { Items } = await db.synonym.query({
IndexName: 'synonymsByUuid',
KeyConditionExpression: 'uuid = :uuid',
ExpressionAttributeValues: {
':uuid': uuid,
},
})

return Items as Synonym[]
}

export async function searchSynonymsByEventId({
limit = 10,
page,
eventId,
}: {
limit?: number
page: number
eventId?: string
}): Promise<{
synonyms: Record<string, string[]>
totalItems: number
totalPages: number
page: number
}> {
const client = await getSearchClient()
const query: any = {
bool: {
should: [
{
match_all: {},
},
],
minimum_should_match: 1,
},
}

if (eventId) {
query.bool.should.push({
match: {
eventId: {
query: eventId,
fuzziness: 'AUTO',
},
},
})
}

const {
body: {
hits: {
total: { value: totalItems },
hits,
},
},
} = await client.search({
index: 'synonyms',
from: page && limit && (page - 1) * limit,
size: limit,
body: {
query,
},
})

const totalPages: number = Math.ceil(totalItems / limit)
const results: Record<string, string[]> = {}

hits.forEach(
({
_source: body,
}: {
_source: Synonym
fields: { eventId: string; uuid: string }
}) =>
results[body.uuid]
? results[body.uuid].push(body.eventId)
: (results[body.uuid] = [body.eventId])
)

return {
synonyms: results,
totalItems,
totalPages,
page,
}
}

/*
* If an eventId already has a synonym and is passed in, it will unlink the
* eventId from the old synonym and the only remaining link will be to the
Expand Down

0 comments on commit c2a0ff7

Please sign in to comment.