#Nextra - Search REST API implementation

43 messages · Page 1 of 1 (latest)

mint creek
#

I'm currently working on a Nextra project where i store my documentation, from my understanding, Nextra uses Flexsearch (by default, i'm not using any custom search system) to perform searches.

My plan is to have a rest api such as mywiki/api/search?query=something that would return the URLs for the top 5 results. While i've successfully setup the api endpoint, i'm at a loss as to how to import the Flexsearch into my api. Once that is imported i belive it's as easy as calling index.search(query) but i cannot find any documentation anywhere on how to achieve that.

loud abyssBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

mint creek
#

@craggy ridge

#

I'm using the default nextra search that i belive is Flexsearch

#

the search itself is working perfectly well within the wiki itself, i just want an api endpoint to get search results from

craggy ridge
#

One sec…

#

Let me read up on flex search

mint creek
#

In short, i just want to pass discord messages to it and return top 5 results within discord

#

i have all the steps covered, except on how to actually search 😅

#
export default async function handler(
    req: NextApiRequest,
    res: NextApiResponse
) {
    await runMiddleware(req, res, cors);
    const { query } = req.query;

    if (req.method === 'GET') {
        if(!query) return res.status(400).json({ status: 'error', error: 'Query is required' });
        //search inside documentation using nextra
        const results = [query];
        
        
        res.status(200).json({ status: 'success', data: results });
    } else {
        res.status(405).json({ status: 'error', error: 'Method not allowed' });
    }
}
#

not much point in this code but ye, i need to import the flexsearch instance in here somehow

craggy ridge
#

when you say import flexsearch

#

is there an instance you've already created of it somewhere?

#

anyways the way I believe this would be done

mint creek
#

that is the whole root of the issue

#

nextra is a black box i have no clue where it initializes flexsearch

craggy ridge
#

is that you need to import/export an index containing your flexsearch data

mint creek
#

if i could get the flexsearch instance there would be no problem, the library is pretty easy to use

craggy ridge
#

__nextra_internal__.flexsearch

#

idk if this is directly relevant

#

but I would begin by searching through the nextra codebase and figuring out how they interact with flexsearch

mint creek
#

i don't see anything in the server side globalThis

craggy ridge
#

this config option may also be helpful

#

you may need to fork this repo even

#

and try to hack in a way to get a flexsearch reference

#

good luck figuring this out!

mint creek
#

@craggy ridge i got something
this doesen't work globalThis.__nextra_internal__
but this does const __nextra_internal__ = globalThis[Symbol.for('__nextra_internal__')]
thanks for the tip, now to hunt if i can actually do something with it

craggy ridge
#

Nice!

#

no problem

#

if you figure this out, I'd love it if you could be ping me with the final solution 😄

#

i might have to do something similar at some point

mint creek
#

ye, digging into the nextra internal atm

#

unfortunatelly i only found the flexsearch options, which is what was in your first finding

#

the key is probably in the second section of code you linked, but i have no clue how to grab that

mint creek
#

i haven't found a way to access it any other way

#

if only this comment was implemented lol // This can be global for better caching.

mint creek
#

@craggy ridge as a closing to this, i basically had to reimplement the whole flexsearch api-side

craggy ridge
#

Ahhh

acoustic palm