typesense-instantsearch-adapter 3.0.3-2 → 3.0.3

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
@@ -311,6 +311,8 @@ If you need to specify a `filter_by` search parameter for Typesense, you want to
311
311
  The format for `facetFilters` and `numericFilters` is the same as Algolia's as described [here](https://www.algolia.com/doc/api-reference/api-parameters/facetFilters/).
312
312
  But `filters` needs to be in Typesense's `filter_by` format as described in this table [here](https://typesense.org/docs/latest/api/documents.html#query-parameters).
313
313
 
314
+ There's one difference from Algolia's format, for values that look like numbers. Algolia reads `temp:-5` as "exclude 5", but this adapter matches the number -5 by default, as it always has. Set `interpretNegativeNumericFacetFiltersAsExclusions: true` to get Algolia's behavior, then write `temp:\-5` wherever you mean the number, including in `initialUiState` and routed URLs. In a JavaScript string, write `"temp:\\-5"`. Plain `"temp:\-5"` is the same string as `"temp:-5"`. Without the option, you can still exclude 5 with `filters: "temp:!=5"`.
315
+
314
316
  Setting `filter_by` inside the `additionalQueryParameters` config only works when the widgets are loaded initially, because InstantSearch internally overrides the `filter_by` field subsequently.
315
317
  Read more [here](https://github.com/typesense/typesense-instantsearch-adapter/issues/17#issuecomment-746912375).
316
318
 
@@ -10530,7 +10530,8 @@ var Configuration = /*#__PURE__*/function () {
10530
10530
  _options$collectionSp4,
10531
10531
  _options$union,
10532
10532
  _options$useOverrideT,
10533
- _options$flipNegative;
10533
+ _options$flipNegative,
10534
+ _options$interpretNeg;
10534
10535
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
10535
10536
  (0,_babel_runtime_helpers_classCallCheck__WEBPACK_IMPORTED_MODULE_0__["default"])(this, Configuration);
10536
10537
  this.server = options.server || {
@@ -10577,6 +10578,7 @@ var Configuration = /*#__PURE__*/function () {
10577
10578
  // flips negative refinement encoding between AND/OR groups
10578
10579
  // AND groups become field:![a,b], OR groups become field:!a || field:!b
10579
10580
  this.flipNegativeRefinementOperator = (_options$flipNegative = options.flipNegativeRefinementOperator) !== null && _options$flipNegative !== void 0 ? _options$flipNegative : false;
10581
+ this.interpretNegativeNumericFacetFiltersAsExclusions = (_options$interpretNeg = options.interpretNegativeNumericFacetFiltersAsExclusions) !== null && _options$interpretNeg !== void 0 ? _options$interpretNeg : false;
10580
10582
  }
10581
10583
  return (0,_babel_runtime_helpers_createClass__WEBPACK_IMPORTED_MODULE_1__["default"])(Configuration, [{
10582
10584
  key: "validate",
@@ -10771,7 +10773,7 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
10771
10773
 
10772
10774
  // Partition values into included and excluded values
10773
10775
  var _fieldValues$reduce = fieldValues.reduce(function (result, fieldValue) {
10774
- if (fieldValue.startsWith("-") && !_this._isNumber(fieldValue)) {
10776
+ if (_this._isExcludedFacetValue(fieldValue)) {
10775
10777
  result[0].push(fieldValue.substring(1));
10776
10778
  } else {
10777
10779
  result[1].push(fieldValue);
@@ -10814,7 +10816,7 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
10814
10816
  _fieldName = _this$_parseFacetFilt2.fieldName,
10815
10817
  fieldValue = _this$_parseFacetFilt2.fieldValue;
10816
10818
  var _typesenseFilterString;
10817
- if (fieldValue.startsWith("-") && !_this._isNumber(fieldValue)) {
10819
+ if (_this._isExcludedFacetValue(fieldValue)) {
10818
10820
  var _operator2 = _this._shouldUseExactMatchForField(_fieldName, collectionName) ? ":!=" : ":!";
10819
10821
  _typesenseFilterString = "".concat(_fieldName).concat(_operator2, "[").concat(_this._escapeFacetValue(fieldValue.substring(1)), "]");
10820
10822
  } else {
@@ -10874,9 +10876,20 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
10874
10876
  fieldValue: fieldValue
10875
10877
  };
10876
10878
  }
10879
+
10880
+ // Algolia reads "-5" as "not 5", but sites that hand-write "-5" in URLs or initialUiState mean the number -5.
10881
+ }, {
10882
+ key: "_isExcludedFacetValue",
10883
+ value: function _isExcludedFacetValue(fieldValue) {
10884
+ return fieldValue.startsWith("-") && (this.configuration.interpretNegativeNumericFacetFiltersAsExclusions || !this._isNumber(fieldValue));
10885
+ }
10877
10886
  }, {
10878
10887
  key: "_escapeFacetValue",
10879
10888
  value: function _escapeFacetValue(value) {
10889
+ // "\-" is InstantSearch's escape for a value that really starts with "-"
10890
+ if (typeof value === "string" && value.startsWith("\\-")) {
10891
+ value = value.substring(1);
10892
+ }
10880
10893
  // Don't escape booleans, integers or floats
10881
10894
  if (typeof value === "boolean" || value === "true" || value === "false" || this._isNumber(value)) {
10882
10895
  return value;