typesense-instantsearch-adapter 2.9.1-1 → 2.10.0-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
@@ -343,6 +343,47 @@ const searchClient = typesenseInstantsearchAdapter.searchClient;
343
343
 
344
344
  Essentially, any parameters set in `collectionSpecificSearchParameters` will be merged with the values in `additionalSearchParameters` when querying Typesense, effectively overriding values in `additionalSearchParameters` on a per-collection-basis.
345
345
 
346
+ #### Union Search
347
+
348
+ > Available as of typesense-instantsearch-adapter `2.10.0` and Typesense Server `v28.0`
349
+
350
+ Union search allows you to merge search results from multiple search queries into a single ordered set of hits. This is particularly useful when you want to combine results from different filters or collections.
351
+
352
+ To enable union search, set the `union` parameter to `true` when instantiating the adapter:
353
+
354
+ ```js
355
+ const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
356
+ server: {
357
+ apiKey: "xyz",
358
+ nodes: [
359
+ {
360
+ host: "localhost",
361
+ port: "8108",
362
+ path: "/",
363
+ protocol: "http",
364
+ },
365
+ ],
366
+ },
367
+ union: true, // <======= Enable union search
368
+ collectionSpecificSearchParameters: {
369
+ products: {
370
+ query_by: "name,description,categories",
371
+ },
372
+ brands: {
373
+ query_by: "name",
374
+ },
375
+ },
376
+ });
377
+ ```
378
+
379
+ When union search is enabled, the adapter will merge search results from multiple search requests into a single ordered result set. This differs from regular multi-search in the following ways:
380
+
381
+ - **Pagination**: Only global pagination parameters are considered, not individual search pagination parameters
382
+ - **Sorting**: All search requests must have the same type, count, and order of sorting fields for the union to work correctly
383
+ - **Result merging**: Results from each search query are combined into one final result set
384
+
385
+ The union search feature leverages Typesense's `multi_search` endpoint with the `union: true` parameter to provide seamless result merging at the server level. For more information, refer to the [Union Search Documentation](https://typesense.org/docs/28.0/api/federated-multi-search.html#union-search)
386
+
346
387
  ### `geoSearch`
347
388
 
348
389
  Algolia uses `_geoloc` by default for the name of the field that stores the lat long values for a record.
