typesense-instantsearch-adapter 2.7.0-1 → 2.7.0-2

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
@@ -398,12 +398,54 @@ const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
398
398
  });
399
399
  ```
400
400
 
401
+ ### Special characters in field names / values
402
+
403
+ - If any string fields in your documents have a colon `:` in their values (for eg, let's say there's a field called `{ brand: "a:b" }`, then you would need to add a parameter like below when instantiating the adapter:
404
+
405
+ ```js
406
+ const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
407
+ server: {
408
+ apiKey: "xyz",
409
+ nodes: [
410
+ {
411
+ host: "localhost",
412
+ port: "8108",
413
+ path: "/",
414
+ protocol: "http",
415
+ },
416
+ ],
417
+ },
418
+ facetableFieldsWithSpecialCharacters: ["brand"], // <======= Add string fields that have colons in their values here, to aid in parsing
419
+ additionalSearchParameters,
420
+ });
421
+ ```
422
+
423
+ - If any numeric field _names_ in your documents have special characters like `>`, `<`, `=` (for eg, let's say there's a field called `{ price>discount: 3.0 }`) then you would need to add a parameter like below when instantiating the adapter:
424
+
425
+ ```js
426
+ const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
427
+ server: {
428
+ apiKey: "xyz",
429
+ nodes: [
430
+ {
431
+ host: "localhost",
432
+ port: "8108",
433
+ path: "/",
434
+ protocol: "http",
435
+ },
436
+ ],
437
+ },
438
+ facetableFieldsWithSpecialCharacters: ["price>discount"], // // <======= Add numeric fields that have >, < or = in their names, to aid in parsing
439
+ additionalSearchParameters,
440
+ });
441
+ ```
442
+
401
443
  Read more about all available options for `renderingContent` in [Algolia's documentation here](https://www.algolia.com/doc/api-reference/api-parameters/renderingContent/?client=javascript#examples).
402
444
 
403
445
  ## Compatibility
404
446
 
405
447
  | Typesense Server | typesense-instantsearch-adapter | instantsearch.js | react-instantsearch | vue-instantsearch | angular-instantsearch |
406
- |------------------|---------------------------------| ---------------- | ------------------- | ----------------- | --------------------- |
448
+ | ---------------- | ------------------------------- | ---------------- | ------------------- | ----------------- | --------------------- |
407
449
  | \>= v0.25.0.rc14 | \>= v2.7.0-1 | \>= 4.51 | \>= 6.39 | \>= 4.8 | \>= 4.4 |
408
450
  | \>= v0.25.0.rc12 | \>= v2.6.0 | \>= 4.51 | \>= 6.39 | \>= 4.8 | \>= 4.4 |
409
451
  | \>= v0.24 | \>= v2.5.0 | \>= 4.2.0 | \>= 6.0.0 | \>= 2.2.1 | \>= 3.0.0 |
@@ -2214,6 +2214,7 @@ var Configuration = /*#__PURE__*/function () {
2214
2214
  _ref4,
2215
2215
  _this$additionalSearc4,
2216
2216
  _options$geoLocationF,
2217
+ _options$facetableFie,
2217
2218
  _options$collectionSp,
2218
2219
  _this = this;
2219
2220
 
@@ -2236,6 +2237,7 @@ var Configuration = /*#__PURE__*/function () {
2236
2237
  this.additionalSearchParameters.sort_by = (_ref3 = (_this$additionalSearc3 = this.additionalSearchParameters.sortBy) !== null && _this$additionalSearc3 !== void 0 ? _this$additionalSearc3 : this.additionalSearchParameters.sort_by) !== null && _ref3 !== void 0 ? _ref3 : "";
2237
2238
  this.additionalSearchParameters.highlight_full_fields = (_ref4 = (_this$additionalSearc4 = this.additionalSearchParameters.highlightFullFields) !== null && _this$additionalSearc4 !== void 0 ? _this$additionalSearc4 : this.additionalSearchParameters.highlight_full_fields) !== null && _ref4 !== void 0 ? _ref4 : this.additionalSearchParameters.query_by;
2238
2239
  this.geoLocationField = (_options$geoLocationF = options.geoLocationField) !== null && _options$geoLocationF !== void 0 ? _options$geoLocationF : "_geoloc";
2240
+ this.facetableFieldsWithSpecialCharacters = (_options$facetableFie = options.facetableFieldsWithSpecialCharacters) !== null && _options$facetableFie !== void 0 ? _options$facetableFie : [];
2239
2241
  this.collectionSpecificSearchParameters = (_options$collectionSp = options.collectionSpecificSearchParameters) !== null && _options$collectionSp !== void 0 ? _options$collectionSp : {};
2240
2242
  Object.keys(this.collectionSpecificSearchParameters).forEach(function (collection) {
2241
2243
  var _params$queryBy, _params$preset, _params$sortBy, _ref5, _ref6, _params$highlightFull;
@@ -2419,9 +2421,10 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2419
2421
  // }
2420
2422
  var intermediateFacetFilters = {};
2421
2423
  item.forEach(function (facetFilter) {
2422
- var facetFilterMatches = facetFilter.match(_this.constructor.FILTER_STRING_MATCHING_REGEX);
2423
- var fieldName = "".concat(facetFilterMatches[1]).concat(facetFilterMatches[2]);
2424
- var fieldValue = "".concat(facetFilterMatches[3]);
2424
+ var _this$_parseFacetFilt = _this._parseFacetFilter(facetFilter),
2425
+ fieldName = _this$_parseFacetFilt.fieldName,
2426
+ fieldValue = _this$_parseFacetFilt.fieldValue;
2427
+
2425
2428
  intermediateFacetFilters[fieldName] = intermediateFacetFilters[fieldName] || [];
2426
2429
  intermediateFacetFilters[fieldName].push(fieldValue);
2427
2430
  });
@@ -2477,11 +2480,9 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2477
2480
  // fieldName:fieldValue
2478
2481
  // Into
2479
2482
  // fieldName:=fieldValue
2480
- var facetFilterMatches = item.match(_this.constructor.FILTER_STRING_MATCHING_REGEX);
2481
-
2482
- var _fieldName = "".concat(facetFilterMatches[1]).concat(facetFilterMatches[2]);
2483
-
2484
- var fieldValue = "".concat(facetFilterMatches[3]);
2483
+ var _this$_parseFacetFilt2 = _this._parseFacetFilter(item),
2484
+ _fieldName = _this$_parseFacetFilt2.fieldName,
2485
+ fieldValue = _this$_parseFacetFilt2.fieldValue;
2485
2486
 
2486
2487
  var _typesenseFilterString;
2487
2488
 
@@ -2498,6 +2499,51 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2498
2499
 
2499
2500
  return adaptedResult;
2500
2501
  }
2502
+ }, {
2503
+ key: "_parseFacetFilter",
2504
+ value: function _parseFacetFilter(facetFilter) {
2505
+ var _this$configuration$f;
2506
+
2507
+ 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`.
2508
+ // 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.
2509
+
2510
+ if (((_this$configuration$f = this.configuration.facetableFieldsWithSpecialCharacters) === null || _this$configuration$f === void 0 ? void 0 : _this$configuration$f.length) > 0) {
2511
+ // escape any Regex special characters, source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
2512
+ var sanitizedFacetableFieldsWithSpecialCharacters = this.configuration.facetableFieldsWithSpecialCharacters.flat().map(function (f) {
2513
+ return f.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2514
+ });
2515
+ filterStringMatchingRegex = new RegExp("^(".concat(sanitizedFacetableFieldsWithSpecialCharacters.join("|"), "):(.*)$"));
2516
+ facetFilterMatches = facetFilter.match(filterStringMatchingRegex);
2517
+
2518
+ if (facetFilterMatches != null) {
2519
+ fieldName = "".concat(facetFilterMatches[1]);
2520
+ fieldValue = "".concat(facetFilterMatches[2]);
2521
+ return {
2522
+ fieldName: fieldName,
2523
+ fieldValue: fieldValue
2524
+ };
2525
+ }
2526
+ } // If we haven't found any matches yet
2527
+ // Use the default filter parsing regex, which assumes that only facet names have colons, and not facet values
2528
+
2529
+
2530
+ filterStringMatchingRegex = this.constructor.DEFAULT_FACET_FILTER_STRING_MATCHING_REGEX;
2531
+ facetFilterMatches = facetFilter.match(filterStringMatchingRegex); // console.log(filterStringMatchingRegex);
2532
+ // console.log(facetFilter);
2533
+ // console.log(facetFilterMatches);
2534
+
2535
+ if (facetFilterMatches == null) {
2536
+ console.error("[Typesense-Instantsearch-Adapter] Parsing failed for a facet filter `".concat(facetFilter, "` with the Regex `").concat(filterStringMatchingRegex, "`. If you have field names with special characters, be sure to add them to a parameter called `facetableFieldsWithSpecialCharacters` when instantiating the adapter."));
2537
+ } else {
2538
+ fieldName = "".concat(facetFilterMatches[1]).concat(facetFilterMatches[2]);
2539
+ fieldValue = "".concat(facetFilterMatches[3]);
2540
+ }
2541
+
2542
+ return {
2543
+ fieldName: fieldName,
2544
+ fieldValue: fieldValue
2545
+ };
2546
+ }
2501
2547
  }, {
2502
2548
  key: "_escapeFacetValue",
2503
2549
  value: function _escapeFacetValue(value) {
@@ -2517,6 +2563,8 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2517
2563
  }, {
2518
2564
  key: "_adaptNumericFilters",
2519
2565
  value: function _adaptNumericFilters(numericFilters) {
2566
+ var _this2 = this;
2567
+
2520
2568
  // Need to transform this:
2521
2569
  // ["field1<=634", "field1>=289", "field2<=5", "field3>=3"]
2522
2570
  // to:
@@ -2542,14 +2590,13 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2542
2590
 
2543
2591
  var filtersHash = {};
2544
2592
  numericFilters.forEach(function (filter) {
2545
- var _filter$match = filter.match(new RegExp("(.*?)(<=|>=|>|<|:|=)(.*)")),
2546
- _filter$match2 = (0,_babel_runtime_helpers_slicedToArray__WEBPACK_IMPORTED_MODULE_1__["default"])(_filter$match, 4),
2547
- field = _filter$match2[1],
2548
- operator = _filter$match2[2],
2549
- value = _filter$match2[3];
2550
-
2551
- filtersHash[field] = filtersHash[field] || {};
2552
- filtersHash[field][operator] = value;
2593
+ var _this2$_parseNumericF = _this2._parseNumericFilter(filter),
2594
+ fieldName = _this2$_parseNumericF.fieldName,
2595
+ operator = _this2$_parseNumericF.operator,
2596
+ fieldValue = _this2$_parseNumericF.fieldValue;
2597
+
2598
+ filtersHash[fieldName] = filtersHash[fieldName] || {};
2599
+ filtersHash[fieldName][operator] = fieldValue;
2553
2600
  }); // Transform that to:
2554
2601
  // "field1:=[634..289] && field2:<=5 && field3:>=3"
2555
2602
 
@@ -2570,6 +2617,64 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2570
2617
  adaptedResult = adaptedFilters.join(" && ");
2571
2618
  return adaptedResult;
2572
2619
  }
2620
+ }, {
2621
+ key: "_parseNumericFilter",
2622
+ value: function _parseNumericFilter(numericFilter) {
2623
+ var _this$configuration$f2;
2624
+
2625
+ var filterStringMatchingRegex, numericFilterMatches;
2626
+ var fieldName, operator, fieldValue; // The following is helpful when the facetName has special characters like > and the default regex fails to parse it properly.
2627
+ // So we ask users to pass in facetable fields in `facetableFieldsWithSpecialCharactersWithSpecialCharacters` when instantiating the adapter, so we can explicitly match against that.
2628
+
2629
+ if (((_this$configuration$f2 = this.configuration.facetableFieldsWithSpecialCharacters) === null || _this$configuration$f2 === void 0 ? void 0 : _this$configuration$f2.length) > 0) {
2630
+ // escape any Regex special characters, source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
2631
+ var sanitizedFacetableFieldsWithSpecialCharacters = this.configuration.facetableFieldsWithSpecialCharacters.map(function (f) {
2632
+ return f.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2633
+ });
2634
+ filterStringMatchingRegex = new RegExp("^(".concat(sanitizedFacetableFieldsWithSpecialCharacters.join("|"), ")(<=|>=|>|<|=)(.*)$"));
2635
+ numericFilterMatches = numericFilter.match(filterStringMatchingRegex);
2636
+
2637
+ if (numericFilterMatches != null) {
2638
+ // If no matches are found or if the above didn't trigger, fall back to the default regex
2639
+ var _numericFilterMatches = numericFilterMatches;
2640
+
2641
+ var _numericFilterMatches2 = (0,_babel_runtime_helpers_slicedToArray__WEBPACK_IMPORTED_MODULE_1__["default"])(_numericFilterMatches, 4);
2642
+
2643
+ fieldName = _numericFilterMatches2[1];
2644
+ operator = _numericFilterMatches2[2];
2645
+ fieldValue = _numericFilterMatches2[3];
2646
+ return {
2647
+ fieldName: fieldName,
2648
+ operator: operator,
2649
+ fieldValue: fieldValue
2650
+ };
2651
+ }
2652
+ } // If we haven't found any matches yet, fall back to the default regex
2653
+
2654
+
2655
+ filterStringMatchingRegex = this.constructor.DEFAULT_NUMERIC_FILTER_STRING_MATCHING_REGEX;
2656
+ numericFilterMatches = numericFilter.match(filterStringMatchingRegex); // console.log(filterStringMatchingRegex);
2657
+ // console.log(numericFilter);
2658
+ // console.log(numericFilterMatches);
2659
+
2660
+ if (numericFilterMatches == null) {
2661
+ console.error("[Typesense-Instantsearch-Adapter] Parsing failed for a numeric filter `".concat(numericFilter, "` with the Regex `").concat(filterStringMatchingRegex, "`. If you have field names with special characters, be sure to add them to a parameter called `facetableFieldsWithSpecialCharacters` when instantiating the adapter."));
2662
+ } else {
2663
+ var _numericFilterMatches3 = numericFilterMatches;
2664
+
2665
+ var _numericFilterMatches4 = (0,_babel_runtime_helpers_slicedToArray__WEBPACK_IMPORTED_MODULE_1__["default"])(_numericFilterMatches3, 4);
2666
+
2667
+ fieldName = _numericFilterMatches4[1];
2668
+ operator = _numericFilterMatches4[2];
2669
+ fieldValue = _numericFilterMatches4[3];
2670
+ }
2671
+
2672
+ return {
2673
+ fieldName: fieldName,
2674
+ operator: operator,
2675
+ fieldValue: fieldValue
2676
+ };
2677
+ }
2573
2678
  }, {
2574
2679
  key: "_adaptGeoFilter",
2575
2680
  value: function _adaptGeoFilter(_ref) {
@@ -2726,15 +2831,16 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2726
2831
  key: "request",
2727
2832
  value: function () {
2728
2833
  var _request = (0,_babel_runtime_helpers_asyncToGenerator__WEBPACK_IMPORTED_MODULE_0__["default"])( /*#__PURE__*/_babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_4___default().mark(function _callee() {
2729
- var _this2 = this;
2834
+ var _this3 = this;
2730
2835
 
2731
2836
  var searches;
2732
2837
  return _babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_4___default().wrap(function _callee$(_context) {
2733
2838
  while (1) {
2734
2839
  switch (_context.prev = _context.next) {
2735
2840
  case 0:
2841
+ // console.log(this.instantsearchRequests);
2736
2842
  searches = this.instantsearchRequests.map(function (instantsearchRequest) {
2737
- return _this2._buildSearchParameters(instantsearchRequest);
2843
+ return _this3._buildSearchParameters(instantsearchRequest);
2738
2844
  });
2739
2845
  return _context.abrupt("return", this.typesenseClient.multiSearch.perform({
2740
2846
  searches: searches
@@ -2760,10 +2866,15 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
2760
2866
  return new RegExp("^(.+?)(?=(/sort/(.*))|$)");
2761
2867
  }
2762
2868
  }, {
2763
- key: "FILTER_STRING_MATCHING_REGEX",
2869
+ key: "DEFAULT_FACET_FILTER_STRING_MATCHING_REGEX",
2764
2870
  get: function get() {
2765
2871
  return new RegExp("(.*)((?!:).):(?!:)(.*)");
2766
2872
  }
2873
+ }, {
2874
+ key: "DEFAULT_NUMERIC_FILTER_STRING_MATCHING_REGEX",
2875
+ get: function get() {
2876
+ return new RegExp("(.*?)(<=|>=|>|<|=)(.*)");
2877
+ }
2767
2878
  }]);
2768
2879
 
2769
2880
  return SearchRequestAdapter;