typesense-instantsearch-adapter 2.9.1-2 → 2.10.0-1

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,15 @@ 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
+ commonParams.page = searches[0].page;
718
+ }
719
+ return _context.abrupt("return", this.typesenseClient.multiSearch.perform(searchRequest, commonParams));
720
+ case 6:
714
721
  case "end":
715
722
  return _context.stop();
716
723
  }
@@ -832,7 +839,10 @@ var SearchResponseAdapter = /*#__PURE__*/function () {
832
839
  }
833
840
 
834
841
  // 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) {
842
+ ["text_match", "geo_distance_meters", "curated", "text_match_info", "hybrid_search_info", "vector_distance", "collection",
843
+ // Union search specific
844
+ "search_index" // Union search specific
845
+ ].forEach(function (metadataField) {
836
846
  if (Object.keys(typesenseHit).includes(metadataField) && !Object.keys(adaptedHit).includes(metadataField)) {
837
847
  adaptedHit[metadataField] = typesenseHit[metadataField];
838
848
  }
@@ -1024,22 +1034,26 @@ var SearchResponseAdapter = /*#__PURE__*/function () {
1024
1034
  key: "_adaptFacets",
1025
1035
  value: function _adaptFacets(typesenseFacetCounts) {
1026
1036
  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
- });
1037
+ if (Array.isArray(typesenseFacetCounts)) {
1038
+ typesenseFacetCounts.forEach(function (facet) {
1039
+ 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) {
1040
+ return (0,_babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_3__["default"])({}, count.value, count.count);
1041
+ }))))));
1042
+ });
1043
+ }
1032
1044
  return adaptedResult;
1033
1045
  }
1034
1046
  }, {
1035
1047
  key: "_adaptFacetStats",
1036
1048
  value: function _adaptFacetStats(typesenseFacetCounts) {
1037
1049
  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
- });
1050
+ if (Array.isArray(typesenseFacetCounts)) {
1051
+ typesenseFacetCounts.forEach(function (facet) {
1052
+ if (facet.stats && Object.keys(facet.stats).length > 0) {
1053
+ Object.assign(adaptedResult, (0,_babel_runtime_helpers_defineProperty__WEBPACK_IMPORTED_MODULE_3__["default"])({}, facet.field_name, facet.stats));
1054
+ }
1055
+ });
1056
+ }
1043
1057
  return adaptedResult;
1044
1058
  }
