view src/goodjava/queryparser/NumberFieldParser.java @ 1402:27efb1fcbcb5

move luan.lib to goodjava
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 17 Sep 2019 01:35:01 -0400
parents src/luan/lib/queryparser/NumberFieldParser.java@dc2af9d5463b
children 5f038be65271
line wrap: on
line source

package goodjava.queryparser;

import org.apache.lucene.search.Query;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.SortField;
import goodjava.parser.ParseException;


public abstract class NumberFieldParser implements FieldParser {

	@Override public final Query getQuery(SaneQueryParser qp,String field,String query) throws ParseException {
		return getRangeQuery(qp,field,query,query,true,true);
	}

	@Override public final Query getRangeQuery(SaneQueryParser qp,String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) throws ParseException {
		try {
			return getRangeQuery(field,minQuery,maxQuery,includeMin,includeMax);
		} catch(NumberFormatException e) {
			throw qp.exception(e);
		}
	}

	abstract protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax);

	@Override public SortField getSortField(SaneQueryParser qp,String field,boolean reverse) {
		return new SortField( field, sortType(), reverse );
	}

	abstract protected SortField.Type sortType();


	public static final FieldParser INT = new NumberFieldParser() {

		@Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
			int min = Integer.parseInt(minQuery);
			int max = Integer.parseInt(maxQuery);
			return NumericRangeQuery.newIntRange(field,min,max,includeMin,includeMax);
		}

		@Override protected SortField.Type sortType() {
			return SortField.Type.INT;
		}
	};

	public static final FieldParser LONG = new NumberFieldParser() {

		@Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
			long min = Long.parseLong(minQuery);
			long max = Long.parseLong(maxQuery);
			return NumericRangeQuery.newLongRange(field,min,max,includeMin,includeMax);
		}

		@Override protected SortField.Type sortType() {
			return SortField.Type.LONG;
		}
	};

	public static final FieldParser FLOAT = new NumberFieldParser() {

		@Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
			float min = Float.parseFloat(minQuery);
			float max = Float.parseFloat(maxQuery);
			return NumericRangeQuery.newFloatRange(field,min,max,includeMin,includeMax);
		}

		@Override protected SortField.Type sortType() {
			return SortField.Type.FLOAT;
		}
	};

	public static final FieldParser DOUBLE = new NumberFieldParser() {

		@Override protected Query getRangeQuery(String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) {
			double min = Double.parseDouble(minQuery);
			double max = Double.parseDouble(maxQuery);
			return NumericRangeQuery.newDoubleRange(field,min,max,includeMin,includeMax);
		}

		@Override protected SortField.Type sortType() {
			return SortField.Type.DOUBLE;
		}
	};

}