11import MeiliSearch from 'meilisearch'
2- import { isString , removeUndefinedFromObject } from './utils.js'
2+ import { removeUndefinedFromObject } from './utils.js'
3+ import { createHighlighResult , createSnippetResult } from './format.js'
34
45export default function instantMeiliSearch ( hostUrl , apiKey , options = { } ) {
56 return {
@@ -12,38 +13,18 @@ export default function instantMeiliSearch(hostUrl, apiKey, options = {}) {
1213 const limit = this . pagination // if pagination widget is set, use paginationTotalHits as limit
1314 ? this . paginationTotalHits
1415 : this . hitsPerPage
16+ const { query, facets, facetFilters, attributesToSnippet } = params
1517 const searchInput = {
16- q : this . placeholderSearch && params . query === '' ? null : params . query ,
17- facetsDistribution : params . facets . length ? params . facets : undefined ,
18- facetFilters : params . facetFilters ,
18+ q : this . placeholderSearch && query === '' ? null : query ,
19+ facetsDistribution : facets . length ? facets : undefined ,
20+ facetFilters : facetFilters ,
1921 attributesToHighlight : this . attributesToHighlight ,
22+ attributesToCrop : attributesToSnippet ,
2023 limit,
2124 }
2225 return removeUndefinedFromObject ( searchInput )
2326 } ,
2427
25- replaceHighlightTags : function (
26- formattedHit ,
27- highlightPreTag ,
28- highlightPostTag
29- ) {
30- // formattedHit is the `_formatted` object returned by MeiliSearch.
31- // It contains all the highlighted attributes
32- return Object . keys ( formattedHit ) . reduce ( ( result , key ) => {
33- let newHighlightString = formattedHit [ key ] || ''
34- // If the value of the attribute is a string,
35- // the highlight is applied by MeiliSearch (<em> tags)
36- // and we replace the <em> by the expected tag for InstantSearch
37- if ( isString ( formattedHit [ key ] ) ) {
38- newHighlightString = formattedHit [ key ]
39- . replace ( / < e m > / g, highlightPreTag )
40- . replace ( / < \/ e m > / g, highlightPostTag )
41- }
42- result [ key ] = { value : newHighlightString . toString ( ) }
43- return result
44- } , { } )
45- } ,
46-
4728 parseHits : function ( meiliSearchHits , params ) {
4829 if ( this . pagination ) {
4930 const start = params . page * this . hitsPerPage
@@ -55,11 +36,8 @@ export default function instantMeiliSearch(hostUrl, apiKey, options = {}) {
5536 delete hit . _formatted
5637 return {
5738 ...hit ,
58- _highlightResult : this . replaceHighlightTags (
59- formattedHit ,
60- params . highlightPreTag ,
61- params . highlightPostTag
62- ) ,
39+ _highlightResult : createHighlighResult ( { formattedHit, ...params } ) ,
40+ _snippetResult : createSnippetResult ( { formattedHit, ...params } ) ,
6341 }
6442 } )
6543 } ,
@@ -108,7 +86,7 @@ export default function instantMeiliSearch(hostUrl, apiKey, options = {}) {
10886 // Params got from InstantSearch
10987 const params = requests [ 0 ] . params
11088 this . pagination = params . page !== undefined // If the pagination widget has been set
111- this . hitsPerPage = params . hitsPerPage || 20 // 20 is the MeiliSearch's default limit value. It can be changed with `InsantSearch.configure`.
89+ this . hitsPerPage = params . hitsPerPage || 20 // 20 is the MeiliSearch's default limit value. `hitsPerPage` can be changed with `InsantSearch.configure`.
11290 // Gets information from IS and transforms it for MeiliSearch
11391 const searchInput = this . transformToMeiliSearchParams ( params )
11492 const indexUid = requests [ 0 ] . indexName
0 commit comments