typesense-instantsearch-adapter 2.8.0-4 → 2.8.0

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
 
@@ -480,6 +480,41 @@ const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
480
480
 
481
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
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
+ ```
517
+
483
518
  ### Grouped Hits
484
519
 
485
520
  > Available as of typesense-instantsearch-adapter `2.7.1-4`
@@ -2208,7 +2208,9 @@ var Configuration = /*#__PURE__*/function () {
2208
2208
  _this = this,
2209
2209
  _options$flattenGroup,
2210
2210
  _options$facetByOptio,
2211
- _options$collectionSp2;
2211
+ _options$filterByOpti,
2212
+ _options$collectionSp2,
2213
+ _options$collectionSp3;
2212
2214
 
2213
2215
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
2214
2216
 
@@ -2247,7 +2249,9 @@ var Configuration = /*#__PURE__*/function () {
2247
2249
  this.renderingContent = options.renderingContent;
2248
2250
  this.flattenGroupedHits = (_options$flattenGroup = options.flattenGroupedHits) !== null && _options$flattenGroup !== void 0 ? _options$flattenGroup : true;
2249
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 : {};
2250
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 : {};
2251
2255
  }
2252
2256
 
2253
2257
  (0,_babel_runtime_helpers_createClass__WEBPACK_IMPORTED_MODULE_1__["default"])(Configuration, [{
@@ -2378,8 +2382,19 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2378
2382
  }
2379
2383
 
2380
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
+ }, {
2381
2396
  key: "_adaptFacetFilters",
2382
- value: function _adaptFacetFilters(facetFilters) {
2397
+ value: function _adaptFacetFilters(facetFilters, collectionName) {
2383
2398
  var _this = this;
2384
2399
 
2385
2400
  var adaptedResult = "";
@@ -2455,13 +2470,16 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2455
2470
  var typesenseFilterStringComponents = [];
2456
2471
 
2457
2472
  if (includedFieldValues.length > 0) {
2458
- 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) {
2459
2475
  return _this._escapeFacetValue(v);
2460
2476
  }).join(","), "]"));
2461
2477
  }
2462
2478
 
2463
2479
  if (excludedFieldValues.length > 0) {
2464
- 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) {
2465
2483
  return _this._escapeFacetValue(v);
2466
2484
  }).join(","), "]"));
2467
2485
  }
@@ -2482,9 +2500,13 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2482
2500
  var _typesenseFilterString;
2483
2501
 
2484
2502
  if (fieldValue.startsWith("-") && !_this._isNumber(fieldValue)) {
2485
- _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)), "]");
2486
2506
  } else {
2487
- _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), "]");
2488
2510
  }
2489
2511
 
2490
2512
  return _typesenseFilterString;
@@ -2497,12 +2519,12 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2497
2519
  }, {
2498
2520
  key: "_parseFacetFilter",
2499
2521
  value: function _parseFacetFilter(facetFilter) {
2500
- var _this$configuration$f;
2522
+ var _this$configuration$f2;
2501
2523
 
2502
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`.
2503
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.
2504
2526
 
2505
- 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) {
2506
2528
  // escape any Regex special characters, source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
2507
2529
  var sanitizedFacetableFieldsWithSpecialCharacters = this.configuration.facetableFieldsWithSpecialCharacters.flat().map(function (f) {
2508
2530
  return f.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
@@ -2615,13 +2637,13 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2615
2637
  }, {
2616
2638
  key: "_parseNumericFilter",
2617
2639
  value: function _parseNumericFilter(numericFilter) {
2618
- var _this$configuration$f2;
2640
+ var _this$configuration$f3;
2619
2641
 
2620
2642
  var filterStringMatchingRegex, numericFilterMatches;
2621
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.
2622
2644
  // So we ask users to pass in facetable fields in `facetableFieldsWithSpecialCharactersWithSpecialCharacters` when instantiating the adapter, so we can explicitly match against that.
2623
2645
 
2624
- 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) {
2625
2647
  // escape any Regex special characters, source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
2626
2648
  var sanitizedFacetableFieldsWithSpecialCharacters = this.configuration.facetableFieldsWithSpecialCharacters.map(function (f) {
2627
2649
  return f.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
@@ -2727,7 +2749,7 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2727
2749
  }
2728
2750
  }, {
2729
2751
  key: "_adaptFilters",
2730
- value: function _adaptFilters(instantsearchParams) {
2752
+ value: function _adaptFilters(instantsearchParams, collectionName) {
2731
2753
  var adaptedFilters = []; // `filters` can be used with the `Configure` widget
2732
2754
  // However the format needs to be in the Typesense filter_by format, instead of Algolia filter format.
2733
2755
 
@@ -2735,7 +2757,7 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2735
2757
  adaptedFilters.push(instantsearchParams.filters);
2736
2758
  }
2737
2759
 
2738
- adaptedFilters.push(this._adaptFacetFilters(instantsearchParams.facetFilters));
2760
+ adaptedFilters.push(this._adaptFacetFilters(instantsearchParams.facetFilters, collectionName));
2739
2761
  adaptedFilters.push(this._adaptNumericFilters(instantsearchParams.numericFilters));
2740
2762
  adaptedFilters.push(this._adaptGeoFilter(instantsearchParams));
2741
2763
  return adaptedFilters.filter(function (filter) {
@@ -2807,7 +2829,7 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2807
2829
  collection: adaptedCollectionName,
2808
2830
  q: params.query === "" || params.query === undefined ? "*" : params.query,
2809
2831
  facet_by: snakeCasedAdditionalSearchParameters.facet_by || this._adaptFacetBy(params.facets, adaptedCollectionName),
2810
- filter_by: this._adaptFilters(params) || snakeCasedAdditionalSearchParameters.filter_by,
2832
+ filter_by: this._adaptFilters(params, adaptedCollectionName) || snakeCasedAdditionalSearchParameters.filter_by,
2811
2833
  sort_by: adaptedSortBy || snakeCasedAdditionalSearchParameters.sort_by,
2812
2834
  max_facet_values: params.maxValuesPerFacet,
2813
2835
  page: (params.page || 0) + 1