我正在使用 JS SDK 将 Algolia 用于 Web 应用程序,并尝试按 使用 >、<, >= 等比较运算符的字符串值...
我希望它能按 ASCII 值(如“ac”)过滤字符串< "ba"
我尝试使用以下代码解决此问题:
return index.search(query, {
filters: "myId <= 'ba'"
});
期望它只返回 ID 为“acd”、“ax”、“ba”等的记录
但我收到错误:
filters: Unexpected token string(ba) expected numeric at col 13
我现在的问题是:Algolia 是否支持这一点,或者是否有任何解决方法可以实现这一目标?
提前谢谢您。
我不了解 Algolia,但在 Javascript 中你可以尝试使用 regex(正则表达式)来过滤字符串。有大量不同的正则表达式模式可用于匹配特定的 ASCII 字符或部分字符串或整个字符串甚至数字。您只需要找到正确的模式即可。
const ids = 'axsdf qweac 123456 vwxyz pOibA 123a456'
// Regex pattern to return string sections that contains "a" preceded or followed by any other character
// Can also be used on a loop to loop through an array of IDs
const regex = /\w*a\w*/gi
// An array with all the matches
const matches = ids.match(regex)
console.log(matches)
在此处阅读有关正则表达式模式及其工作原理的更多信息。 并且
这里是一个正则表达式可视化工具,您可以在其中测试正则表达式模式,直到它们为您提供所需的结果。