e-Spirit / javascript-content-api-library

Apache License 2.0
7 stars 11 forks source link

Allow for options in $regex filter #204

Open re-kfa opened 3 months ago

re-kfa commented 3 months ago

Hi, currently it is not possible to apply options to a regexp when using the fetchByFilter method.

The reason is that the value is always treated as string and not as a regex.

So this is not working:

await fsxaApi.fetchByFilter({
  filters: [
    {
      field: 'formData.st_searchterms.value',
      operator: '$regex',
      value: "/Suchbegriff(,|$){1}/i",
    },
  ],
});

The query builder renders this url: &filter={"formData.st_searchterms.value":{"$regex":"/Suchbegriff(,|$){1}/i"}}

But for the Mongodb to correctly interpret the regexp the quotes need to be omitted: &filter={"formData.st_searchterms.value":{"$regex":/Suchbegriff(,|$){1}/i}}

Alternatively it is possible to work with this syntax according to the MongoDB-Documentation: &filter={"formData.st_searchterms.value":{"$regex":"OnlineService(,|$){1}", "$options": "i"}}

So possibly allowing an option property in the filter object would also help here.

Thanks!

neo-reply-lukas commented 3 months ago

Hello,

there is a workaround for this Problem: additionalParams You could pass the query as an additionalParam and specify your $options there. Note, that additionalParams require the original Restheart Filter Syntax.

On another Note: Querying large DBs with Regular Expressions might be slow. If possible narrow the result down by passing another index based filter. e.g. You know you only want to look for a specific EntityType? Filter for that, since it's indexed hence fast.

LarsOlt commented 3 months ago

Hello,

there is a workaround for this Problem: additionalParams You could pass the query as an additionalParam and specify your $options there. Note, that additionalParams require the original Restheart Filter Syntax.

On another Note: Querying large DBs with Regular Expressions might be slow. If possible narrow the result down by passing another index based filter. e.g. You know you only want to look for a specific EntityType? Filter for that, since it's indexed hence fast.

Can you add this to the documentation and provide a code example? I am also trying to get a case-insensitive regex to work.

LarsOlt commented 3 months ago

This works for me:


fsxaApi.fetchByFilter({
      // ...
        additionalParams: {
            filter: {
                'routes.route': {
                    $regex: relativeUrl,
                    $options: 'i',
                },
            },
        },
    });
jodeleit-es commented 3 months ago

Thank you for reaching out to us. We appreciate your feedback and understand how important clear instructions are. We’re glad to hear that you were able to figure it out despite the lack of documentation. We encourage you to contribute directly to our documentation. If you have the time, would you consider creating a PR for this? Your firsthand experience would be invaluable in improving our resources for everyone.