question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

This is a proposed special attribute that allows you to fuzzy match a property. Possibly even multiple properties and/or nested documents.

Suggested syntax:

name: {
  $search: ['alice', 'Alice', 'bo', /$bob/i]
}

Following similar syntax to our other special query filters, this would allow you to filter by a singular value, multiple values (treated like an or) and/or regular expressions directly.

For knex we’d have to manually construct OR queries so it would get a little messy because in order to do LIKE queries the syntax is:

knex('users').where('name', 'like', '%Test%');

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
DaddyWarbuckscommented, Mar 11, 2022

Here are a couple examples of how I typically handle this in both Sequelize and Mongo/Mongoose

Sequelize

export const withSearch = (searchProps, idProps) => (context) => {
  if (!context.params.query) {
    return context;
  }

  const { $search, ...query } = context.params.query;

  if (!$search) {
    context.params.query = query;
    return context;
  }

  const $or = [];

  searchProps.forEach((prop) => {
    $or.push({ [prop]: { $iLike: `%${$search}%` } });
  });

  if (idProps && isValidInteger($search)) {
    idProps.forEach((prop) => {
      $or.push({ [prop]: $search });
    });
  }

  context.params.query = {
    ...query,
    $or: [...(query.$or || []), ...$or]
  };

  return context;
};

Mongo/Mongoose

const traverse = require('traverse');

// https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
const escapeRegExp = (string) => {
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
};

module.exports.searchRegex = (stringProps, numberProps) => (context) => {
  if (!context.params.query) {
    return context;
  }

  // TODO: Deepclone? Or is traverse immutable?
  const query = { ...context.params.query };
  const $search = query.$search;
  delete query.$search;

  // Find and replace any `$search` properties, even when
  // nested in query or query arrays
  traverse(query).forEach(function (value) {
    if (this.parent && this.parent.node.$search !== undefined) {
      this.parent.node.$regex = new RegExp(escapeRegExp(value), 'i');
      delete this.parent.node.$search;
    }
  });

  if ($search && stringProps) {
    query.$or = query.$or || [];
    stringProps.forEach((prop) => {
      query.$or.push({
        [prop]: { $regex: new RegExp(escapeRegExp($search), 'i') }
      });
    });
  }

  if ($search && !isNaN(Number($search)) && numberProps) {
    query.$or = query.$or || [];
    numberProps.forEach((prop) => {
      query.$or.push({
        [prop]: Number($search)
      });
    });
  }

  context.params.query = query;

  return context;
};
1reaction
strarsiscommented, Mar 12, 2022

@DaddyWarbucks: Thanks again! I made a hook for KnexJS FeathersJS adapter (q to like query): https://github.com/josx/ra-data-feathers/issues/175#issuecomment-1065925854

Read more comments on GitHub >

github_iconTop Results From Across the Web

Google Help
Choose a Google product. Google Chrome · Google Account · YouTube · Gmail · Google Play · Google Search · Google AdSense ·...
Read more >
Google SEO Help and Support | Google Search Central
Get SEO support from Google Search Specialists. We can help with search appearance and rankings, rich results, security, Search Console, and more.
Read more >
Official Apple Support
Apple support is here to help. Learn more about popular topics and find resources that will help you with all of your Apple...
Read more >
Customer Support Search by Swiftype
Provide a search experience that intelligently displays results and significantly reduces the number of inbound support tickets. Autocomplete.
Read more >
Zendesk Support search reference
Search operators; Ticket property keywords; User property keywords ... Administrators can search all the data in Zendesk Support.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found