#community-help

Debugging Search Issue with Typesense Server Caching

TLDR Jameshwart reported experiencing caching issues with typesense server, despite the server's default setting of not caching. Through troubleshooting steps with Kishore Nallan, they were able to confirm an issue with the Typesense Javascript client and resolved it by adding cacheSearchResultsForSeconds: 0 after apiKey in the initialisation. Laura also participated in discussion, learning about server and client level caching.

Powered by Struct AI
Jul 12, 2023 (5 months ago)
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:18 AM
hi guys, how do you search without cache?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:19 AM
Cache is not enabled by default. This is for the typesense server.
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:21 AM
I have a problem with the following scenario
1. Woo updates ts
2. Once update successfull
3. woo then send a revalidation request to our next app
4. next app queries data to ts but the problem is the cache. The response is a cache version.
so I am checking if there is a way to use the search without getting cache response
06:23
Jameshwart
06:23 AM
I can see cache_ttl is 60 seconds. by then next tries to regenerate the page but since it is within 60 seconds it generates a cache page.
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:24 AM
Unless you set a cache paramter to your search request we don't cache anything.
06:24
Kishore Nallan
06:24 AM
Are you querying a single node or cluster?
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:24 AM
yes I single cluster.
06:25
Jameshwart
06:25 AM
even in my local dev it shows the cache data unless 60 seconds passes after the ts data update
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:26 AM
What client are you using?
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:26 AM
typesense client
06:32
Jameshwart
06:32 AM
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:32 AM
That's the server
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:33 AM
sorry. I thought that's the client. I was using that for getting data using the search method
06:36
Jameshwart
06:36 AM
im pretty sure I am using that. I thought that is the typesense client.
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:36 AM
What language are you using?
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:36 AM
js
06:36
Jameshwart
06:36 AM
with react
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:36 AM
Maybe you can post a small example that reproduces the issue.
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:38 AM
that would be hard. ahahha
06:40
Jameshwart
06:40 AM
Kishore Nallan i really think it is affected by cache_ttl
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:40 AM
It's difficult for me to debug without a reproduceable example. If it's just a cache TTL related issue then it should be easy to reproduce as a standalone example.
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:42 AM
you need the query right?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:42 AM
No, a small example program where you create a collection, index data and query it to show how the data is not reflecting the update
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:43 AM
it would be hard for me because the update is from woo. What I can do is show you a video. detailing the steps. Would that help?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:45 AM
What happens if you added a sleep of 5 seconds to your next app query? Does it only work if you wait 60 seconds exactly?
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:47 AM
i can't say its exactly 60 seconds because what I do is
1. run next.js app on dev env and visit a product.
2. update the product with small change
3. then i reload the dev page i open in step 1
4. page is not updated
5. wait for a minute
6. reload the product page again. This time it will show the updated data
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:48 AM
Instead of waiting for 1 min can you try refreshing after few seconds?
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:48 AM
yeah okay
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:48 AM
Also check typesense log to see what happens in that interval
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:51 AM
how can I see ts log?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:51 AM
How are you running Typensese?
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:51 AM
your service. Cloud typesense
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:52 AM
On cloud that's not possible but you said you can reproduce this locally right? For local also you are using cloud?
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:52 AM
I did reload the page after i updated woo. a few times and it did not update right away
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:52 AM
Please DM me your cluster ID
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:53 AM
yes for my local site I am using cloud api keys
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:55 AM
Your cluster looks fine. Are you making a query directly from your JS in the browser or through your backend node.js?
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:56 AM
I believe next.js getStaticProps is a node.js.
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:56 AM
Are you sure there are no other caches anywhere else in your app
06:57
Kishore Nallan
06:57 AM
Can you paste the exact query your app sends to typesense?
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:57 AM
inside the get static props I use typesense libaray to get the data.
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:57 AM
If it's made from frontend, you can get it from network inspect on your browser console.
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
06:58 AM
this is the search params codes I have.
const getProductBySlug = async (sku: string): Promise<ITSProduct | null> => {
    let searchParameters = {
        q: '*',
        query_by: 'slug',
        filter_by: `slug:=[${sku}]`,
        sort_by: '_text_match:desc',
        facet_by: 'slug',
    };

    const results = await client
        .collections(TS_CONFIG.collectionNames.product)
        .documents()
        .search(searchParameters);

    let found = null;
    if (typeof results.hits !== 'undefined' && results.hits[0]) {
        const tsProductData: ITSProduct = JSON.parse(
            JSON.stringify(results.hits[0].document)
        );
        return tsProductData;
    }
    return found;
};
06:59
Jameshwart
06:59 AM
its not in the froned since it is done via getstaticprops.
06:59
Jameshwart
06:59 AM
how can I log the query using your ts library?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:00 AM
Can you show me how client is initialized?
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
07:01 AM
This is how we initialize it.
import { Client } from 'typesense';
import TS_CONFIG from './config';

