为什么该Lucene查询是“包含"的?而不是"startsWith"? [英] Why is this Lucene query a "contains" instead of a "startsWith"?

查看:114
本文介绍了为什么该Lucene查询是“包含"的?而不是"startsWith"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string q = "m";
Query query = new QueryParser("company", new StandardAnalyzer()).Parse(q+"*");

将导致查询为prefixQuery:company:a *

will result in query being a prefixQuery :company:a*

仍然会得到"Fleet Africa"之类的结果,很明显A不在开始,因此给了我不良的结果.

Still I will get results like "Fleet Africa" where it is rather obvious that the A is not at the start and thus gives me undesired results.

Query query = new TermQuery(new Term("company", q+"*"));

将导致查询成为termQuery:company:a *,并且不返回任何结果.可能是因为它将查询解释为完全匹配,而我的值都不是"a *"文字.

will result in query being a termQuery :company:a* and not returning any results. Probably because it interprets the query as an exact match and none of my values are the "a*" literal.

Query query = new WildcardQuery(new Term("company", q+"*"));

将返回与prefixquery相同的结果;

will return the same results as the prefixquery;

我在做什么错了?

推荐答案

StandardAnalyzer会将"Fleet Africa"标记为"fleet"和"africa".您的a *搜索将与后面的字词匹配.

StandardAnalyzer will tokenize "Fleet Africa" into "fleet" and "africa". Your a* search will match the later term.

如果您想将"Fleet Africa"视为一个术语,请使用不会在空格上弄乱字符串的分析器. KeywordAnalyzer是一个示例,但您仍可能希望将数据小写,以使查询不区分大小写.

If you want to consider "Fleet Africa" as one single term, use an analyzer that does not break up your string on whitespaces. KeywordAnalyzer is an example, but you may still want to lowercase your data so queries are case insensitive.

这篇关于为什么该Lucene查询是“包含"的?而不是"startsWith"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