全国服务热线:400-885-2225

扩展QueryParser

有时,QueryParser查询并不能满足我们的要求,这时,我们可以考虑扩展QueryParser,扩展方式为写一个类继承QueryParser,然后覆盖相应的方法.
温馨提示:java培训活动全新优惠,尽在腾科IT教育

public class MyQueryParser extends QueryParser {
public MyQueryParser(Version matchVersion, String f, Analyzer a) {
super(matchVersion, f, a);
}
//禁用通配符查询
@Override
public Query getWildcardQuery(String field, String termStr)
throws ParseException {
throw new ParseException("禁用通配符查询!");
}
//禁用模糊查询
@Override
public Query getFuzzyQuery(String field, String termStr,
float minSimilarity) throws ParseException {
throw new ParseException("禁用模糊查询!");
}

//处理数值域的范围查询
@Override
public Query getRangeQuery(String field, String lowTerm,
String upperTerm, boolean startInclusive, boolean endInclusive) throws ParseException {
Query query = super.getRangeQuery(field, lowTerm, upperTerm, startInclusive, endInclusive);
if(field.equals(DocInfo.FILE_SIZE)){
query = NumericRangeQuery.newLongRange(field,Long.parseLong(lowTerm) , Long.parseLong(upperTerm), startInclusive, endInclusive);
}
return query;
}

}
测试扩展的QueryParser:

public void testMyQueryParser() {
        Query query = null;
        try {
            MyQueryParser parser = new MyQueryParser(LuceneUtil.version, DocInfo.FILE_TITLE,
                    LuceneUtil.ANALYZER);
            query = parser.parse("size:[200 TO 500]");

        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<DocInfo> docInfos = SearcherManager.findIndexes(query, indexFile);
        LuceneUtil.printDocInfos(docInfos);

    }
腾科IT教育技术编辑!

上一篇:Lucene4.1分页查询

下一篇:循环控制语句

 
点击QQ沟通