-
Notifications
You must be signed in to change notification settings - Fork 5
/
search.js
65 lines (61 loc) 路 1.59 KB
/
search.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
// @flow
import searchQueryParser from 'search-query-parser';
import { getMultiContent } from './multi-content';
export type StructuredSearchQuery = {|
types: string[],
type: string[],
ids: string[],
id: string[],
tags: string[],
tag: string[],
pageSize: number,
orderings: string[],
// content type specific
'article-series': string[],
|};
export async function search(req: ?Request, stringQuery: string) {
const query = parseQuery(stringQuery);
return getMultiContent(req, query);
}
// TODO:
// * free text search
// * excludes
export function parseQuery(query: string): StructuredSearchQuery {
const structuredQuery = searchQueryParser.parse(query, {
keywords: [
'types',
'type',
'ids',
'id',
'tags',
'tag',
'pageSize',
'orderings',
// content type specific
'article-series',
],
});
const arrayedStructuredQuery = Object.keys(structuredQuery).reduce(
(acc, key) => {
const value = structuredQuery[key];
if (typeof value === 'string') {
acc[key] = [value];
} else {
acc[key] = value;
}
return acc;
},
{}
);
return {
types: arrayedStructuredQuery.types || [],
type: arrayedStructuredQuery.type || [],
ids: arrayedStructuredQuery.ids || [],
id: arrayedStructuredQuery.id || [],
tags: arrayedStructuredQuery.tags || [],
tag: arrayedStructuredQuery.tag || [],
orderings: arrayedStructuredQuery.orderings || [],
pageSize: arrayedStructuredQuery.pageSize,
'article-series': arrayedStructuredQuery['article-series'] || [],
};
}