Search in sources :

Example 6 with FunctionQuery

use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.

the class Grouping method addFunctionCommand.

public void addFunctionCommand(String groupByStr, SolrQueryRequest request) throws SyntaxError {
    QParser parser = QParser.getParser(groupByStr, "func", request);
    Query q = parser.getQuery();
    final Grouping.Command gc;
    if (q instanceof FunctionQuery) {
        ValueSource valueSource = ((FunctionQuery) q).getValueSource();
        if (valueSource instanceof StrFieldSource) {
            String field = ((StrFieldSource) valueSource).getField();
            CommandField commandField = new CommandField();
            commandField.groupBy = field;
            gc = commandField;
        } else {
            CommandFunc commandFunc = new CommandFunc();
            commandFunc.groupBy = valueSource;
            gc = commandFunc;
        }
    } else {
        CommandFunc commandFunc = new CommandFunc();
        commandFunc.groupBy = new QueryValueSource(q, 0.0f);
        gc = commandFunc;
    }
    gc.withinGroupSort = withinGroupSort;
    gc.key = groupByStr;
    gc.numGroups = limitDefault;
    gc.docsPerGroup = docsPerGroupDefault;
    gc.groupOffset = groupOffsetDefault;
    gc.offset = cmd.getOffset();
    gc.groupSort = groupSort;
    gc.format = defaultFormat;
    gc.totalCount = defaultTotalCount;
    if (main) {
        gc.main = true;
        gc.format = Grouping.Format.simple;
    }
    if (gc.format == Grouping.Format.simple) {
        // doesn't make sense
        gc.groupOffset = 0;
    }
    commands.add(gc);
}
Also used : FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) ValueSource(org.apache.lucene.queries.function.ValueSource) StrFieldSource(org.apache.solr.schema.StrFieldSource) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource)

Example 7 with FunctionQuery

use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.

the class SortSpecParsing method parseSortSpecImpl.

private static SortSpec parseSortSpecImpl(String sortSpec, IndexSchema schema, SolrQueryRequest optionalReq) {
    if (sortSpec == null || sortSpec.length() == 0)
        return newEmptySortSpec();
    List<SortField> sorts = new ArrayList<>(4);
    List<SchemaField> fields = new ArrayList<>(4);
    try {
        StrParser sp = new StrParser(sortSpec);
        while (sp.pos < sp.end) {
            sp.eatws();
            final int start = sp.pos;
            // short circuit test for a really simple field name
            String field = sp.getId(null);
            Exception qParserException = null;
            if ((field == null || !Character.isWhitespace(sp.peekChar())) && (optionalReq != null)) {
                // let's try it as a function instead
                field = null;
                String funcStr = sp.val.substring(start);
                QParser parser = QParser.getParser(funcStr, FunctionQParserPlugin.NAME, optionalReq);
                Query q = null;
                try {
                    if (parser instanceof FunctionQParser) {
                        FunctionQParser fparser = (FunctionQParser) parser;
                        fparser.setParseMultipleSources(false);
                        fparser.setParseToEnd(false);
                        q = fparser.getQuery();
                        if (fparser.localParams != null) {
                            if (fparser.valFollowedParams) {
                                // need to find the end of the function query via the string parser
                                int leftOver = fparser.sp.end - fparser.sp.pos;
                                // reset our parser to the same amount of leftover
                                sp.pos = sp.end - leftOver;
                            } else {
                                // the value was via the "v" param in localParams, so we need to find
                                // the end of the local params themselves to pick up where we left off
                                sp.pos = start + fparser.localParamsEnd;
                            }
                        } else {
                            // need to find the end of the function query via the string parser
                            int leftOver = fparser.sp.end - fparser.sp.pos;
                            // reset our parser to the same amount of leftover
                            sp.pos = sp.end - leftOver;
                        }
                    } else {
                        // A QParser that's not for function queries.
                        // It must have been specified via local params.
                        q = parser.getQuery();
                        assert parser.getLocalParams() != null;
                        sp.pos = start + parser.localParamsEnd;
                    }
                    Boolean top = sp.getSortDirection();
                    if (null != top) {
                        // we have a Query and a valid direction
                        if (q instanceof FunctionQuery) {
                            sorts.add(((FunctionQuery) q).getValueSource().getSortField(top));
                        } else {
                            sorts.add((new QueryValueSource(q, 0.0f)).getSortField(top));
                        }
                        fields.add(null);
                        continue;
                    }
                } catch (Exception e) {
                    // hang onto this in case the string isn't a full field name either
                    qParserException = e;
                }
            }
            if (field == null) {
                // try again, simple rules for a field name with no whitespace
                sp.pos = start;
                field = sp.getSimpleString();
            }
            Boolean top = sp.getSortDirection();
            if (null == top) {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Can't determine a Sort Order (asc or desc) in sort spec " + sp);
            }
            if (SCORE.equals(field)) {
                if (top) {
                    sorts.add(SortField.FIELD_SCORE);
                } else {
                    sorts.add(new SortField(null, SortField.Type.SCORE, true));
                }
                fields.add(null);
            } else if (DOCID.equals(field)) {
                sorts.add(new SortField(null, SortField.Type.DOC, top));
                fields.add(null);
            } else {
                // try to find the field
                SchemaField sf = schema.getFieldOrNull(field);
                if (null == sf) {
                    if (null != qParserException) {
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "sort param could not be parsed as a query, and is not a " + "field that exists in the index: " + field, qParserException);
                    }
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "sort param field can't be found: " + field);
                }
                sorts.add(sf.getSortField(top));
                fields.add(sf);
            }
        }
    } catch (SyntaxError e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "error in sort: " + sortSpec, e);
    }
    // normalize a sort on score desc to null
    if (sorts.size() == 1 && sorts.get(0) == SortField.FIELD_SCORE) {
        return newEmptySortSpec();
    }
    Sort s = new Sort(sorts.toArray(new SortField[sorts.size()]));
    return new SortSpec(s, fields);
}
Also used : Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ArrayList(java.util.ArrayList) SortField(org.apache.lucene.search.SortField) SolrException(org.apache.solr.common.SolrException) SchemaField(org.apache.solr.schema.SchemaField) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) Sort(org.apache.lucene.search.Sort) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) SolrException(org.apache.solr.common.SolrException)

