在执行搜索之前处理Lucene查询 [英] Manipulate Lucene query before performing search

查看:90
本文介绍了在执行搜索之前处理Lucene查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究使用SOLR作为其搜索引擎的Java Web应用程序(春季3.x).我希望能够拦截Lucene查询,并基于查找服务将虚拟"搜索字段替换为两个索引字段之一(如果成功,则使用范围搜索,否则搜索常规字段).

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).

例如,给定类似field0:foo (field1:bar OR field1:bash) AND field2:bing的查询(field1是虚拟字段)

E.g., given a query like field0:foo (field1:bar OR field1:bash) AND field2:bing (field1 being a virtual field)

处理查询以获取field0:foo (field3:[42 TO 45] OR field4:bash) AND field2:bing

因此,在考虑了仅使用reg ex的想法之后,我决定查看Lucene类,以查看是否可以重用现有代码.我希望能够获得查询的解析版本,以便遍历子句,查找要操作的某些字段.然后重新生成查询字符串,并将其传递给SOLR.

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.

我已经很接近使用Lucene的 QueryParser ,但我只能获取条款,而不能获取布尔运算符:

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);
}

我查看了 BooleanQuery ,但也没有运气.请帮忙.

I've looked at the BooleanQuery but haven't had luck there either. Please help.

推荐答案

创建自己的查询解析器:

Make your own query parser:

class MyParser : MultiFieldQueryParser {
  @override
  public Query getFieldQuery(string field, string queryText) {
     if lookupSuccessful(field, queryText) { 
       return myQuery(field, queryText);
     }
     return base.getFieldQuery(field, queryText);
  }
}

这篇关于在执行搜索之前处理Lucene查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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