Lucene 扩展QueryParse

一、QueryParser的功能扩展点

        

//通配符查询	
protected  Query getWildcardQuery(String field,String termStr) throws ParseException{
	
		throw new ParseException("Wildcard not allowed");
	}
//模糊查询
protected Query getFuzzyQuery(String field,String term,float minSimilarity) throws ParseException{
		
		throw new ParseException("Fuzzy queries no allowed");
	}
//boolean查询
	@Override
protected Query getBooleanQuery(
			List<BooleanClause> arg0, boolean arg1) throws ParseException {
		// TODO Auto-generated method stub
		return super.getBooleanQuery(arg0, arg1);
	}
//boolean查询
	@Override
protected Query getBooleanQuery(
			List<BooleanClause> clauses) throws ParseException {
		// TODO Auto-generated method stub
		return super.getBooleanQuery(clauses);
	}
//用户构造termquery对象,或者PhraseQuery
	@Override
protected Query getFieldQuery(String field,
			String queryText, boolean quoted) throws ParseException {
		// TODO Auto-generated method stub
		return super.getFieldQuery(field, queryText, quoted);
	}
//当查询项以*结尾时,该方法构造一个query对象
	@Override
protected  Query getPrefixQuery(String field,
			String termStr) throws ParseException {
		// TODO Auto-generated method stub
		return super.getPrefixQuery(field, termStr);
	}
//范围查询
	@Override
protected  Query getRangeQuery(String arg0,
			String arg1, String arg2, boolean arg3, boolean arg4)
			throws ParseException {
		// TODO Auto-generated method stub
		return super.getRangeQuery(arg0, arg1, arg2, arg3, arg4);
	}

 二、禁用模糊查询和通配查询的小例子

        直接覆盖方法

public class TestQueryParse extends QueryParser{

	public TestQueryParse(Version mathchVersion,String field ,Analyzer analyzer){
		super(mathchVersion, field , analyzer);
	}
    
	protected final Query getWildcardQuery(String field,String termStr) throws ParseException{
	
		throw new ParseException("Wildcard not allowed");
	}
	protected Query getFuzzyQuery(String field,String term,float minSimilarity) throws ParseException{
		
		throw new ParseException("Fuzzy queries no allowed");
	}
	
	
	
}

三、通过构建子类去实现日期范围数字范围查询

  Query query =NumericRangeQuery.newIntRange("id", 1, 1, true,true);

NumercRangeQuery 对数字进行范围的一个查询

四、对已排序短语进行查询

猜你喜欢

转载自xiaozhou09.iteye.com/blog/1868165