Example 8 with FunctionQuery

use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.

the class FunctionRangeQParserPlugin method createParser.

@Override
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
    return new QParser(qstr, localParams, params, req) {

        ValueSource vs;

        String funcStr;

        @Override
        public Query parse() throws SyntaxError {
            funcStr = localParams.get(QueryParsing.V, null);
            QParser subParser = subQuery(funcStr, FunctionQParserPlugin.NAME);
            // the range can be based on the relevancy score of embedded queries.
            subParser.setIsFilter(false);
            Query funcQ = subParser.getQuery();
            if (funcQ instanceof FunctionQuery) {
                vs = ((FunctionQuery) funcQ).getValueSource();
            } else {
                vs = new QueryValueSource(funcQ, 0.0f);
            }
            String l = localParams.get("l");
            String u = localParams.get("u");
            boolean includeLower = localParams.getBool("incl", true);
            boolean includeUpper = localParams.getBool("incu", true);
            // TODO: add a score=val option to allow score to be the value
            ValueSourceRangeFilter rf = new ValueSourceRangeFilter(vs, l, u, includeLower, includeUpper);
            FunctionRangeQuery frq = new FunctionRangeQuery(rf);
            return frq;
        }
    };
}
Also used : FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ValueSource(org.apache.lucene.queries.function.ValueSource) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource)

Example 9 with FunctionQuery

use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.

the class ExtendedDismaxQParser method getMultiplicativeBoosts.

/**
   * Parses all multiplicative boosts
   */
protected List<ValueSource> getMultiplicativeBoosts() throws SyntaxError {
    List<ValueSource> boosts = new ArrayList<>();
    if (config.hasMultiplicativeBoosts()) {
        for (String boostStr : config.multBoosts) {
            if (boostStr == null || boostStr.length() == 0)
                continue;
            Query boost = subQuery(boostStr, FunctionQParserPlugin.NAME).getQuery();
            ValueSource vs;
            if (boost instanceof FunctionQuery) {
                vs = ((FunctionQuery) boost).getValueSource();
            } else {
                vs = new QueryValueSource(boost, 1.0f);
            }
            boosts.add(vs);
        }
    }
    return boosts;
}
Also used : FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostedQuery(org.apache.lucene.queries.function.BoostedQuery) BoostQuery(org.apache.lucene.search.BoostQuery) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) ValueSource(org.apache.lucene.queries.function.ValueSource) ArrayList(java.util.ArrayList) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource)

Example 10 with FunctionQuery

use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.

the class AbstractSpatialFieldType method getQueryFromSpatialArgs.

protected Query getQueryFromSpatialArgs(QParser parser, SchemaField field, SpatialArgs spatialArgs) {
    T strategy = getStrategy(field.getName());
    SolrParams localParams = parser.getLocalParams();
    //See SOLR-2883 needScore
    String scoreParam = (localParams == null ? null : localParams.get(SCORE_PARAM));
    //We get the valueSource for the score then the filter and combine them.
    ValueSource valueSource = getValueSourceFromSpatialArgs(parser, field, spatialArgs, scoreParam, strategy);
    if (valueSource == null) {
        //assumed constant scoring
        return strategy.makeQuery(spatialArgs);
    }
    FunctionQuery functionQuery = new FunctionQuery(valueSource);
    if (localParams != null && !localParams.getBool(FILTER_PARAM, true))
        return functionQuery;
    Query filterQuery = strategy.makeQuery(spatialArgs);
    return new BooleanQuery.Builder().add(functionQuery, //matches everything and provides score
    Occur.MUST).add(filterQuery, //filters (score isn't used)
    Occur.FILTER).build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ValueSource(org.apache.lucene.queries.function.ValueSource) SolrParams(org.apache.solr.common.params.SolrParams)

Aggregations

FunctionQuery (org.apache.lucene.queries.function.FunctionQuery)20 Query (org.apache.lucene.search.Query)17 ValueSource (org.apache.lucene.queries.function.ValueSource)11 BooleanQuery (org.apache.lucene.search.BooleanQuery)10 BoostQuery (org.apache.lucene.search.BoostQuery)8 TermQuery (org.apache.lucene.search.TermQuery)7 QueryValueSource (org.apache.lucene.queries.function.valuesource.QueryValueSource)6 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)6 Term (org.apache.lucene.index.Term)4 ConstValueSource (org.apache.lucene.queries.function.valuesource.ConstValueSource)4 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 IndexReader (org.apache.lucene.index.IndexReader)3 ScoreDoc (org.apache.lucene.search.ScoreDoc)3 TopDocs (org.apache.lucene.search.TopDocs)3 ArrayList (java.util.ArrayList)2 BoostedQuery (org.apache.lucene.queries.function.BoostedQuery)2 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)2 SolrException (org.apache.solr.common.SolrException)2 SolrParams (org.apache.solr.common.params.SolrParams)2 SchemaField (org.apache.solr.schema.SchemaField)2