forked from metaspace2020/sm-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasetFilters.js
145 lines (121 loc) · 3.84 KB
/
datasetFilters.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
const capitalize = require('lodash/capitalize');
function getPgField(schemaPath) {
const pathElements = schemaPath.replace(/\./g, ',');
return "metadata#>>'{" + pathElements + "}'";
}
class AbstractDatasetFilter {
constructor(schemaPath, options) {
this.schemaPath = schemaPath;
this.options = options;
this.esField = options.esField || ('ds_meta.' + this.schemaPath);
this.pgField = options.pgField || getPgField(this.schemaPath);
}
preprocess(val) {
if (this.options.preprocess)
return this.options.preprocess(val);
return val;
}
esFilter(value) {}
pgFilter(q, value) {}
}
class ExactMatchFilter extends AbstractDatasetFilter {
constructor(schemaPath, options) {
super(schemaPath, options);
}
esFilter(value) {
return {term: {[this.esField]: this.preprocess(value)}}
}
pgFilter(q, value) {
return q.whereRaw(this.pgField + ' = ?', [this.preprocess(value)]);
}
}
class SubstringMatchFilter extends AbstractDatasetFilter {
constructor(schemaPath, options) {
super(schemaPath, options);
}
esFilter(value) {
return {wildcard: {[this.esField]: `*${this.preprocess(value)}*`}}
}
pgFilter(q, value) {
return q.whereRaw(this.pgField + ' ILIKE ?', ['%' + this.preprocess(value) + '%']);
}
}
class PhraseMatchFilter extends SubstringMatchFilter {
constructor(schemaPath, options) {
super(schemaPath, options);
}
esFilter(value) {
return {match: {[this.esField]: {query: this.preprocess(value), type: 'phrase'}}}
}
}
class DatasetIdFilter extends AbstractDatasetFilter {
constructor() {
super('', {});
}
esFilter(ids) {
// FIXME: array filter doesn't work presumably because of bugs in apollo-server
ids = ids.split("|");
if (ids.length > 0)
return {terms: {ds_id: ids}};
else
return {};
}
pgFilter(q, ids) {
ids = ids.split("|");
return q.whereIn('id', ids);
}
}
class PersonFilter extends AbstractDatasetFilter {
constructor(schemaPath) {
super(schemaPath, {});
this.pgNameField = getPgField(schemaPath + '.First_Name');
this.pgSurnameField = getPgField(schemaPath + '.Surname');
}
esFilter({name, surname}) {
return [
// TODO: make these not_analyzed
{term: {[this.esField + '.First_Name']: name}},
{term: {[this.esField + '.Surname']: surname}}
];
}
pgFilter(q, {name, surname}) {
return q.whereRaw(`${this.pgNameField} = ? AND ${this.pgSurnameField} = ?`,
[name, surname]);
}
}
const datasetFilters = {
institution: new ExactMatchFilter('Submitted_By.Institution', {}),
polarity: new PhraseMatchFilter('MS_Analysis.Polarity', {preprocess: capitalize}),
ionisationSource: new ExactMatchFilter('MS_Analysis.Ionisation_Source', {}),
analyzerType: new PhraseMatchFilter('MS_Analysis.Analyzer', {}),
organism: new ExactMatchFilter('Sample_Information.Organism', {}),
organismPart: new ExactMatchFilter('Sample_Information.Organism_Part', {}),
condition: new ExactMatchFilter('Sample_Information.Condition', {}),
growthConditions: new ExactMatchFilter('Sample_Information.Sample_Growth_Conditions', {}),
maldiMatrix: new ExactMatchFilter('Sample_Preparation.MALDI_Matrix', {}),
name: new SubstringMatchFilter('', {esField: 'ds_name', pgField: 'name'}),
ids: new DatasetIdFilter(),
status: new ExactMatchFilter('', {esField: 'ds_status', pgField: 'status'}),
submitter: new PersonFilter('Submitted_By.Submitter'),
metadataType: new ExactMatchFilter('Data_Type', {}),
}
function dsField(hit, alias){
let info = hit._source.ds_meta;
for (let field of datasetFilters[alias].schemaPath.split(".")) {
info = info[field];
if (!info)
return info;
}
return info;
}
module.exports = {
AbstractDatasetFilter,
ExactMatchFilter,
PhraseMatchFilter,
SubstringMatchFilter,
DatasetIdFilter,
PersonFilter,
datasetFilters,
dsField,
getPgField
};