在执行搜索之前操作 Lucene 查询
我正在开发一个 Java Web 应用程序(Spring 3.x),它使用 SOLR 作为其搜索引擎。我希望能够拦截 Lucene 查询,并根据查找服务用“虚拟”搜索字段替换两个索引字段之一(如果成功,则使用范围搜索,否则搜索常规字段)。
例如,给定像 field0:foo (field1:bar OR field1:bash) AND field2:bing
(field1 是虚拟字段)这样的查询,
操作查询来获取 field0:foo (field3: [42 TO 45] OR field4:bash) AND field2:bing
因此,在考虑仅使用 reg ex 的想法之后,我决定查看 Lucene 类,看看是否可以重用现有代码。我希望能够获得查询的解析版本,以便迭代子句,查找要操作的某些字段。然后重新生成查询字符串并将其传递给 SOLR。
我已经接近使用 Lucene 的 QueryParser< /a> 但我只能获取术语而不能获取布尔运算符:
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
QueryParser queryParser = new QueryParser(Version.LUCENE_30, "text", analyzer);
try {
Query query = queryParser.parse(queryString);
Set<Term> terms = new TreeSet<Term>();
query.extractTerms(terms);
for (Term t : terms) {
logger.info("Term - field:" + t.field() + " | text:" + t.text());
}
} catch (ParseException ex) {
logger.warn(ex.getMessage(), ex);
}
我查看了 BooleanQuery 但没有运气那里也有。请帮忙。
I'm working on a Java webapp (Spring 3.x) that uses SOLR for its search engine. I want to be able to intercept the Lucene query and substitute a "virtual" search field for either one of two indexed fields, based upon a lookup service (if successful use a range search otherwise search a regular field).
E.g., given a query like field0:foo (field1:bar OR field1:bash) AND field2:bing
(field1 being a virtual field)
manipulate the query to get field0:foo (field3:[42 TO 45] OR field4:bash) AND field2:bing
So after toying with the idea of just using a reg ex, I decided to look at the Lucene classes, to see if I could re-use existing code. I'd like to be able to get a parsed version of the query so as to iterate over the clauses, looking for certain fields to manipulate. Then re-generate the query string and pass it on to SOLR.
I've got close using Lucene's QueryParser but I can only get the terms and not the boolean operators:
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
QueryParser queryParser = new QueryParser(Version.LUCENE_30, "text", analyzer);
try {
Query query = queryParser.parse(queryString);
Set<Term> terms = new TreeSet<Term>();
query.extractTerms(terms);
for (Term t : terms) {
logger.info("Term - field:" + t.field() + " | text:" + t.text());
}
} catch (ParseException ex) {
logger.warn(ex.getMessage(), ex);
}
I've looked at the BooleanQuery but haven't had luck there either. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
制作您自己的查询解析器:
Make your own query parser: