typesense-instantsearch-adapter 2.8.0-3 → 2.8.0-5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -448,7 +448,7 @@ Read more about all available options for `renderingContent` in [Algolia's docum
448
448
 
449
449
  > Available as of typesense-instantsearch-adapter `2.8.0-1` and Typesense Server `v0.26.0.rc25`
450
450
 
451
- The `facet_by` parameter is managed by InstantSearch internally when you use the various filter widget.
451
+ The `facet_by` parameter is managed by InstantSearch internally when you use the various filter widgets.
452
452
 
453
453
  But if you need to pass custom options to the `facet_by` parameter (eg: server-side sort options), then you can use the `facetByOptions` parameter as shown below:
454
454
 
@@ -469,11 +469,51 @@ const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
469
469
  brand: "(sort_by: _alpha:asc)",
470
470
  category: "(sort_by: _alpha:desc)",
471
471
  }, // <======= Add any facet_by parameter as a key value pair. Don't forget the surrounding parantheses in the value.
472
+ collectionSpecificFacetByOptions: {
473
+ collection1: {
474
+ brand: "(sort_by: _alpha:desc)",
475
+ },
476
+ }, // <======= Use this parameter if multiple collections share the same field names, and you want to use different options for each field. This will override facetByOptions for that particular collection.
472
477
  additionalSearchParameters,
473
478
  });
