Search in sources :

Example 1 with QueryConfigHandler

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;
}
Also used : FieldConfig(org.apache.lucene.queryparser.flexible.core.config.FieldConfig) QueryConfigHandler(org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler) PointsConfig(org.apache.lucene.queryparser.flexible.standard.config.PointsConfig) FieldQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode) QueryNodeParseException(org.apache.lucene.queryparser.flexible.core.QueryNodeParseException) PointRangeQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.PointRangeQueryNode) TermRangeQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.TermRangeQueryNode) QueryNodeParseException(org.apache.lucene.queryparser.flexible.core.QueryNodeParseException) ParseException(java.text.ParseException) MessageImpl(org.apache.lucene.queryparser.flexible.messages.MessageImpl) PointQueryNode(org.apache.lucene.queryparser.flexible.standard.nodes.PointQueryNode) NumberFormat(java.text.NumberFormat)

Example 2 with QueryConfigHandler

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);
}
Also used : FuzzyConfig(org.apache.lucene.queryparser.flexible.standard.config.FuzzyConfig) QueryConfigHandler(org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler) StandardQueryConfigHandler(org.apache.lucene.queryparser.flexible.standard.config.StandardQueryConfigHandler)

Example 3 with QueryConfigHandler

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);
}
Also used : FuzzyConfig(org.apache.lucene.queryparser.flexible.standard.config.FuzzyConfig) QueryConfigHandler(org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler) StandardQueryConfigHandler(org.apache.lucene.queryparser.flexible.standard.config.StandardQueryConfigHandler)

Example 4 with QueryConfigHandler

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;
}
Also used : FieldableNode(org.apache.lucene.queryparser.flexible.core.nodes.FieldableNode) QueryConfigHandler(org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler)

Example 5 with QueryConfigHandler

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");
}
Also used : QueryNodeProcessorPipeline(org.apache.lucene.queryparser.flexible.core.processors.QueryNodeProcessorPipeline) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) OrQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.OrQueryNode) QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode) QueryConfigHandler(org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler) SyntaxParser(org.apache.lucene.queryparser.flexible.core.parser.SyntaxParser) StandardSyntaxParser(org.apache.lucene.queryparser.flexible.standard.parser.StandardSyntaxParser) StandardSyntaxParser(org.apache.lucene.queryparser.flexible.standard.parser.StandardSyntaxParser) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Aggregations

QueryConfigHandler (org.apache.lucene.queryparser.flexible.core.config.QueryConfigHandler)8 FieldConfig (org.apache.lucene.queryparser.flexible.core.config.FieldConfig)3 FuzzyConfig (org.apache.lucene.queryparser.flexible.standard.config.FuzzyConfig)3 NumberFormat (java.text.NumberFormat)2 ParseException (java.text.ParseException)2 QueryNodeParseException (org.apache.lucene.queryparser.flexible.core.QueryNodeParseException)2 FieldQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode)2 FieldableNode (org.apache.lucene.queryparser.flexible.core.nodes.FieldableNode)2 MessageImpl (org.apache.lucene.queryparser.flexible.messages.MessageImpl)2 PointsConfig (org.apache.lucene.queryparser.flexible.standard.config.PointsConfig)2 StandardQueryConfigHandler (org.apache.lucene.queryparser.flexible.standard.config.StandardQueryConfigHandler)2 PointQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.PointQueryNode)2 PointRangeQueryNode (org.apache.lucene.queryparser.flexible.standard.nodes.PointRangeQueryNode)2 Analyzer (org.apache.lucene.analysis.Analyzer)1 BoostQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.BoostQueryNode)1 FuzzyQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.FuzzyQueryNode)1 OrQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.OrQueryNode)1 QueryNode (org.apache.lucene.queryparser.flexible.core.nodes.QueryNode)1 RangeQueryNode (org.apache.lucene.queryparser.flexible.core.nodes.RangeQueryNode)1 SyntaxParser (org.apache.lucene.queryparser.flexible.core.parser.SyntaxParser)1