-
Notifications
You must be signed in to change notification settings - Fork 9
/
algolia.js
182 lines (173 loc) · 5.35 KB
/
algolia.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* eslint-disable no-alert, no-console */
import algoliasearch from 'algoliasearch'
// export `createAlgoliaClient` to use it in page components
export class AlgoliaClient {
constructor(algoliaId, algoliaKey, PENNSIEVE_API_LOCATION = 'https://api.pennsieve.io') {
this.client = algoliasearch(
algoliaId,
algoliaKey
)
this.PENNSIEVE_API_LOCATION = PENNSIEVE_API_LOCATION
}
initIndex(ALGOLIA_INDEX) {
this.index = this.client.initIndex(ALGOLIA_INDEX);
}
getAlgoliaFacets(propPathMapping) {
const map = new Map(Object.entries(propPathMapping));
const facetPropPaths = Array.from(map.keys());
let facetData = []
let facetId = 0
return this.index
.search('', {
sortFacetValuesBy: 'alpha',
facets: facetPropPaths
})
.then(response => {
facetPropPaths.map((facetPropPath) => {
var children = []
const responseFacets = response.facets
if (responseFacets === undefined) { return }
const responseFacetChildren =
responseFacets[facetPropPath] == undefined
? {}
: responseFacets[facetPropPath]
Object.keys(responseFacetChildren).map(facet => {
children.push({
label: facet,
id: facetId++,
facetPropPath: facetPropPath
})
})
if (children.length > 0) {
facetData.push({
label: map.get(facetPropPath),
id: facetId++,
children: children,
key: facetPropPath
})
}
})
return facetData
})
}
// Returns all DOIs of all versions for a given discover dataset
_discoverAllDois(discoverId, PENNSIEVE_API_LOCATION = 'https://api.pennsieve.io') {
return new Promise(resolve => {
fetch(`${PENNSIEVE_API_LOCATION}/discover/datasets/${discoverId}/versions`).then(r => r.json()).then(dataset => {
resolve(dataset.map(version => version.doi))
})
})
}
// Get all dois given a list of discoverIds
_expandDois(discoverIds, PENNSIEVE_API_LOCATION = 'https://api.pennsieve.io') {
return new Promise(resolve => {
let promiseList = discoverIds.map(discoverId => this._discoverAllDois(discoverId, PENNSIEVE_API_LOCATION))
Promise.all(promiseList).then((values) => {
resolve(values.flat())
});
})
}
_processResultsForCards(results) {
let newResults = []
let newResult = {}
for (let res of results) {
newResult = { ...res }
newResult = {
doi: res.item.curie.split(':')[1],
name: res.item.name,
description: res.item.description,
updated: res.pennsieve.updatedAt,
publishDate: res.pennsieve.publishDate,
datasetId: res.objectID,
detailsReady: false
}
newResults.push(newResult)
}
return newResults
}
_processAnatomy(hits) {
let foundKeyWords = []
let uniqueKeywords = []
hits.forEach(hit => {
if (hit.item && hit.item.keywords) {
hit.item.keywords.forEach(keywordObj => {
let keyword = keywordObj.keyword.toUpperCase()
if (keyword.includes('UBERON') || keyword.includes('ILX')) {
foundKeyWords.push(this._processUberonURL(keyword))
}
})
}
if (hit.anatomy && hit.anatomy.organ ) {
hit.anatomy.organ.forEach(anatomy => {
if (anatomy.curie) {
foundKeyWords.push(anatomy.curie)
}
})
}
})
uniqueKeywords = [...new Set(foundKeyWords)]
return uniqueKeywords
}
_processUberonURL(url) {
let ub = url.split('/').pop()
return ub.replace('_', ':')
}
/**
* Get Search results
* This is using fetch from the Algolia API
*/
search(filter, query = '', hitsperPage = 10, page = 1) {
return new Promise(resolve => {
this.index
.search(query, {
facets: ['*'],
hitsPerPage: hitsperPage,
page: page - 1,
filters: filter,
attributesToHighlight: [],
attributesToRetrieve: [
'pennsieve.publishDate',
'pennsieve.updatedAt',
'item.curie',
'item.name',
'item.description',
'objectID',
],
})
.then(response => {
let searchData = {
items: this._processResultsForCards(response.hits),
total: response.nbHits,
discoverIds: response.hits.map(r => r.pennsieve.identifier),
dois: response.hits.map(r => r.item.curie.split(':')[1])
}
resolve(searchData)
})
})
}
/**
* Get key words
* This is used to return all keywords for a given search. Note that you often want the hits per page to be maxed out
*/
anatomyInSearch(filter, query = '', hitsperPage = 999999, page = 1) {
return new Promise(resolve => {
this.index
.search(query, {
facets: ['*'],
hitsPerPage: hitsperPage,
page: page - 1,
filters: filter,
attributesToHighlight: [],
attributesToRetrieve: [
'item.keywords.keyword',
'anatomy.organ.name',
'anatomy.organ.curie'
],
})
.then(response => {
let anatomyAsUberons = this._processAnatomy(response.hits)
resolve(anatomyAsUberons)
})
})
}
}