typesense-instantsearch-adapter 2.9.0-4 → 2.9.0-6

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
@@ -21,6 +21,7 @@ Here is an example of UI you can build with this adapater: [songs-search.typesen
21
21
  - [With vue-instantsearch](#with-vue-instantsearch)
22
22
  - [With angular-instantsearch](#with-angular-instantsearch)
23
23
  - [Widget Specific Instructions](#widget-specific-instructions)
24
+ - [Caching](#caching)
24
25
  - [Compatibility](#compatibility)
25
26
  - [Development](#development)
26
27
  - [Help](#help)
@@ -604,38 +605,70 @@ const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
604
605
  // from https://github.com/typesense/showcase-hn-comments-semantic-search/blob/8a33006cae58b425c53f56a64e1273e808cd9375/src/js/index.js#L101
605
606
  const searchClient = typesenseInstantsearchAdapter.searchClient;
606
607
  search = instantsearch({
607
- searchClient,
608
- indexName: INDEX_NAME,
609
- routing: true,
610
- async searchFunction(helper) {
611
- // This fetches 200 (nearest neighbor) results for semantic / hybrid search
612
-
613
- let query = helper.getQuery().query;
614
- const page = helper.getPage(); // Retrieve the current page
615
-
616
- if (
617
- query !== "" &&
618
- ["semantic", "hybrid"].includes($("#search-type-select").val())
619
- ) {
620
- console.log(helper.getQuery().query);
621
- helper
622
- .setQueryParameter(
623
- "typesenseVectorQuery", // <=== Special parameter that only works in typesense-instantsearch-adapter@2.7.0-3 and above
624
- `embedding:([], k:200)`,
625
- )
626
- .setPage(page)
627
- .search();
628
- console.log(helper.getQuery().query);
629
- } else {
630
- helper
631
- .setQueryParameter("typesenseVectorQuery", null)
632
- .setPage(page)
633
- .search();
634
- }
635
- },
636
- });
608
+ searchClient,
609
+ indexName: INDEX_NAME,
610
+ routing: true,
611
+ async searchFunction(helper) {
612
+ // This fetches 200 (nearest neighbor) results for semantic / hybrid search
613
+
614
+ let query = helper.getQuery().query;
615
+ const page = helper.getPage(); // Retrieve the current page
616
+
617
+ if (query !== "" && ["semantic", "hybrid"].includes($("#search-type-select").val())) {
618
+ console.log(helper.getQuery().query);
619
+ helper
620
+ .setQueryParameter(
621
+ "typesenseVectorQuery", // <=== Special parameter that only works in typesense-instantsearch-adapter@2.7.0-3 and above
622
+ `embedding:([], k:200)`,
623
+ )
624
+ .setPage(page)
625
+ .search();
626
+ console.log(helper.getQuery().query);
627
+ } else {
628
+ helper.setQueryParameter("typesenseVectorQuery", null).setPage(page).search();
629
+ }
630
+ },
631
+ });
637
632
  ```
638
633
 
634
+ ## Caching
635
+
636
+ There are two modes of caching:
637
+
638
+ 1. **Server-side caching:**
639
+
640
+ To enable server-side caching, add a parameter called `useServerSideSearchCache: true` in the `server` configuration block of the typesense-instantsearch-adapter like this:
641
+
642
+ ```javascript
643
+ const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
644
+ server: {
645
+ apiKey: "...",
646
+ nearestNode: {...},
647
+ nodes: [...],
648
+ useServerSideSearchCache: true // <<< Add this to send use_cache as a query parameter instead of post body parameter
649
+ },
650
+ additionalSearchParameters: {...}
651
+ });
652
+ ```
653
+
654
+ This will cause the adapter to add `?use_cache=true` as a URL query parameter to all search requests initiated by the adapter, which will then cause Typesense Server to enable server-side caching for these requests.
655
+
656
+ 2. **Client-side caching:**
657
+
658
+ The adapter also has client-side caching enabled by default, to prevent unnecessary network calls to the server. The TTL for this client-side cache can be configured like this:
659
+
660
+ ```javascript
661
+ const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
662
+ server: {
663
+ apiKey: "...",
664
+ nearestNode: {...},
665
+ nodes: [...],
666
+ cacheSearchResultsForSeconds: 2 * 60 // <<< Add this to configure the TTL for client-side cache in the browser
667
+ },
668
+ additionalSearchParameters: {...}
669
+ });
670
+ ```
671
+
639
672
  ## Compatibility
640
673
 
641
674
  | Typesense Server | typesense-instantsearch-adapter | instantsearch.js | react-instantsearch | vue-instantsearch | angular-instantsearch |
@@ -655,9 +688,7 @@ If a particular version of the above libraries don't work with the adapter, plea
655
688
 
656
689
  ### Widget Compatibility
657
690
 
658
- This adapter works with all widgets in [this list](https://www.algolia.com/doc/api-reference/widgets/js/), _except_ for the following:
659
-
660
- - `queryRuleContext`
691
+ This adapter works with all widgets in [this list](https://www.algolia.com/doc/api-reference/widgets/js/)
661
692
 
662
693
  ## Development
663
694
 
@@ -590,6 +590,11 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
590
590
  }
591
591
  }).join(",");
592
592
  }
593
+ }, {
594
+ key: "_adaptRulesContextsToOverrideTags",
595
+ value: function _adaptRulesContextsToOverrideTags(ruleContexts) {
596
+ return ruleContexts.join(",");
597
+ }
593
598
  }, {
594
599
  key: "_buildSearchParameters",
595
600
  value: function _buildSearchParameters(instantsearchRequest) {
@@ -627,13 +632,16 @@ var SearchRequestAdapter = /*#__PURE__*/function () {
627
632
  max_facet_values: params.maxValuesPerFacet,
628
633
  page: (params.page || 0) + 1
629
634
  });
630
- if (params.hitsPerPage) {
635
+ if (params.hitsPerPage != null) {
631
636
  typesenseSearchParams.per_page = params.hitsPerPage;
632
637
  }
633
638
  if (params.facetQuery) {
634
639
  typesenseSearchParams.facet_query = "".concat(params.facetName, ":").concat(params.facetQuery);
635
640
  typesenseSearchParams.per_page = 0;
636
641
  }
642
+ if (params.ruleContexts && params.ruleContexts.length > 0) {
643
+ typesenseSearchParams.override_tags = this._adaptRulesContextsToOverrideTags(params.ruleContexts);
644
+ }
637
645
 
638
646
  // If a custom vector query is specified, set q=*
639
647
  if (params.typesenseVectorQuery) {