1045
1059
  }, {
@@ -1052,7 +1066,7 @@ var SearchResponseAdapter = /*#__PURE__*/function () {
1052
1066
  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
1067
  adaptedResult.facetOrdering = adaptedResult.facetOrdering || {};
1054
1068
  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) {
1069
+ adaptedResult.facetOrdering.facets.order = (0,_babel_runtime_helpers_toConsumableArray__WEBPACK_IMPORTED_MODULE_2__["default"])(new Set((Array.isArray(typesenseFacetCounts) ? typesenseFacetCounts : []).map(function (fc) {
1056
1070
  return fc["field_name"];
1057
1071
  }).concat(this.allTypesenseResults.map(function (r) {
1058
1072
  return r.facet_counts || [];
@@ -1074,15 +1088,20 @@ var SearchResponseAdapter = /*#__PURE__*/function () {
1074
1088
  key: "adapt",
1075
1089
  value: function adapt() {
1076
1090
  var adaptedRenderingContent = this._adaptRenderingContent(this.typesenseResponse.facet_counts || []);
1091
+
1092
+ // For union search, use union_request_params, otherwise use request_params
1093
+ var requestParams = this.typesenseResponse.union_request_params ? this.typesenseResponse.union_request_params[0] : this.typesenseResponse.request_params;
1077
1094
  var adaptedResult = _objectSpread({
1078
1095
  hits: this.typesenseResponse.grouped_hits ? this._adaptGroupedHits(this.typesenseResponse.grouped_hits) : this._adaptHits(this.typesenseResponse.hits),
1079
1096
  nbHits: this.typesenseResponse.found,
1080
- page: this.typesenseResponse.page - 1,
1097
+ page: this.typesenseResponse.union_request_params ? this.typesenseResponse.page // Union search already uses 0-based page
1098
+ : this.typesenseResponse.page - 1,
1099
+ // Regular search uses 1-based page, convert to 0-based
1081
1100
  nbPages: this._adaptNumberOfPages(),
1082
- hitsPerPage: this.typesenseResponse.request_params.per_page,
1101
+ hitsPerPage: (requestParams === null || requestParams === void 0 ? void 0 : requestParams.per_page) || 10,
1083
1102
  facets: this._adaptFacets(this.typesenseResponse.facet_counts || []),
1084
- facets_stats: this._adaptFacetStats(this.typesenseResponse.facet_counts || {}),
1085
- query: this.typesenseResponse.request_params.q,
1103
+ facets_stats: this._adaptFacetStats(this.typesenseResponse.facet_counts || []),
1104
+ query: (requestParams === null || requestParams === void 0 ? void 0 : requestParams.q) || "",
1086
1105
  processingTimeMS: this.typesenseResponse.search_time_ms
1087
1106
  }, Object.keys(adaptedRenderingContent).length > 0 ? {
1088
1107
  renderingContent: adaptedRenderingContent
@@ -1094,6 +1113,11 @@ var SearchResponseAdapter = /*#__PURE__*/function () {
1094
1113
  adaptedResult.userData = this._adaptUserData(this.typesenseResponse.metadata);
1095
1114
  }
1096
1115
 
1116
+ // Add parsed_nl_query if natural language search was used
1117
+ if (this.typesenseResponse.parsed_nl_query) {
1118
+ adaptedResult.parsed_nl_query = this.typesenseResponse.parsed_nl_query;
1119
+ }
1120
+
1097
1121
  // If no results were found for the search, but there is still a conversation response,
1098
1122
  // still send that as a hit so the conversation is accessible via Instantsearch
1099
1123
  if (this.fullTypesenseResponse.conversation && adaptedResult.hits.length === 0) {
@@ -1127,7 +1151,10 @@ var utils = {
1127
1151
  return value.replace(new RegExp("<mark>", "g"), highlightPreTag || "<mark>").replace(new RegExp("</mark>", "g"), highlightPostTag || "</mark>");
1128
1152
  },
1129
1153
  _adaptNumberOfPages: function _adaptNumberOfPages() {
1130
- var result = this.typesenseResponse.found / this.typesenseResponse.request_params.per_page;
1154
+ // For union search, use union_request_params, otherwise use request_params
1155
+ var requestParams = this.typesenseResponse.union_request_params ? this.typesenseResponse.union_request_params[0] : this.typesenseResponse.request_params;
1156
+ var perPage = (requestParams === null || requestParams === void 0 ? void 0 : requestParams.per_page) || 10;
1157
+ var result = this.typesenseResponse.found / perPage;
1131
1158
  if (Number.isFinite(result)) {
1132
1159
  return Math.ceil(result);
1133
1160
  } else {
@@ -10003,7 +10030,7 @@ var TypesenseInstantsearchAdapter = /*#__PURE__*/function () {
10003
10030
  value: function () {
10004
10031
  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
10032
  var _this2 = this;
10006
- var typesenseResponse, adaptedResponses;
10033
+ var typesenseResponse, responseAdapter, adaptedResponse, adaptedResponses, _adaptedResponses;
10007
10034
  return _babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_3___default().wrap(function _callee$(_context) {
10008
10035
  while (1) switch (_context.prev = _context.next) {
10009
10036
  case 0:
@@ -10012,25 +10039,48 @@ var TypesenseInstantsearchAdapter = /*#__PURE__*/function () {
10012
10039
  return this._adaptAndPerformTypesenseRequest(instantsearchRequests);
10013
10040
  case 3:
10014
10041
  typesenseResponse = _context.sent;
10015
- adaptedResponses = typesenseResponse.results.map(function (typesenseResult, index) {
10042
+ if (!typesenseResponse.union_request_params) {
10043
+ _context.next = 12;
10044
+ break;
10045
+ }
10046
+ // Handle union search response - single unified response
10047
+ this._validateTypesenseResult(typesenseResponse);
10048
+ responseAdapter = new _SearchResponseAdapter__WEBPACK_IMPORTED_MODULE_7__.SearchResponseAdapter(typesenseResponse, instantsearchRequests[0],
10049
+ // Use first request as base
10050
+ this.configuration, [typesenseResponse],
10051
+ // Pass single response as allTypesenseResults
10052
+ typesenseResponse);
10053
+ adaptedResponse = responseAdapter.adapt(); // InstantSearch expects one result per request, so return the same adapted response for each request
10054
+ adaptedResponses = instantsearchRequests.map(function () {
10055
+ return adaptedResponse;
10056
+ });
10057
+ return _context.abrupt("return", {
10058
+ results: adaptedResponses
10059
+ });
10060
+ case 12:
10061
+ // Handle regular multi-search response - multiple separate responses
10062
+ _adaptedResponses = typesenseResponse.results.map(function (typesenseResult, index) {
10016
10063
  _this2._validateTypesenseResult(typesenseResult);
10017
10064
  var responseAdapter = new _SearchResponseAdapter__WEBPACK_IMPORTED_MODULE_7__.SearchResponseAdapter(typesenseResult, instantsearchRequests[index], _this2.configuration, typesenseResponse.results, typesenseResponse);
10018
10065
  var adaptedResponse = responseAdapter.adapt();
10019
10066
  return adaptedResponse;
10020
10067
  });
10021
10068
  return _context.abrupt("return", {
10022
- results: adaptedResponses
10069
+ results: _adaptedResponses
10023
10070
  });
10024
- case 8:
10025
- _context.prev = 8;
10071
+ case 14:
10072
+ _context.next = 20;
10073
+ break;
10074
+ case 16:
10075
+ _context.prev = 16;
10026
10076
  _context.t0 = _context["catch"](0);
10027
10077
  console.error(_context.t0);
10028
10078
  throw _context.t0;
10029
- case 12:
10079
+ case 20:
10030
10080
  case "end":
10031
10081
  return _context.stop();
10032
10082
  }
10033
- }, _callee, this, [[0, 8]]);
10083
+ }, _callee, this, [[0, 16]]);
10034
10084
  }));
10035
10085
  function searchTypesenseAndAdapt(_x) {
10036
10086
  return _searchTypesenseAndAdapt.apply(this, arguments);