Merge pull request #2855 from payloadcms/fix/#2832

fix: #2832, slow like queries with lots of records
This commit is contained in:
James Mikrut
2023-06-19 10:18:49 -04:00
committed by GitHub
3 changed files with 33 additions and 6 deletions

View File

@@ -372,6 +372,23 @@ export class ParamParser {
}
}
if (operator === 'like' && typeof formattedValue === 'string') {
const words = formattedValue.split(' ');
const result = {
value: {
$and: words.map((word) => ({
[path]: {
$regex: word.replace(/[\\^$*+?\\.()|[\]{}]/g, '\\$&'),
$options: 'i',
},
})),
},
};
return result;
}
// Some operators like 'near' need to define a full query
// so if there is no operator key, just return the value
if (!operatorKey) {

View File

@@ -1,6 +1,5 @@
import mongoose from 'mongoose';
import { createArrayFromCommaDelineated } from './createArrayFromCommaDelineated';
import wordBoundariesRegex from '../utilities/wordBoundariesRegex';
import { Field, TabAsField } from '../fields/config/types';
type SanitizeQueryValueArgs = {
@@ -107,11 +106,6 @@ export const sanitizeQueryValue = ({ field, path, operator, val, hasCustomID }:
if (operator === 'contains') {
formattedValue = { $regex: formattedValue, $options: 'i' };
}
if (operator === 'like' && typeof formattedValue === 'string') {
const $regex = wordBoundariesRegex(formattedValue);
formattedValue = { $regex };
}
}
if (operator === 'exists') {