use of org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler in project lucene-solr by apache.
the class PointRangeQueryNodeProcessor method postProcessNode.
@Override
protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
if (node instanceof TermRangeQueryNode) {
QueryConfigHandler config = getQueryConfigHandler();
if (config != null) {
TermRangeQueryNode termRangeNode = (TermRangeQueryNode) node;
FieldConfig fieldConfig = config.getFieldConfig(StringUtils.toString(termRangeNode.getField()));
if (fieldConfig != null) {
PointsConfig numericConfig = fieldConfig.get(ConfigurationKeys.POINTS_CONFIG);
if (numericConfig != null) {
FieldQueryNode lower = termRangeNode.getLowerBound();
FieldQueryNode upper = termRangeNode.getUpperBound();
String lowerText = lower.getTextAsString();
String upperText = upper.getTextAsString();
NumberFormat numberFormat = numericConfig.getNumberFormat();
Number lowerNumber = null, upperNumber = null;
if (lowerText.length() > 0) {
try {
lowerNumber = numberFormat.parse(lowerText);
} catch (ParseException e) {
throw new QueryNodeParseException(new MessageImpl(QueryParserMessages.COULD_NOT_PARSE_NUMBER, lower.getTextAsString(), numberFormat.getClass().getCanonicalName()), e);
}
}
if (upperText.length() > 0) {
try {
upperNumber = numberFormat.parse(upperText);
} catch (ParseException e) {
throw new QueryNodeParseException(new MessageImpl(QueryParserMessages.COULD_NOT_PARSE_NUMBER, upper.getTextAsString(), numberFormat.getClass().getCanonicalName()), e);
}
}
if (Integer.class.equals(numericConfig.getType())) {
if (upperNumber != null)
upperNumber = upperNumber.intValue();
if (lowerNumber != null)
lowerNumber = lowerNumber.intValue();
} else if (Long.class.equals(numericConfig.getType())) {
if (upperNumber != null)
upperNumber = upperNumber.longValue();
if (lowerNumber != null)
lowerNumber = lowerNumber.longValue();
} else if (Double.class.equals(numericConfig.getType())) {
if (upperNumber != null)
upperNumber = upperNumber.doubleValue();
if (lowerNumber != null)
lowerNumber = lowerNumber.doubleValue();
} else if (Float.class.equals(numericConfig.getType())) {
if (upperNumber != null)
upperNumber = upperNumber.floatValue();
if (lowerNumber != null)
lowerNumber = lowerNumber.floatValue();
}
PointQueryNode lowerNode = new PointQueryNode(termRangeNode.getField(), lowerNumber, numberFormat);
PointQueryNode upperNode = new PointQueryNode(termRangeNode.getField(), upperNumber, numberFormat);
boolean lowerInclusive = termRangeNode.isLowerInclusive();
boolean upperInclusive = termRangeNode.isUpperInclusive();
return new PointRangeQueryNode(lowerNode, upperNode, lowerInclusive, upperInclusive, numericConfig);
}
}
}
}
return node;
}
use of org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler in project lucene-solr by apache.
the class StandardQueryParser method setFuzzyPrefixLength.
/**
* Set the prefix length for fuzzy queries. Default is 0.
*
* @param fuzzyPrefixLength
* The fuzzyPrefixLength to set.
*/
@Override
public void setFuzzyPrefixLength(int fuzzyPrefixLength) {
QueryConfigHandler config = getQueryConfigHandler();
FuzzyConfig fuzzyConfig = config.get(ConfigurationKeys.FUZZY_CONFIG);
if (fuzzyConfig == null) {
fuzzyConfig = new FuzzyConfig();
config.set(ConfigurationKeys.FUZZY_CONFIG, fuzzyConfig);
}
fuzzyConfig.setPrefixLength(fuzzyPrefixLength);
}
use of org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler in project lucene-solr by apache.
the class StandardQueryParser method setFuzzyMinSim.
/**
* Set the minimum similarity for fuzzy queries. Default is defined on
* {@link FuzzyQuery#defaultMinSimilarity}.
*/
@Override
public void setFuzzyMinSim(float fuzzyMinSim) {
QueryConfigHandler config = getQueryConfigHandler();
FuzzyConfig fuzzyConfig = config.get(ConfigurationKeys.FUZZY_CONFIG);
if (fuzzyConfig == null) {
fuzzyConfig = new FuzzyConfig();
config.set(ConfigurationKeys.FUZZY_CONFIG, fuzzyConfig);
}
fuzzyConfig.setMinSimilarity(fuzzyMinSim);
}
use of org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler in project lucene-solr by apache.
the class UniqueFieldQueryNodeProcessor method preProcessNode.
@Override
protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
if (node instanceof FieldableNode) {
FieldableNode fieldNode = (FieldableNode) node;
QueryConfigHandler queryConfig = getQueryConfigHandler();
if (queryConfig == null) {
throw new IllegalArgumentException("A config handler is expected by the processor UniqueFieldQueryNodeProcessor!");
}
if (!queryConfig.has(SpansQueryConfigHandler.UNIQUE_FIELD)) {
throw new IllegalArgumentException("UniqueFieldAttribute should be defined in the config handler!");
}
String uniqueField = queryConfig.get(SpansQueryConfigHandler.UNIQUE_FIELD);
fieldNode.setField(uniqueField);
}
return node;
}
use of org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler in project lucene-solr by apache.
the class TestSpanQueryParserSimpleSample method testBasicDemo.
public void testBasicDemo() throws Exception {
SyntaxParser queryParser = new StandardSyntaxParser();
// convert the CharSequence into a QueryNode tree
QueryNode queryTree = queryParser.parse("body:text", null);
// create a config handler with a attribute used in
// UniqueFieldQueryNodeProcessor
QueryConfigHandler spanQueryConfigHandler = new SpansQueryConfigHandler();
spanQueryConfigHandler.set(SpansQueryConfigHandler.UNIQUE_FIELD, "index");
// set up the processor pipeline with the ConfigHandler
// and create the pipeline for this simple demo
QueryNodeProcessorPipeline spanProcessorPipeline = new QueryNodeProcessorPipeline(spanQueryConfigHandler);
// @see SpansValidatorQueryNodeProcessor
spanProcessorPipeline.add(new SpansValidatorQueryNodeProcessor());
// @see UniqueFieldQueryNodeProcessor
spanProcessorPipeline.add(new UniqueFieldQueryNodeProcessor());
// print to show out the QueryNode tree before being processed
if (VERBOSE)
System.out.println(queryTree);
// Process the QueryTree using our new Processors
queryTree = spanProcessorPipeline.process(queryTree);
// print to show out the QueryNode tree after being processed
if (VERBOSE)
System.out.println(queryTree);
// create a instance off the Builder
SpansQueryTreeBuilder spansQueryTreeBuilder = new SpansQueryTreeBuilder();
// convert QueryNode tree to span query Objects
SpanQuery spanquery = spansQueryTreeBuilder.build(queryTree);
assertTrue(spanquery instanceof SpanTermQuery);
assertEquals(spanquery.toString(), "index:text");
}
Aggregations