[英]String comparison in Java, which algorithm should I use?
我要求将用户要搜索的产品名称与可用产品进行比较。 我有存储在MySQL数据库中的产品名称。 我正在收集所有名称,并在我的java服务启动时将其设置为应用程序级别(java)。
现在我的字符串比较方案是这样的:
Available product names:
1) Samsung galaxy s2
2) Samsung galaxy s3
3) Samsung galaxy s4
User input1: galaxy s3 - Then in this scenario my 2nd result should come first as it has 2 matching keywords 'galaxy' and 's3', where other 2 has only 1 matching keyword 'galaxy'.
User input2: s3 - Then here only 2nd result should come, because the other 2 has no matching key word.
User input3: samsung - Then here all three results should come.
任何人都可以建议使用哪种算法适合Java? 还有一件事,将所有产品名称从MySQL提升到应用程序级别(java)是正确的方法吗? 还是可以在MySQL级别上做到这一点? (PS:我不想在MySQL端使用类似查询,因为它会非常慢)
用户输入中的单词由空格分隔,因此将它们分成多个小节,然后使用string.contains()(最长的字符串首先匹配),可以为您排名。
我解决了与使用trie( Trie )相同的问题,并存储了trie中所有的字符串组合,然后您可以在trie中搜索用户输入
注意:将所有组合存储在trie中不是一个好方法。 但将所有组合存储在trie中将有助于您以所需的方式进行搜索,否则将以前缀搜索方式进行。
这不是正确的方法,请为您的搜索查询
例如:
SELECT productname FROM product WHERE productname='%samsung%';
总是只获取需要的记录,而不是所有记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.