@@ -50,7 +50,8 @@ var Configuration = /*#__PURE__*/function () {
50
50
  _options$sortByOption,
51
51
  _options$collectionSp2,
52
52
  _options$collectionSp3,
53
- _options$collectionSp4;
53
+ _options$collectionSp4,
54
+ _options$union;
54
55
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
55
56
  (0,_babel_runtime_helpers_classCallCheck__WEBPACK_IMPORTED_MODULE_0__["default"])(this, Configuration);
56
57
  this.server = options.server || {
@@ -91,6 +92,7 @@ var Configuration = /*#__PURE__*/function () {
91
92
  this.collectionSpecificFacetByOptions = (_options$collectionSp2 = options.collectionSpecificFacetByOptions) !== null && _options$collectionSp2 !== void 0 ? _options$collectionSp2 : {};
92
93
  this.collectionSpecificFilterByOptions = (_options$collectionSp3 = options.collectionSpecificFilterByOptions) !== null && _options$collectionSp3 !== void 0 ? _options$collectionSp3 : {};
93
94
  this.collectionSpecificSortByOptions = (_options$collectionSp4 = options.collectionSpecificSortByOptions) !== null && _options$collectionSp4 !== void 0 ? _options$collectionSp4 : {};
95
+ this.union = (_options$union = options.union) !== null && _options$union !== void 0 ? _options$union : false;
94
96
  }
95
97
  return (0,_babel_runtime_helpers_createClass__WEBPACK_IMPORTED_MODULE_1__["default"])(Configuration, [{
96
98
  key: "validate",
@@ -680,7 +682,7 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
680
682
  var _this4 = this,
681
683
  _searches$,
682
684
  _searches$2;
683
- var searches, commonParams, _searches$3, q, conversation, conversation_id, conversation_model_id;
685
+ var searches, commonParams, _searches$3, q, conversation, conversation_id, conversation_model_id, searchRequest;
684
686
  return _babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_5___default().wrap(function _callee$(_context) {
685
687
  while (1) switch (_context.prev = _context.next) {
686
688
  case 0:
@@ -707,10 +709,14 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
707
709
  return modifiedSearchParams;
708
710
  });
709
711
  }
710
- return _context.abrupt("return", this.typesenseClient.multiSearch.perform({
712
+ searchRequest = {
711
713
  searches: searches
712
- }, commonParams));
713
- case 4:
714
+ }; // Add union parameter if configured
715
+ if (this.configuration.union) {
716
+ searchRequest.union = this.configuration.union;
717
+ }
718
+ return _context.abrupt("return", this.typesenseClient.multiSearch.perform(searchRequest, commonParams));
719
+ case 6:
714
720
  case "end":
715
721
  return _context.stop();
716
722
  }
@@ -832,7 +838,10 @@ var SearchResponseAdapter = /*#__PURE__*/function () {
832
838
  }
833
839
 
834
840
  // Add metadata fields to result, if a field with that name doesn't already exist
835
- ["text_match", "geo_distance_meters", "curated", "text_match_info", "hybrid_search_info", "vector_distance"].forEach(function (metadataField) {
841
+ ["text_match", "geo_distance_meters", "curated", "text_match_info", "hybrid_search_info", "vector_distance", "collection",
842
+ // Union search specific
843
+ "search_index" // Union search specific
844
+ ].forEach(function (metadataField) {
836
845
  if (Object.keys(typesenseHit).includes(metadataField) && !Object.keys(adaptedHit).includes(metadataField)) {
837
846
  adaptedHit[metadataField] = typesenseHit[metadataField];
838
847
  }
@@ -1024,22 +1033,26 @@ var SearchResponseAdapter = /*#__PURE__*/function () {
1024
1033
  key: "_adaptFacets",
1025
1034
  value: function _adaptFacets(typesenseFacetCounts) {
1026
1035
  var adaptedResult = {};
1027
- typesenseFacetCounts.forEach(function (facet) {
1028
- Object.assign(adaptedResult, (0,_babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_3__["default"])({}, facet.field_name, Object.assign.apply(Object, [{}].concat((0,_babel_runtime_helpers_toConsumableArray__WEBPACK_IMPORTED_MODULE_2__["default"])(facet.counts.map(function (count) {
1029
- return (0,_babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_3__["default"])({}, count.value, count.count);
1030
- }))))));
1031
- });
1036
+ if (Array.isArray(typesenseFacetCounts)) {
1037
+ typesenseFacetCounts.forEach(function (facet) {
1038
+ Object.assign(adaptedResult, (0,_babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_3__["default"])({}, facet.field_name, Object.assign.apply(Object, [{}].concat((0,_babel_runtime_helpers_toConsumableArray__WEBPACK_IMPORTED_MODULE_2__["default"])(facet.counts.map(function (count) {
1039
+ return (0,_babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_3__["default"])({}, count.value, count.count);
1040
+ }))))));
1041
+ });
1042
+ }
1032
1043
  return adaptedResult;
1033
1044
  }
1034
1045
  }, {
1035
1046
  key: "_adaptFacetStats",
1036
1047
  value: function _adaptFacetStats(typesenseFacetCounts) {
1037
1048
  var adaptedResult = {};
1038
- typesenseFacetCounts.forEach(function (facet) {
1039
- if (Object.keys(facet.stats).length > 0) {
1040
- Object.assign(adaptedResult, (0,_babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_3__["default"])({}, facet.field_name, facet.stats));
1041
- }
1042
- });
1049
+ if (Array.isArray(typesenseFacetCounts)) {
1050
+ typesenseFacetCounts.forEach(function (facet) {
1051
+ if (facet.stats && Object.keys(facet.stats).length > 0) {
1052
+ Object.assign(adaptedResult, (0,_babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_3__["default"])({}, facet.field_name, facet.stats));
1053
+ }
1054
+ });
1055
+ }
1043
1056
  return adaptedResult;
1044
1057
  }
1045
1058
  }, {
@@ -1052,7 +1065,7 @@ var SearchResponseAdapter = /*#__PURE__*/function () {
1052
1065
  if (((_adaptedResult$facetO = adaptedResult.facetOrdering) === null || _adaptedResult$facetO === void 0 || (_adaptedResult$facetO = _adaptedResult$facetO.facets) === null || _adaptedResult$facetO === void 0 ? void 0 : _adaptedResult$facetO.order) == null) {
1053
1066
  adaptedResult.facetOrdering = adaptedResult.facetOrdering || {};
1054
1067
  adaptedResult.facetOrdering.facets = adaptedResult.facetOrdering.facets || {};
1055
- adaptedResult.facetOrdering.facets.order = (0,_babel_runtime_helpers_toConsumableArray__WEBPACK_IMPORTED_MODULE_2__["default"])(new Set(typesenseFacetCounts.map(function (fc) {
1068
+ adaptedResult.facetOrdering.facets.order = (0,_babel_runtime_helpers_toConsumableArray__WEBPACK_IMPORTED_MODULE_2__["default"])(new Set((Array.isArray(typesenseFacetCounts) ? typesenseFacetCounts : []).map(function (fc) {
1056
1069
  return fc["field_name"];
1057
1070
  }).concat(this.allTypesenseResults.map(function (r) {
1058
1071
  return r.facet_counts || [];
@@ -1074,15 +1087,20 @@ var SearchResponseAdapter = /*#__PURE__*/function () {
1074
1087
  key: "adapt",
1075
1088
  value: function adapt() {
1076
1089
  var adaptedRenderingContent = this._adaptRenderingContent(this.typesenseResponse.facet_counts || []);
1090
+
1091
+ // For union search, use union_request_params, otherwise use request_params
1092
+ var requestParams = this.typesenseResponse.union_request_params ? this.typesenseResponse.union_request_params[0] : this.typesenseResponse.request_params;
1077
1093
  var adaptedResult = _objectSpread({
1078
1094
  hits: this.typesenseResponse.grouped_hits ? this._adaptGroupedHits(this.typesenseResponse.grouped_hits) : this._adaptHits(this.typesenseResponse.hits),
1079
1095
  nbHits: this.typesenseResponse.found,
1080
- page: this.typesenseResponse.page - 1,
1096
+ page: this.typesenseResponse.union_request_params ? this.typesenseResponse.page // Union search already uses 0-based page
1097
+ : this.typesenseResponse.page - 1,
1098
+ // Regular search uses 1-based page, convert to 0-based
1081
1099
  nbPages: this._adaptNumberOfPages(),
1082
- hitsPerPage: this.typesenseResponse.request_params.per_page,
1100
+ hitsPerPage: (requestParams === null || requestParams === void 0 ? void 0 : requestParams.per_page) || 10,
1083
1101
  facets: this._adaptFacets(this.typesenseResponse.facet_counts || []),
1084
- facets_stats: this._adaptFacetStats(this.typesenseResponse.facet_counts || {}),
1085
- query: this.typesenseResponse.request_params.q,
1102
+ facets_stats: this._adaptFacetStats(this.typesenseResponse.facet_counts || []),
1103
+ query: (requestParams === null || requestParams === void 0 ? void 0 : requestParams.q) || "",
1086
1104
  processingTimeMS: this.typesenseResponse.search_time_ms
1087
1105
  }, Object.keys(adaptedRenderingContent).length > 0 ? {
1088
1106
  renderingContent: adaptedRenderingContent
@@ -1127,7 +1145,10 @@ var utils = {
1127
1145
  return value.replace(new RegExp("<mark>", "g"), highlightPreTag || "<mark>").replace(new RegExp("</mark>", "g"), highlightPostTag || "</mark>");
1128
1146
  },
1129
1147
  _adaptNumberOfPages: function _adaptNumberOfPages() {
1130
- var result = this.typesenseResponse.found / this.typesenseResponse.request_params.per_page;
1148
+ // For union search, use union_request_params, otherwise use request_params
1149
+ var requestParams = this.typesenseResponse.union_request_params ? this.typesenseResponse.union_request_params[0] : this.typesenseResponse.request_params;
1150
+ var perPage = (requestParams === null || requestParams === void 0 ? void 0 : requestParams.per_page) || 10;
1151
+ var result = this.typesenseResponse.found / perPage;
1131
1152
  if (Number.isFinite(result)) {
1132
1153
  return Math.ceil(result);
1133
1154
  } else {
@@ -10003,7 +10024,7 @@ var TypesenseInstantsearchAdapter = /*#__PURE__*/function () {
10003
10024
  value: function () {
10004
10025
  var _searchTypesenseAndAdapt = (0,_babel_runtime_helpers_asyncToGenerator__WEBPACK_IMPORTED_MODULE_0__["default"])( /*#__PURE__*/_babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_3___default().mark(function _callee(instantsearchRequests) {
10005
10026
  var _this2 = this;
10006
- var typesenseResponse, adaptedResponses;
10027
+ var typesenseResponse, responseAdapter, adaptedResponse, adaptedResponses, _adaptedResponses;
10007
10028
  return _babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_3___default().wrap(function _callee$(_context) {
10008
10029
  while (1) switch (_context.prev = _context.next) {
10009
10030
  case 0:
@@ -10012,25 +10033,48 @@ var TypesenseInstantsearchAdapter = /*#__PURE__*/function () {
10012
10033
  return this._adaptAndPerformTypesenseRequest(instantsearchRequests);
10013
10034
  case 3:
10014
10035
  typesenseResponse = _context.sent;
10015
- adaptedResponses = typesenseResponse.results.map(function (typesenseResult, index) {
10036
+ if (!typesenseResponse.union_request_params) {
10037
+ _context.next = 12;
10038
+ break;
10039
+ }
10040
+ // Handle union search response - single unified response
10041
+ this._validateTypesenseResult(typesenseResponse);
10042
+ responseAdapter = new _SearchResponseAdapter__WEBPACK_IMPORTED_MODULE_7__.SearchResponseAdapter(typesenseResponse, instantsearchRequests[0],
10043
+ // Use first request as base
10044
+ this.configuration, [typesenseResponse],
10045
+ // Pass single response as allTypesenseResults
10046
+ typesenseResponse);
10047
+ adaptedResponse = responseAdapter.adapt(); // InstantSearch expects one result per request, so return the same adapted response for each request
10048
+ adaptedResponses = instantsearchRequests.map(function () {
10049
+ return adaptedResponse;
10050
+ });
10051
+ return _context.abrupt("return", {
10052
+ results: adaptedResponses
10053
+ });
10054
+ case 12:
10055
+ // Handle regular multi-search response - multiple separate responses
10056
+ _adaptedResponses = typesenseResponse.results.map(function (typesenseResult, index) {
10016
10057
  _this2._validateTypesenseResult(typesenseResult);
10017
10058
  var responseAdapter = new _SearchResponseAdapter__WEBPACK_IMPORTED_MODULE_7__.SearchResponseAdapter(typesenseResult, instantsearchRequests[index], _this2.configuration, typesenseResponse.results, typesenseResponse);
10018
10059
  var adaptedResponse = responseAdapter.adapt();
10019
10060
  return adaptedResponse;
10020
10061
  });
10021
10062
  return _context.abrupt("return", {
10022
- results: adaptedResponses
10063
+ results: _adaptedResponses
10023
10064
  });
10024
- case 8:
10025
- _context.prev = 8;
10065
+ case 14:
10066
+ _context.next = 20;
10067
+ break;
10068
+ case 16:
10069
+ _context.prev = 16;
10026
10070
  _context.t0 = _context["catch"](0);
10027
10071
  console.error(_context.t0);
10028
10072
  throw _context.t0;
10029
- case 12:
10073
+ case 20:
10030
10074
  case "end":
10031
10075
  return _context.stop();
10032
10076
  }
10033
- }, _callee, this, [[0, 8]]);
10077
+ }, _callee, this, [[0, 16]]);
10034
10078
  }));
10035
10079
  function searchTypesenseAndAdapt(_x) {
10036
10080
  return _searchTypesenseAndAdapt.apply(this, arguments);