2024-02-13 13:37:58 -08:00
|
|
|
import { documents, authors, affiliations, topics } from '@/app/db/data'
|
2024-02-12 21:21:55 -08:00
|
|
|
import MiniSearch from 'minisearch'
|
|
|
|
import { CustomSearchResult } from './searchDocs'
|
2024-02-15 15:11:34 -08:00
|
|
|
import crypto from 'crypto'
|
|
|
|
import hash from './hash'
|
2024-02-12 21:21:55 -08:00
|
|
|
|
2024-02-13 13:37:58 -08:00
|
|
|
// converting the documents object into an array that can be index by minisearch
|
2024-02-12 21:21:55 -08:00
|
|
|
const docs = Object.entries(documents).map(([key, value]) => ({
|
|
|
|
id: key,
|
|
|
|
keywords: value.manifest.keywords.join(' '),
|
|
|
|
abstract: value.abstract,
|
2024-02-13 13:37:58 -08:00
|
|
|
topics: value.manifest.topics
|
|
|
|
.map((topicId) => topics[topicId])
|
|
|
|
.map((topic) => topic.name)
|
|
|
|
.join(' '),
|
|
|
|
authors: value.manifest.authors // pull out author data from the user id
|
|
|
|
.map((authorId) => authors[authorId])
|
|
|
|
.map(
|
|
|
|
(author) =>
|
|
|
|
`${author.name.first} ${author.name.last} ${author.name.nickname}`
|
|
|
|
)
|
|
|
|
.join(' '),
|
2024-02-12 21:21:55 -08:00
|
|
|
title: value.manifest.title,
|
|
|
|
manifest: value.manifest,
|
|
|
|
type: value.manifest.type,
|
2024-02-13 13:37:58 -08:00
|
|
|
affiliations: value.manifest.authors // pull the affiliation metadata from the author data
|
|
|
|
.map((authorId) => authors[authorId])
|
|
|
|
.map((author) => author.affiliation)
|
|
|
|
.map((affiliationId) =>
|
|
|
|
affiliationId
|
|
|
|
.map(
|
|
|
|
(af) =>
|
|
|
|
`${affiliations[af.split('@')[1]].name} ${affiliations[af.split('@')[1]].short}`
|
|
|
|
)
|
|
|
|
.join(' ')
|
|
|
|
)
|
|
|
|
.join(' '),
|
|
|
|
key: key,
|
2024-02-15 15:11:34 -08:00
|
|
|
citation: value.citation ? value.citation : hash(key),
|
|
|
|
doi: value.doi ? value.doi : undefined,
|
2024-02-12 21:21:55 -08:00
|
|
|
}))
|
|
|
|
|
|
|
|
const miniSearch = new MiniSearch({
|
2024-02-13 13:37:58 -08:00
|
|
|
fields: [
|
|
|
|
'abstract',
|
|
|
|
'keywords',
|
|
|
|
'topics',
|
|
|
|
'authors',
|
|
|
|
'title',
|
|
|
|
'type',
|
|
|
|
'affiliations',
|
2024-02-15 15:11:34 -08:00
|
|
|
'citation',
|
|
|
|
'doi',
|
2024-02-13 13:37:58 -08:00
|
|
|
],
|
2024-02-12 21:21:55 -08:00
|
|
|
storeFields: ['key', 'abstract', 'manifest'],
|
|
|
|
searchOptions: {
|
|
|
|
boost: {
|
2024-02-13 13:37:58 -08:00
|
|
|
title: 3,
|
2024-02-12 21:21:55 -08:00
|
|
|
keywords: 2,
|
|
|
|
topics: 1,
|
|
|
|
authors: 2,
|
2024-02-13 13:37:58 -08:00
|
|
|
abstract: 0.3,
|
2024-02-12 21:21:55 -08:00
|
|
|
},
|
2024-02-13 22:36:13 -08:00
|
|
|
fuzzy: 0.2,
|
2024-02-12 21:21:55 -08:00
|
|
|
prefix: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
miniSearch.addAll(docs)
|
|
|
|
|
|
|
|
onmessage = (e: MessageEvent<string>) => {
|
|
|
|
postMessage(miniSearch.search(e.data) as CustomSearchResult[])
|
|
|
|
}
|