@@ -6,6 +6,92 @@ import {
66} from './geo-rules-adapter'
77import { adaptFilters } from './filter-adapter'
88
9+ function ParamsBuilder ( searchContext : SearchContext ) {
10+ const meiliSearchParams : Record < string , any > = { }
11+
12+ return {
13+ getParams ( ) {
14+ return meiliSearchParams
15+ } ,
16+ addFacets ( ) {
17+ const facets = searchContext ?. facets
18+ if ( facets ?. length ) {
19+ meiliSearchParams . facetsDistribution = facets
20+ }
21+ } ,
22+ addAttributesToCrop ( ) {
23+ const attributesToCrop = searchContext ?. attributesToSnippet
24+ if ( attributesToCrop ) {
25+ meiliSearchParams . attributesToCrop = attributesToCrop
26+ }
27+ } ,
28+ addAttributesToRetrieve ( ) {
29+ const attributesToRetrieve = searchContext ?. attributesToRetrieve
30+ if ( attributesToRetrieve ) {
31+ meiliSearchParams . attributesToRetrieve = attributesToRetrieve
32+ }
33+ } ,
34+ addFilters ( ) {
35+ const filter = adaptFilters (
36+ searchContext ?. filters ,
37+ searchContext ?. numericFilters ,
38+ searchContext ?. facetFilters
39+ )
40+ if ( filter . length ) {
41+ meiliSearchParams . filter = filter
42+ }
43+ } ,
44+ addAttributesToHighlight ( ) {
45+ meiliSearchParams . attributesToHighlight = searchContext ?. attributesToHighlight || [
46+ '*' ,
47+ ]
48+ } ,
49+ addLimit ( ) {
50+ const placeholderSearch = searchContext . placeholderSearch
51+ const query = searchContext . query
52+ const { pagination } = searchContext
53+
54+ // Limit based on pagination preferences
55+ if (
56+ ( ! placeholderSearch && query === '' ) ||
57+ pagination . paginationTotalHits === 0
58+ ) {
59+ meiliSearchParams . limit = 0
60+ } else if ( searchContext . finitePagination ) {
61+ meiliSearchParams . limit = pagination . paginationTotalHits
62+ } else {
63+ const limit = ( pagination . page + 1 ) * pagination . hitsPerPage + 1
64+ // If the limit is bigger than the total hits accepted
65+ // force the limit to that amount
66+ if ( limit > pagination . paginationTotalHits ) {
67+ meiliSearchParams . limit = pagination . paginationTotalHits
68+ } else {
69+ meiliSearchParams . limit = limit
70+ }
71+ }
72+ } ,
73+ addSort ( ) {
74+ const sort = searchContext . sort
75+
76+ if ( sort ?. length ) {
77+ meiliSearchParams . sort = [ sort ]
78+ }
79+ } ,
80+ addGeoSearchRules ( ) {
81+ const geoSearchContext = createGeoSearchContext ( searchContext )
82+ const geoRules = adaptGeoPointsRules ( geoSearchContext )
83+
84+ if ( geoRules ?. filter ) {
85+ if ( meiliSearchParams . filter ) {
86+ meiliSearchParams . filter . unshift ( geoRules . filter )
87+ } else {
88+ meiliSearchParams . filter = [ geoRules . filter ]
89+ }
90+ }
91+ } ,
92+ }
93+ }
94+
995/**
1096 * Adapt search request from instantsearch.js
1197 * to search request compliant with Meilisearch
@@ -16,111 +102,15 @@ import { adaptFilters } from './filter-adapter'
16102export function adaptSearchParams (
17103 searchContext : SearchContext
18104) : MeiliSearchParams {
19- // Creates search params object compliant with Meilisearch
20- const meiliSearchParams : Record < string , any > = { }
21-
22- // Facets
23- const facets = searchContext ?. facets
24- if ( facets ?. length ) {
25- meiliSearchParams . facets = facets
26- }
27-
28- // Attributes To Crop
29- const attributesToCrop = searchContext ?. attributesToSnippet
30- if ( attributesToCrop ) {
31- meiliSearchParams . attributesToCrop = attributesToCrop
32- }
33-
34- // Attributes To Crop marker
35- const cropMarker = searchContext ?. snippetEllipsisText
36- if ( cropMarker != null ) {
37- meiliSearchParams . cropMarker = cropMarker
38- }
39-
40- // Attributes To Retrieve
41- const attributesToRetrieve = searchContext ?. attributesToRetrieve
42- if ( attributesToRetrieve ) {
43- meiliSearchParams . attributesToRetrieve = attributesToRetrieve
44- }
45-
46- // Filter
47- const filter = adaptFilters (
48- searchContext ?. filters ,
49- searchContext ?. numericFilters ,
50- searchContext ?. facetFilters
51- )
52- if ( filter . length ) {
53- meiliSearchParams . filter = filter
54- }
55-
56- // Attributes To Retrieve
57- if ( attributesToRetrieve ) {
58- meiliSearchParams . attributesToCrop = attributesToRetrieve
59- }
60-
61- // Attributes To Highlight
62- meiliSearchParams . attributesToHighlight = searchContext ?. attributesToHighlight || [
63- '*' ,
64- ]
65-
66- // Highlight pre tag
67- const highlightPreTag = searchContext ?. highlightPreTag
68- if ( highlightPreTag ) {
69- meiliSearchParams . highlightPreTag = highlightPreTag
70- } else {
71- meiliSearchParams . highlightPreTag = '__ais-highlight__'
72- }
73-
74- // Highlight post tag
75- const highlightPostTag = searchContext ?. highlightPostTag
76- if ( highlightPostTag ) {
77- meiliSearchParams . highlightPostTag = highlightPostTag
78- } else {
79- meiliSearchParams . highlightPostTag = '__/ais-highlight__'
80- }
81-
82- const placeholderSearch = searchContext . placeholderSearch
83- const query = searchContext . query
84-
85- // Pagination
86- const { pagination } = searchContext
87-
88- // Limit based on pagination preferences
89- if (
90- ( ! placeholderSearch && query === '' ) ||
91- pagination . paginationTotalHits === 0
92- ) {
93- meiliSearchParams . limit = 0
94- } else if ( searchContext . finitePagination ) {
95- meiliSearchParams . limit = pagination . paginationTotalHits
96- } else {
97- const limit = ( pagination . page + 1 ) * pagination . hitsPerPage + 1
98- // If the limit is bigger than the total hits accepted
99- // force the limit to that amount
100- if ( limit > pagination . paginationTotalHits ) {
101- meiliSearchParams . limit = pagination . paginationTotalHits
102- } else {
103- meiliSearchParams . limit = limit
104- }
105- }
106-
107- const sort = searchContext . sort
108-
109- // Sort
110- if ( sort ?. length ) {
111- meiliSearchParams . sort = [ sort ]
112- }
113-
114- const geoSearchContext = createGeoSearchContext ( searchContext )
115- const geoRules = adaptGeoPointsRules ( geoSearchContext )
116-
117- if ( geoRules ?. filter ) {
118- if ( meiliSearchParams . filter ) {
119- meiliSearchParams . filter . unshift ( geoRules . filter )
120- } else {
121- meiliSearchParams . filter = [ geoRules . filter ]
122- }
123- }
124-
125- return meiliSearchParams
105+ const buildParams = ParamsBuilder ( searchContext )
106+ buildParams . addFacets ( )
107+ buildParams . addFilters ( )
108+ buildParams . addLimit ( )
109+ buildParams . addSort ( )
110+ buildParams . addGeoSearchRules ( )
111+ buildParams . addAttributesToCrop ( )
112+ buildParams . addAttributesToHighlight ( )
113+ buildParams . addAttributesToRetrieve ( )
114+
115+ return buildParams . getParams ( )
126116}
0 commit comments