const c = new Client(TS_CONFIG.server);

export default c;

give me a sec i'll get you the ts_config
07:01
Jameshwart
07:01 AM
const TS_CONFIG = {
    server: {
        apiKey: process.env.NEXT_PUBLIC_TS_APIKEY as string, // Be sure to use an API key that only allows search operations
        nodes: [
            {
                host: process.env.NEXT_PUBLIC_TS_HOST as string,
                path: '', // Optional. Example: If you have your typesense mounted in localhost:8108/typesense, path should be equal to '/typesense'
                port: 443,
                protocol: 'https',
            },
        ],
    },
    collectionNames: {
        product: `product-${process.env.NEXT_PUBLIC_TS_STORE_ID}`,
        taxonomy: `taxonomy-${process.env.NEXT_PUBLIC_TS_STORE_ID}`,
        page: `page-${process.env.NEXT_PUBLIC_TS_STORE_ID}`,
    },
};

export default TS_CONFIG;

this is the TS_CONFIG
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:02 AM
Can you try adding cacheSearchResultsForSeconds: 0 after apiKey
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
07:03 AM
okay testing now
07:04
Jameshwart
07:04 AM
yes it works immediately
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:05 AM
Ok so the JS client alone seems to have this configuration but according to this comment it is disabled by default: https://github.com/typesense/typesense-js/issues/52#issuecomment-834863277
07:05
Kishore Nallan
07:05 AM
Are you using instantsearch adapter?
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
07:06 AM
yes for the actual website search but for this query no need to use it.
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:06 AM
Somehow it was being enabled. Will take a look, but your problem is now solved, correct?
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
07:09 AM
yeah I just need to disable it on this query
Jul 13, 2023 (5 months ago)
Laura
Photo of md5-e31442d4d6c849124582a10193fe7721
Laura
12:20 PM
hi guys, I saw this trend and I am also implementing caching in my search, but I defined it within my search preset, using the query parameters, is it necessary to put it also in the client initialization to work correctly, from what I saw here? I got this doubt now
12:21
Laura
12:21 PM
I am setting the search parameters here and using as preset in my code
Image 1 for I am setting the search parameters here and using as preset in my code
12:22
Laura
12:22 PM
Image 1 for
12:22
Laura
12:22 PM
Image 1 for
Jameshwart
Photo of md5-283d3397742c32cc2b6be6bda508c03e
Jameshwart
12:22 PM
That might just work. Laura.
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
12:23 PM
The Instant search adapter has a client level cache. That's what the client level initialisation param enables.
12:24
Kishore Nallan
12:24 PM
What you define in search preset is server side caching.
Laura
Photo of md5-e31442d4d6c849124582a10193fe7721
Laura
12:30 PM
Okay, so from what I understand, I am implementing the caching only on the server side with the search parameters, but I also need to implement it on the client side in my typesense initialization in the code?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
12:32 PM
You can do either or both as per your requirement 🙂
Laura
Photo of md5-e31442d4d6c849124582a10193fe7721
Laura
12:33 PM
Okay! Thanks

Typesense

Lightning-fast, open source search engine for everyone | Knowledge Base powered by Struct.AI

Indexed 3015 threads (79% resolved)

Join Our Community

Similar Threads

Troubleshooting TypeScript Error with Typesense

GM experienced an error with Typesense in TypeScript, requiring help to correct the issues. Jason helped propose solutions and adjustments to the code. Ultimately, they were able to resolve the errors and successfully implement a search function.

4

38
14mo

Enhancing Typesense-JS For Improved Cache Management

Jordan requested a way to clear the cache in 'typesense-js' when a new document is added. Jason recommended a work-around and supported Jordan's idea to add a 'clearCache' method. Jordan then submitted a PR for this implementation.

1

10
16mo

Issues with Typesense Frontend Search Integration

Akash experienced issues integrating Typesense into their frontend search, particularly with their search box. Jason guided them to troubleshoot, eventually identifying the issue to be with react-instantsearch-dom and suggesting a downgrade to react 17, which resolved the issue.

2

52
14mo

Resolving Typesense Analytics "top_queries" Collection Issue

Zain faced issues with typesense analytics and an empty "top_queries" collection. Jason and Kishore Nallan assisted in troubleshooting, identified a bug, and suggested a fix in `0.25.0.rc66`. The fix worked for Zain.

3

29
4mo

Troubleshooting Typesense Setup and Understanding Facets and Keywords

Demitri encountered errors when exploring Typesense for the first time. Jason guided them through troubleshooting and discussed facets, keyword settings, and widget configurations. Helin shared a Python demo app and its source code to help Demitri with their project.

1

56
21mo