-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathsearch.js
225 lines (203 loc) · 7.5 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import fuzzy from 'fuzzy';
import { currentBackend } from 'Backends/backend';
import { getIntegrationProvider } from 'Integrations';
import { selectIntegration, selectEntries } from 'Reducers';
import { selectInferedField } from 'Reducers/collections';
import { WAIT_UNTIL_ACTION } from 'Redux/middleware/waitUntilAction';
import { loadEntries, ENTRIES_SUCCESS } from './entries';
/*
* Contant Declarations
*/
export const SEARCH_ENTRIES_REQUEST = 'SEARCH_ENTRIES_REQUEST';
export const SEARCH_ENTRIES_SUCCESS = 'SEARCH_ENTRIES_SUCCESS';
export const SEARCH_ENTRIES_FAILURE = 'SEARCH_ENTRIES_FAILURE';
export const QUERY_REQUEST = 'INIT_QUERY';
export const QUERY_SUCCESS = 'QUERY_OK';
export const QUERY_FAILURE = 'QUERY_ERROR';
export const SEARCH_CLEAR = 'SEARCH_CLEAR';
/*
* Simple Action Creators (Internal)
* We still need to export them for tests
*/
export function searchingEntries(searchTerm) {
return {
type: SEARCH_ENTRIES_REQUEST,
payload: { searchTerm },
};
}
export function searchSuccess(searchTerm, entries, page) {
return {
type: SEARCH_ENTRIES_SUCCESS,
payload: {
searchTerm,
entries,
page,
},
};
}
export function searchFailure(searchTerm, error) {
return {
type: SEARCH_ENTRIES_FAILURE,
payload: {
searchTerm,
error,
},
};
}
export function querying(namespace, collection, searchFields, searchTerm) {
return {
type: QUERY_REQUEST,
payload: {
namespace,
collection,
searchFields,
searchTerm,
},
};
}
export function querySuccess(namespace, collection, searchFields, searchTerm, response) {
return {
type: QUERY_SUCCESS,
payload: {
namespace,
collection,
searchFields,
searchTerm,
response,
},
};
}
export function queryFailure(namespace, collection, searchFields, searchTerm, error) {
return {
type: QUERY_SUCCESS,
payload: {
namespace,
collection,
searchFields,
searchTerm,
error,
},
};
}
/*
* Exported simple Action Creators
*/
export function clearSearch() {
return { type: SEARCH_CLEAR };
}
/*
* Exported Thunk Action Creators
*/
// SearchEntries will search for complete entries in all collections.
export function searchEntries(searchTerm, page = 0) {
return (dispatch, getState) => {
const state = getState();
const allCollections = state.collections.keySeq().toArray();
const collections = allCollections.filter(collection => selectIntegration(state, collection, 'search'));
const integration = selectIntegration(state, collections[0], 'search');
if (!integration) {
localSearch(searchTerm, getState, dispatch);
} else {
const provider = getIntegrationProvider(state.integrations, currentBackend(state.config).getToken, integration);
dispatch(searchingEntries(searchTerm));
provider.search(collections, searchTerm, page).then(
response => dispatch(searchSuccess(searchTerm, response.entries, response.pagination)),
error => dispatch(searchFailure(searchTerm, error))
);
}
};
}
// Instead of searching for complete entries, query will search for specific fields
// in specific collections and return raw data (no entries).
export function query(namespace, collection, searchFields, searchTerm) {
return (dispatch, getState) => {
const state = getState();
const integration = selectIntegration(state, collection, 'search');
dispatch(querying(namespace, collection, searchFields, searchTerm));
if (!integration) {
localQuery(namespace, collection, searchFields, searchTerm, state, dispatch);
} else {
const provider = getIntegrationProvider(state.integrations, currentBackend(state.config).getToken, integration);
provider.searchBy(searchFields.map(f => `data.${ f }`), collection, searchTerm).then(
response => dispatch(querySuccess(namespace, collection, searchFields, searchTerm, response)),
error => dispatch(queryFailure(namespace, collection, searchFields, searchTerm, error))
);
}
};
}
// Local Query & Search functions
function localSearch(searchTerm, getState, dispatch) {
return (function acc(localResults = { entries: [] }) {
function processCollection(collection, collectionKey) {
const state = getState();
if (state.entries.hasIn(['pages', collectionKey, 'ids'])) {
const searchFields = [
selectInferedField(collection, 'title'),
selectInferedField(collection, 'shortTitle'),
selectInferedField(collection, 'author'),
];
const collectionEntries = selectEntries(state, collectionKey).toJS();
const filteredEntries = fuzzy.filter(searchTerm, collectionEntries, {
extract: entry => searchFields.reduce((acc, field) => {
const f = entry.data[field];
return f ? `${ acc } ${ f }` : acc;
}, ""),
}).filter(entry => entry.score > 5);
localResults[collectionKey] = true;
localResults.entries = localResults.entries.concat(filteredEntries);
const returnedKeys = Object.keys(localResults);
const allCollections = state.collections.keySeq().toArray();
if (allCollections.every(v => returnedKeys.indexOf(v) !== -1)) {
const sortedResults = localResults.entries.sort((a, b) => {
if (a.score > b.score) return -1;
if (a.score < b.score) return 1;
return 0;
}).map(f => f.original);
if (allCollections.size > 3 || localResults.entries.length > 30) {
console.warn('The Netlify CMS is currently using a Built-in search.' +
'\nWhile this works great for small sites, bigger projects might benefit from a separate search integration.' +
'\nPlease refer to the documentation for more information');
}
dispatch(searchSuccess(searchTerm, sortedResults, 0));
}
} else {
// Collection entries aren't loaded yet.
// Dispatch loadEntries and wait before redispatching this action again.
dispatch({
type: WAIT_UNTIL_ACTION,
predicate: action => (action.type === ENTRIES_SUCCESS && action.payload.collection === collectionKey),
run: () => processCollection(collection, collectionKey),
});
dispatch(loadEntries(collection));
}
}
getState().collections.forEach(processCollection);
}());
}
function localQuery(namespace, collection, searchFields, searchTerm, state, dispatch) {
// Check if entries in this collection were already loaded
if (state.entries.hasIn(['pages', collection, 'ids'])) {
const entries = selectEntries(state, collection).toJS();
const filteredEntries = fuzzy.filter(searchTerm, entries, {
extract: entry => searchFields.reduce((acc, field) => {
const f = entry.data[field];
return f ? `${ acc } ${ f }` : acc;
}, ""),
}).filter(entry => entry.score > 5);
const resultObj = {
query: searchTerm,
hits: [],
};
resultObj.hits = filteredEntries.map(f => f.original);
dispatch(querySuccess(namespace, collection, searchFields, searchTerm, resultObj));
} else {
// Collection entries aren't loaded yet.
// Dispatch loadEntries and wait before redispatching this action again.
dispatch({
type: WAIT_UNTIL_ACTION,
predicate: action => (action.type === ENTRIES_SUCCESS && action.payload.collection === collection),
run: dispatch => dispatch(query(namespace, collection, searchFields, searchTerm)),
});
dispatch(loadEntries(state.collections.get(collection)));
}
}