How can I use query.fullText()?

Hi Moralis team!

Thanks for your Moralis service.

I am now building a new Dapp and want to search some text.
So I used query.fullText statement in the cloud function api.
But it returns an issue as below.

MongoError: text index required for $text query
    at MessageStream.messageHandler (/moralis-server/node_modules/mongodb/lib/cmap/connection.js:268:20)
    at MessageStream.emit (node:events:527:28)
    at MessageStream.emit (node:domain:475:12)
    at processIncomingData (/moralis-server/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/moralis-server/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at writeOrBuffer (node:internal/streams/writable:389:12)
    at _write (node:internal/streams/writable:330:10)
    at MessageStream.Writable.write (node:internal/streams/writable:334:10)
    at Socket.ondata (node:internal/streams/readable:754:22)
    at Socket.emit (node:events:527:28)
    at Socket.emit (node:domain:475:12)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
    at TCP.callbackTrampoline (node:internal/async_hooks:130:17)

How can I fix this issue?

Thanks

There are some similar forum threads, I don’t remember the solution now, there is a solution with adding an index, one with using regexp.

Hi @cryptokid!

Thanks for your reply.

You mean this thread?

Yes, that is one of them.

I am using fullText on cloud function and trying to creat index.
But query.createIndex is not working.

You have to connect directly to mongo db to create that index

I used this codes, but not working.
Can you figure out the reason?

const pipeline = [    
        // join to Token collection on token_address
        {
            lookup: {
                from: 'TblUsers',
                localField: 'deployer',
                foreignField: 'address',
                as: 'address'
            },
        },
        match: {
                field: {
                    $regex: `/${keyword}/g`
                }
            }
    ];

maybe you have to use normal quotes here

I used above code, but it occurs exception, too. :disappointed_relieved:

TypeError: /${keyword}/g is not a function
    at eval (eval at customUserPlugin (/moralis-server/cloud/main.js:157:21), <anonymous>:1:10871)
    at /moralis-server/lib/Routers/FunctionsRouter.js:264:16
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

I didn’t post it right, I just quoted the original code

try to use a constant for that keyword to see if it works that way

Is it possible to connect to database directly on cloud functions?
If possible, can you send me some example codes?

you can not connect directly to the database from cloud code, you can connect directly to the database from your local computer
https://docs.moralis.io/moralis-dapp/database/direct_access

Then, is there any method to query datas which certain colume contains substring?

yes, regexp works, and that way of adding an index, you add the index after you connect directly to mongo db from your local computer, you only have to add it once

I tried with regexp method.
I used this code to get the records which contains substring, but this is not working.

$regex: `/${substring}/g`

And I tried to create index on client side to use fullText statement.
But on cloud function side, if I use query.fullText(), it returns an exception and the exception is below.

{"ok":0,"code":27,"codeName":"IndexNotFound","name":"MongoError"}

For first try with regexp, did you try with hardcoded values first instead of variables?

For the second part, you connected to mongodb directly and you created an index?

I tried with hardcoded regexp, but regexp was not working.
And I tried this code to creat index.

const database = client.db("parse")
const collection = database.collection("TblColllection")

await collection.createIndex({ tokenURIData: "text" })

And I used fullText on cloud function like this.

query.fullText('tokenURIData', 'xxx');

Can you list the indexes now?
Was there any error while creating that index?

There was no exception while I was creating index.

Maybe the createIndex function could return some error message