474
479
  ```
475
480
 
476
- Note that for sorting in refinementLists, in addition to sorting on the Typesense Server-side, you'd also need to pass the [`sortBy`](https://www.algolia.com/doc/api-reference/widgets/refinement-list/js/#widget-param-sortby) parameter to the refinementList widget to also sort the results appropriately on the client-side.
481
+ Note that for sorting in refinementLists, in addition to sorting on the Typesense Server-side, you'd also need to pass the [`sortBy`](https://www.algolia.com/doc/api-reference/widgets/refinement-list/js/#widget-param-sortby) parameter to the refinementList widget to also sort the results appropriately on the client-side.
482
+
483
+ ### Setting `filter_by` options
484
+
485
+ > Available as of typesense-instantsearch-adapter `2.8.0-5`
486
+
487
+ The `filter_by` parameter is managed by InstantSearch internally when you use the various filter widgets.
488
+
489
+ By default, the adapter uses exact filtering (`filter_by: field:=value`) when sending the queries to Typesense.
490
+ If you need to configure the adapter to use `:` (non-exact word-level filtering - `filter_by: field:value`), you want to instantiate the adapter using the `filterByOptions` configuration:
491
+
492
+ ```js
493
+ const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
494
+ server: {
495
+ apiKey: "xyz",
496
+ nodes: [
497
+ {
498
+ host: "localhost",
499
+ port: "8108",
500
+ path: "/",
501
+ protocol: "http",
502
+ },
503
+ ],
504
+ },
505
+ filterByOptions: {
506
+ brand: { exactMatch: false }, // <========== Add this to do non-exact word-level filtering
507
+ category: { exactMatch: false },
508
+ },
509
+ collectionSpecificFacetByOptions: {
510
+ collection1: {
511
+ brand: { exactMatch: false },
512
+ },
513
+ }, // <======= Use this parameter if multiple collections share the same field names, and you want to use different options for each field. This will override filterByOptions for that particular collection.
514
+ additionalSearchParameters,
515
+ });
516
+ ```
477
517
 
478
518
  ### Grouped Hits
479
519
 
@@ -2207,7 +2207,10 @@ var Configuration = /*#__PURE__*/function () {
2207
2207
  _options$collectionSp,
2208
2208
  _this = this,
2209
2209
  _options$flattenGroup,
2210
- _options$facetByOptio;
2210
+ _options$facetByOptio,
2211
+ _options$filterByOpti,
2212
+ _options$collectionSp2,
2213
+ _options$collectionSp3;
2211
2214
 
2212
2215
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
2213
2216
 
@@ -2246,6 +2249,9 @@ var Configuration = /*#__PURE__*/function () {
2246
2249
  this.renderingContent = options.renderingContent;
2247
2250
  this.flattenGroupedHits = (_options$flattenGroup = options.flattenGroupedHits) !== null && _options$flattenGroup !== void 0 ? _options$flattenGroup : true;
2248
2251
  this.facetByOptions = (_options$facetByOptio = options.facetByOptions) !== null && _options$facetByOptio !== void 0 ? _options$facetByOptio : {};
2252
+ this.filterByOptions = (_options$filterByOpti = options.filterByOptions) !== null && _options$filterByOpti !== void 0 ? _options$filterByOpti : {};
2253
+ this.collectionSpecificFacetByOptions = (_options$collectionSp2 = options.collectionSpecificFacetByOptions) !== null && _options$collectionSp2 !== void 0 ? _options$collectionSp2 : {};
2254
+ this.collectionSpecificFilterByOptions = (_options$collectionSp3 = options.collectionSpecificFilterByOptions) !== null && _options$collectionSp3 !== void 0 ? _options$collectionSp3 : {};
2249
2255
  }
2250
2256
 
2251
2257
  (0,_babel_runtime_helpers_createClass__WEBPACK_IMPORTED_MODULE_1__["default"])(Configuration, [{
@@ -2376,8 +2382,19 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2376
2382
  }
2377
2383
 
2378
2384
  (0,_babel_runtime_helpers_createClass__WEBPACK_IMPORTED_MODULE_3__["default"])(SearchRequestAdapter, [{
2385
+ key: "_shouldUseExactMatchForField",
2386
+ value: function _shouldUseExactMatchForField(fieldName, collectionName) {
2387
+ var _this$configuration$c, _this$configuration$f;
2388
+
2389
+ if (((_this$configuration$c = this.configuration.collectionSpecificFilterByOptions) === null || _this$configuration$c === void 0 || (_this$configuration$c = _this$configuration$c[collectionName]) === null || _this$configuration$c === void 0 || (_this$configuration$c = _this$configuration$c[fieldName]) === null || _this$configuration$c === void 0 ? void 0 : _this$configuration$c.exactMatch) === false || ((_this$configuration$f = this.configuration.filterByOptions) === null || _this$configuration$f === void 0 || (_this$configuration$f = _this$configuration$f[fieldName]) === null || _this$configuration$f === void 0 ? void 0 : _this$configuration$f.exactMatch) === false) {
2390
+ return false;
2391
+ } else {
2392
+ return true;
2393
+ }
2394
+ }
2395
+ }, {
2379
2396
  key: "_adaptFacetFilters",
2380
- value: function _adaptFacetFilters(facetFilters) {
2397
+ value: function _adaptFacetFilters(facetFilters, collectionName) {
2381
2398
  var _this = this;
2382
2399
 
2383
2400
  var adaptedResult = "";
@@ -2453,13 +2470,16 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2453
2470
  var typesenseFilterStringComponents = [];
2454
2471
 
2455
2472
  if (includedFieldValues.length > 0) {
2456
- typesenseFilterStringComponents.push("".concat(fieldName, ":=[").concat(includedFieldValues.map(function (v) {
2473
+ var operator = _this._shouldUseExactMatchForField(fieldName, collectionName) ? ":=" : ":";
2474
+ typesenseFilterStringComponents.push("".concat(fieldName).concat(operator, "[").concat(includedFieldValues.map(function (v) {
2457
2475
  return _this._escapeFacetValue(v);
2458
2476
  }).join(","), "]"));
2459
2477
  }
2460
2478
 
2461
2479
  if (excludedFieldValues.length > 0) {
2462
- typesenseFilterStringComponents.push("".concat(fieldName, ":!=[").concat(excludedFieldValues.map(function (v) {
2480
+ var _operator = _this._shouldUseExactMatchForField(fieldName, collectionName) ? ":!=" : ":!";
2481
+
2482
+ typesenseFilterStringComponents.push("".concat(fieldName).concat(_operator, "[").concat(excludedFieldValues.map(function (v) {
2463
2483
  return _this._escapeFacetValue(v);
2464
2484
  }).join(","), "]"));
2465
2485
  }
@@ -2480,9 +2500,13 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2480
2500
  var _typesenseFilterString;
2481
2501
 
2482
2502
  if (fieldValue.startsWith("-") && !_this._isNumber(fieldValue)) {
2483
- _typesenseFilterString = "".concat(_fieldName, ":!=[").concat(_this._escapeFacetValue(fieldValue.substring(1)), "]");
2503
+ var _operator2 = _this._shouldUseExactMatchForField(_fieldName, collectionName) ? ":!=" : ":!";
2504
+
2505
+ _typesenseFilterString = "".concat(_fieldName).concat(_operator2, "[").concat(_this._escapeFacetValue(fieldValue.substring(1)), "]");
2484
2506
  } else {
2485
- _typesenseFilterString = "".concat(_fieldName, ":=[").concat(_this._escapeFacetValue(fieldValue), "]");
2507
+ var _operator3 = _this._shouldUseExactMatchForField(_fieldName, collectionName) ? ":=" : ":";
2508
+
2509
+ _typesenseFilterString = "".concat(_fieldName).concat(_operator3, "[").concat(_this._escapeFacetValue(fieldValue), "]");
2486
2510
  }
2487
2511
 
2488
2512
  return _typesenseFilterString;
@@ -2495,12 +2519,12 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2495
2519
  }, {
2496
2520
  key: "_parseFacetFilter",
2497
2521
  value: function _parseFacetFilter(facetFilter) {
2498
- var _this$configuration$f;
2522
+ var _this$configuration$f2;
2499
2523
 
2500
2524
  var filterStringMatchingRegex, facetFilterMatches, fieldName, fieldValue; // This is helpful when the filter looks like `facetName:with:colons:facetValue:with:colons` and the default regex above parses the filter as `facetName:with:colons:facetValue:with` and `colon`.
2501
2525
  // So if a facetValue can contain a colon, we ask users to pass in all possible facetable fields in `facetableFieldsWithSpecialCharacters` when instantiating the adapter, so we can explicitly match against that.
2502
2526
 
2503
- if (((_this$configuration$f = this.configuration.facetableFieldsWithSpecialCharacters) === null || _this$configuration$f === void 0 ? void 0 : _this$configuration$f.length) > 0) {
2527
+ if (((_this$configuration$f2 = this.configuration.facetableFieldsWithSpecialCharacters) === null || _this$configuration$f2 === void 0 ? void 0 : _this$configuration$f2.length) > 0) {
2504
2528
  // escape any Regex special characters, source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
2505
2529
  var sanitizedFacetableFieldsWithSpecialCharacters = this.configuration.facetableFieldsWithSpecialCharacters.flat().map(function (f) {
2506
2530
  return f.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
@@ -2613,13 +2637,13 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2613
2637
  }, {
2614
2638
  key: "_parseNumericFilter",
2615
2639
  value: function _parseNumericFilter(numericFilter) {
2616
- var _this$configuration$f2;
2640
+ var _this$configuration$f3;
2617
2641
 
2618
2642
  var filterStringMatchingRegex, numericFilterMatches;
2619
2643
  var fieldName, operator, fieldValue; // The following is helpful when the facetName has special characters like > and the default regex fails to parse it properly.
2620
2644
  // So we ask users to pass in facetable fields in `facetableFieldsWithSpecialCharactersWithSpecialCharacters` when instantiating the adapter, so we can explicitly match against that.
2621
2645
 
2622
- if (((_this$configuration$f2 = this.configuration.facetableFieldsWithSpecialCharacters) === null || _this$configuration$f2 === void 0 ? void 0 : _this$configuration$f2.length) > 0) {
2646
+ if (((_this$configuration$f3 = this.configuration.facetableFieldsWithSpecialCharacters) === null || _this$configuration$f3 === void 0 ? void 0 : _this$configuration$f3.length) > 0) {
2623
2647
  // escape any Regex special characters, source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
2624
2648
  var sanitizedFacetableFieldsWithSpecialCharacters = this.configuration.facetableFieldsWithSpecialCharacters.map(function (f) {
2625
2649
  return f.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
@@ -2725,7 +2749,7 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2725
2749
  }
2726
2750
  }, {
2727
2751
  key: "_adaptFilters",
2728
- value: function _adaptFilters(instantsearchParams) {
2752
+ value: function _adaptFilters(instantsearchParams, collectionName) {
2729
2753
  var adaptedFilters = []; // `filters` can be used with the `Configure` widget
2730
2754
  // However the format needs to be in the Typesense filter_by format, instead of Algolia filter format.
2731
2755
 
@@ -2733,7 +2757,7 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2733
2757
  adaptedFilters.push(instantsearchParams.filters);
2734
2758
  }
2735
2759
 
2736
- adaptedFilters.push(this._adaptFacetFilters(instantsearchParams.facetFilters));
2760
+ adaptedFilters.push(this._adaptFacetFilters(instantsearchParams.facetFilters, collectionName));
2737
2761
  adaptedFilters.push(this._adaptNumericFilters(instantsearchParams.numericFilters));
2738
2762
  adaptedFilters.push(this._adaptGeoFilter(instantsearchParams));
2739
2763
  return adaptedFilters.filter(function (filter) {
@@ -2752,11 +2776,15 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2752
2776
  }
2753
2777
  }, {
2754
2778
  key: "_adaptFacetBy",
2755
- value: function _adaptFacetBy(facets) {
2779
+ value: function _adaptFacetBy(facets, collectionName) {
2756
2780
  var _this3 = this;
2757
2781
 
2758
2782
  return [facets].flat().map(function (facet) {
2759
- if (_this3.configuration.facetByOptions[facet]) {
2783
+ var _this3$configuration$;
2784
+
2785
+ if ((_this3$configuration$ = _this3.configuration.collectionSpecificFacetByOptions) !== null && _this3$configuration$ !== void 0 && (_this3$configuration$ = _this3$configuration$[collectionName]) !== null && _this3$configuration$ !== void 0 && _this3$configuration$[facet]) {
2786
+ return "".concat(facet).concat(_this3.configuration.collectionSpecificFacetByOptions[collectionName][facet]);
2787
+ } else if (_this3.configuration.facetByOptions[facet]) {
2760
2788
  return "".concat(facet).concat(_this3.configuration.facetByOptions[facet]);
2761
2789
  } else {
2762
2790
  return facet;
@@ -2800,8 +2828,8 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2800
2828
  Object.assign(typesenseSearchParams, {
2801
2829
  collection: adaptedCollectionName,
2802
2830
  q: params.query === "" || params.query === undefined ? "*" : params.query,
2803
- facet_by: snakeCasedAdditionalSearchParameters.facet_by || this._adaptFacetBy(params.facets),
2804
- filter_by: this._adaptFilters(params) || snakeCasedAdditionalSearchParameters.filter_by,
2831
+ facet_by: snakeCasedAdditionalSearchParameters.facet_by || this._adaptFacetBy(params.facets, adaptedCollectionName),
2832
+ filter_by: this._adaptFilters(params, adaptedCollectionName) || snakeCasedAdditionalSearchParameters.filter_by,
2805
2833
  sort_by: adaptedSortBy || snakeCasedAdditionalSearchParameters.sort_by,
2806
2834
  max_facet_values: params.maxValuesPerFacet,
2807
2835
  page: (params.page || 0) + 1