@@ -8,8 +8,28 @@ import { getBuckets, clone } from './helpers.js';
88export function search ( items , input , configuration , fulltext , facets ) {
99 input = input || Object . create ( null ) ;
1010
11- const per_page = parseInt ( input . per_page || 12 ) ;
12- const page = parseInt ( input . page || 1 ) ;
11+ const normalizeNumber = ( value ) => {
12+ if ( typeof value === 'number' ) {
13+ return value ;
14+ }
15+ const parsed = parseInt ( value , 10 ) ;
16+ return parsed ;
17+ } ;
18+
19+ let per_page = normalizeNumber ( input . per_page ) ;
20+ if ( ! Number . isFinite ( per_page ) || per_page < 0 ) {
21+ per_page = 12 ;
22+ }
23+
24+ let page = normalizeNumber ( input . page ) ;
25+ if ( ! Number . isFinite ( page ) || page < 1 ) {
26+ page = 1 ;
27+ }
28+
29+ // Allow per_page to be zero to support queries that only need aggregations
30+ if ( per_page === 0 ) {
31+ page = 1 ;
32+ }
1333 const is_all_filtered_items = input . is_all_filtered_items || false ;
1434
1535 if ( configuration . native_search_enabled === false && input . query ) {
@@ -178,6 +198,7 @@ export function sorted_items(items, sort, sortings) {
178198 * useful for autocomplete or list all aggregation options
179199 */
180200export function similar ( items , id , options ) {
201+ options = options || Object . create ( null ) ;
181202 const per_page = options . per_page || 10 ;
182203 const minimum = options . minimum || 0 ;
183204 const page = options . page || 1 ;
@@ -191,6 +212,19 @@ export function similar(items, id, options) {
191212 }
192213 }
193214
215+ if ( ! item ) {
216+ return {
217+ pagination : {
218+ per_page : per_page ,
219+ page : page ,
220+ total : 0 ,
221+ } ,
222+ data : {
223+ items : [ ] ,
224+ } ,
225+ } ;
226+ }
227+
194228 if ( ! options . field ) {
195229 throw new Error ( 'Please define field in options' ) ;
196230 }
@@ -203,9 +237,10 @@ export function similar(items, id, options) {
203237 const intersection = _intersection ( item [ field ] , items [ i ] [ field ] ) ;
204238
205239 if ( intersection . length >= minimum ) {
206- sorted_items . push ( items [ i ] ) ;
207- sorted_items [ sorted_items . length - 1 ] . intersection_length =
208- intersection . length ;
240+ sorted_items . push ( {
241+ ...items [ i ] ,
242+ intersection_length : intersection . length ,
243+ } ) ;
209244 }
210245 }
211246 }
@@ -250,9 +285,18 @@ export function aggregation(items, input, configuration, fulltext, facets) {
250285 throw new Error ( 'field name is required' ) ;
251286 }
252287
253- configuration . aggregations [ input . name ] . size = 10000 ;
288+ const aggregationConfig = {
289+ ...configuration ,
290+ aggregations : {
291+ ...configuration . aggregations ,
292+ [ input . name ] : {
293+ ...configuration . aggregations [ input . name ] ,
294+ size : 10000 ,
295+ } ,
296+ } ,
297+ } ;
254298
255- const result = search ( items , search_input , configuration , fulltext , facets ) ;
299+ const result = search ( items , search_input , aggregationConfig , fulltext , facets ) ;
256300 const buckets = result . data . aggregations [ input . name ] . buckets ;
257301
258302 return {
0 commit comments