ThaqalaynAPI icon indicating copy to clipboard operation
ThaqalaynAPI copied to clipboard

Better query

Open AAR072 opened this issue 1 year ago • 4 comments

Salam, I am really thankful that you created this project. May Allah reward you for your efforts. I have a feature request that I am willing to assist and contribute too. It's a simple idea. Currently you can query for exact text matches. I want to implement a way to have multiple queries at once and only keep the overlapping ones. In this way, I can search for wudhu and break at the same time, without having to only search for the string "break wudhu". It would ideally return all responses that contain wudhu and break.

AAR072 avatar Apr 28 '24 17:04 AAR072

Looking at the code, it seems like you have a function to strop regex. Could avoiding calling that work? I would love to play around with this but I'm having some trouble setting up the environment and therefore no way to actually run it

AAR072 avatar Apr 29 '24 15:04 AAR072

Salam, thank you for the issue. I tried to add documentation on how to setup locally. I added ability for multiple queries. Currently multiple queries use pipe operator to match either query 1 or query 2. Will look further into adding ability for multiple queries where match has both strings contained.

MohammedArab1 avatar May 02 '24 03:05 MohammedArab1

Ws. I have drafted a way to do what I proposed earlier. I wanted to ask for your opinion before I did anything.

Currently the query handler function has this code which is relevant

const escapedQuery = utils.escapeRegExp(query);
const $regex = new RegExp(escapedQuery, "i");
const englishQueryResults = await model.find(
{ englishText: { $regex } },
{ _id: 0, __v: 0 },);

I propose that it is slightly tweaked to this:

      // Split the query by "|" to get individual queries
      const queries = query.split('|');
      // Map each query to its escaped form
      const escapedQueries = queries.map(utils.escapeRegExp);
      // Join escaped queries with "|" to form a regex pattern
      const combinedQuery = escapedQueries.join('|');
      // Create the regex object
      const $regex = new RegExp(combinedQuery, "i");

      const englishQueryResults = await model.find(
        { englishText: { $regex } },
        { _id: 0, __v: 0 },
      );

Doing some hacky local testing seems to show the modification works. EGrQBeA

Inshallah you will find this is a good and working feature. If there are any concerns, feel free to let me know. Salam

AAR072 avatar May 03 '24 14:05 AAR072

Thank you for the code sample. A couple of things:

  1. From what I see, it does not make much sense to have the following code:
const queries = query.split("|")
const escapedQueries = queries.map(escapeRegExp)
const combinedQuery = escapedQueries.join("|")

We can just do escapedQuery = escapeRegExp(query) since the escapeRegExp function does not escape pipe character.

  1. I'm not sure this is doing what you want it to do. When we query (ex. "wudhu|break|ziyarat") the database with the regex you created (new RegExp($regex, "gi")), this will match all hadiths that have either wudhu, break, OR ziyarat in the content. In your request you asked specifically for the ability to query based on an AND parameter. I'm looking more into that right now but I can't guarantee I'll add anything anytime soon.

MohammedArab1 avatar May 03 '24 18:05 MohammedArab1