-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathhandleSearch.ts
144 lines (137 loc) · 4.6 KB
/
handleSearch.ts
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
// @ts-ignore
import algoliasearch from 'algoliasearch/dist/algoliasearch-lite.esm.browser';
import type { SearchClient } from 'algoliasearch/lite';
import { preResolve, postResolve, resolve } from '@algolia/autocomplete-core/dist/esm/resolve';
import { reshape } from '@algolia/autocomplete-core/dist/esm/reshape';
import type { DocSearchHit } from './types';
import { groupBy, removeHighlightTags } from './utils';
import { version } from './version';
let client: SearchClient;
export function handleSearch(
query: string,
{ state, appId, apiKey, indexName, snippetLength, transformItems }: any
) {
if (!client) {
client = algoliasearch(appId, apiKey);
client.addAlgoliaAgent('docsearch', version);
}
let q = Promise.resolve([] as any[]);
if (query) {
q = client
.search<DocSearchHit>([
{
query,
indexName,
params: {
attributesToRetrieve: [
'hierarchy.lvl0',
'hierarchy.lvl1',
'hierarchy.lvl2',
'hierarchy.lvl3',
'hierarchy.lvl4',
'hierarchy.lvl5',
'hierarchy.lvl6',
'content',
'type',
'url',
],
attributesToSnippet: [
`hierarchy.lvl1:${snippetLength}`,
`hierarchy.lvl2:${snippetLength}`,
`hierarchy.lvl3:${snippetLength}`,
`hierarchy.lvl4:${snippetLength}`,
`hierarchy.lvl5:${snippetLength}`,
`hierarchy.lvl6:${snippetLength}`,
`content:${snippetLength}`,
],
snippetEllipsisText: '…',
highlightPreTag: '<mark>',
highlightPostTag: '</mark>',
hitsPerPage: 20,
},
},
])
.then(({ results }) => {
const { hits, nbHits } = results[0];
const rawSources = groupBy(hits, (hit) => removeHighlightTags(hit));
// We store the `lvl0`s to display them as search suggestions
// in the "no results" screen.
if ((state.context.searchSuggestions as any[]).length < Object.keys(rawSources).length) {
state.context.searchSuggestions = Object.keys(rawSources);
}
state.context.nbHits = nbHits;
return Object.values<DocSearchHit[]>(rawSources).map((items, index) => {
return {
sourceId: `hits${index}`,
items,
getItemUrl: ({ item }: any) => item.url,
getItemInputValue: () => {},
onSelect: () => {},
getItems() {
return Promise.all(
Object.values(groupBy(items, (item) => item.hierarchy.lvl1)).map((x) => {
if (transformItems) {
return transformItems(x);
}
return x;
})
).then((resp) => {
return resp
.map((groupedHits) =>
groupedHits.map((item: any) => {
return {
...item,
__docsearch_parent:
item.type !== 'lvl1' &&
groupedHits.find(
(siblingItem: any) =>
siblingItem.type === 'lvl1' &&
siblingItem.hierarchy.lvl1 === item.hierarchy.lvl1
),
};
})
)
.flat();
});
},
};
});
});
}
return q
.then((sources) => {
return Promise.all(
sources.map(async (source) => {
return Promise.resolve(source.getItems()).then((itemsOrDescription) =>
preResolve<any>(itemsOrDescription, source.sourceId)
);
})
)
.then(resolve)
.then((responses) => postResolve(responses, sources as any))
.then((collections) => {
return reshape({
collections,
props: {
reshape: ({ sources }: any) => sources,
} as any,
state: {} as any,
});
});
})
.then((collections) => {
let baseItemId = 0;
const value = collections.map<any>((collection) => ({
...collection,
// We flatten the stored items to support calling `getAlgoliaResults`
// from the source itself.
items: (collection.items as any[]).flat(Infinity).map((item: any) => ({
...item,
__autocomplete_id: baseItemId++,
})),
}));
return {
collections: value,
};
});
}