Search in sources :

Example 1 with QueryValueSource

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

the class BoostQParserPlugin method createParser.

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

        QParser baseParser;

        ValueSource vs;

        String b;

        @Override
        public Query parse() throws SyntaxError {
            b = localParams.get(BOOSTFUNC);
            baseParser = subQuery(localParams.get(QueryParsing.V), null);
            Query q = baseParser.getQuery();
            if (b == null)
                return q;
            Query bq = subQuery(b, FunctionQParserPlugin.NAME).getQuery();
            if (bq instanceof FunctionQuery) {
                vs = ((FunctionQuery) bq).getValueSource();
            } else {
                vs = new QueryValueSource(bq, 0.0f);
            }
            return new BoostedQuery(q, vs);
        }

        @Override
        public String[] getDefaultHighlightFields() {
            return baseParser.getDefaultHighlightFields();
        }

        @Override
        public Query getHighlightQuery() throws SyntaxError {
            return baseParser.getHighlightQuery();
        }

        @Override
        public void addDebugInfo(NamedList<Object> debugInfo) {
            // encapsulate base debug info in a sub-list?
            baseParser.addDebugInfo(debugInfo);
            debugInfo.add("boost_str", b);
            debugInfo.add("boost_parsed", vs);
        }
    };
}
Also used : FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) Query(org.apache.lucene.search.Query) BoostedQuery(org.apache.lucene.queries.function.BoostedQuery) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ValueSource(org.apache.lucene.queries.function.ValueSource) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) NamedList(org.apache.solr.common.util.NamedList) BoostedQuery(org.apache.lucene.queries.function.BoostedQuery) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource)

Example 2 with QueryValueSource

use of org.apache.lucene.queries.function.valuesource.QueryValueSource 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 3 with QueryValueSource

use of org.apache.lucene.queries.function.valuesource.QueryValueSource 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 4 with QueryValueSource

use of org.apache.lucene.queries.function.valuesource.QueryValueSource 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 5 with QueryValueSource

use of org.apache.lucene.queries.function.valuesource.QueryValueSource 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)

Aggregations

QueryValueSource (org.apache.lucene.queries.function.valuesource.QueryValueSource)9 FunctionQuery (org.apache.lucene.queries.function.FunctionQuery)6 ValueSource (org.apache.lucene.queries.function.ValueSource)5 Query (org.apache.lucene.search.Query)5 ArrayList (java.util.ArrayList)3 Term (org.apache.lucene.index.Term)2 BoostedQuery (org.apache.lucene.queries.function.BoostedQuery)2 ConstValueSource (org.apache.lucene.queries.function.valuesource.ConstValueSource)2 DocFreqValueSource (org.apache.lucene.queries.function.valuesource.DocFreqValueSource)2 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)2 IDFValueSource (org.apache.lucene.queries.function.valuesource.IDFValueSource)2 JoinDocFreqValueSource (org.apache.lucene.queries.function.valuesource.JoinDocFreqValueSource)2 LiteralValueSource (org.apache.lucene.queries.function.valuesource.LiteralValueSource)2 MaxDocValueSource (org.apache.lucene.queries.function.valuesource.MaxDocValueSource)2 NormValueSource (org.apache.lucene.queries.function.valuesource.NormValueSource)2 NumDocsValueSource (org.apache.lucene.queries.function.valuesource.NumDocsValueSource)2 SumTotalTermFreqValueSource (org.apache.lucene.queries.function.valuesource.SumTotalTermFreqValueSource)2 TFValueSource (org.apache.lucene.queries.function.valuesource.TFValueSource)2 TermFreqValueSource (org.apache.lucene.queries.function.valuesource.TermFreqValueSource)2 TotalTermFreqValueSource (org.apache.lucene.queries.function.valuesource.TotalTermFreqValueSource)2