Search in sources :

Example 16 with ValueSource

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

the class Grouping method addFieldCommand.

/**
   * Adds a field command based on the specified field.
   * If the field is not compatible with {@link CommandField} it invokes the
   * {@link #addFunctionCommand(String, org.apache.solr.request.SolrQueryRequest)} method.
   *
   * @param field The fieldname to group by.
   */
public void addFieldCommand(String field, SolrQueryRequest request) throws SyntaxError {
    // Throws an exception when field doesn't exist. Bad request.
    SchemaField schemaField = searcher.getSchema().getField(field);
    FieldType fieldType = schemaField.getType();
    ValueSource valueSource = fieldType.getValueSource(schemaField, null);
    if (!(valueSource instanceof StrFieldSource)) {
        addFunctionCommand(field, request);
        return;
    }
    Grouping.CommandField gc = new CommandField();
    gc.withinGroupSort = withinGroupSort;
    gc.groupBy = field;
    gc.key = field;
    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 : SchemaField(org.apache.solr.schema.SchemaField) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) ValueSource(org.apache.lucene.queries.function.ValueSource) StrFieldSource(org.apache.solr.schema.StrFieldSource) FieldType(org.apache.solr.schema.FieldType)

Example 17 with ValueSource

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

the class TestValueSource method getMultiValueSources.

private static MVResult getMultiValueSources(List<ValueSource> sources) {
    MVResult mvr = new MVResult();
    if (sources.size() % 2 != 0) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Illegal number of sources.  There must be an even number of sources");
    }
    if (sources.size() == 2) {
        //check to see if these are MultiValueSource
        boolean s1MV = sources.get(0) instanceof MultiValueSource;
        boolean s2MV = sources.get(1) instanceof MultiValueSource;
        if (s1MV && s2MV) {
            mvr.mv1 = (MultiValueSource) sources.get(0);
            mvr.mv2 = (MultiValueSource) sources.get(1);
        } else if (s1MV || s2MV) {
            //if one is a MultiValueSource, than the other one needs to be too.
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Illegal number of sources.  There must be an even number of sources");
        } else {
            mvr.mv1 = new VectorValueSource(Collections.singletonList(sources.get(0)));
            mvr.mv2 = new VectorValueSource(Collections.singletonList(sources.get(1)));
        }
    } else {
        int dim = sources.size() / 2;
        List<ValueSource> sources1 = new ArrayList<>(dim);
        List<ValueSource> sources2 = new ArrayList<>(dim);
        //Get dim value sources for the first vector
        splitSources(dim, sources, sources1, sources2);
        mvr.mv1 = new VectorValueSource(sources1);
        mvr.mv2 = new VectorValueSource(sources2);
    }
    return mvr;
}
Also used : AggValueSource(org.apache.solr.search.facet.AggValueSource) ValueSource(org.apache.lucene.queries.function.ValueSource) ArrayList(java.util.ArrayList) SolrException(org.apache.solr.common.SolrException)

Example 18 with ValueSource

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

the class FunctionQParser method parseAgg.

/** @lucene.experimental */
public AggValueSource parseAgg(int flags) throws SyntaxError {
    String id = sp.getId();
    AggValueSource vs = null;
    boolean hasParen = false;
    if ("agg".equals(id)) {
        hasParen = sp.opt("(");
        vs = parseAgg(flags | FLAG_IS_AGG);
    } else {
        // parse as an aggregation...
        if (!id.startsWith("agg_")) {
            id = "agg_" + id;
        }
        hasParen = sp.opt("(");
        ValueSourceParser argParser = req.getCore().getValueSourceParser(id);
        argParser = req.getCore().getValueSourceParser(id);
        if (argParser == null) {
            throw new SyntaxError("Unknown aggregation " + id + " in (" + sp + ")");
        }
        ValueSource vv = argParser.parse(this);
        if (!(vv instanceof AggValueSource)) {
            if (argParser == null) {
                throw new SyntaxError("Expected aggregation from " + id + " but got (" + vv + ") in (" + sp + ")");
            }
        }
        vs = (AggValueSource) vv;
    }
    if (hasParen) {
        sp.expect(")");
    }
    if ((flags & FLAG_CONSUME_DELIMITER) != 0) {
        consumeArgumentDelimiter();
    }
    return vs;
}
Also used : ValueSource(org.apache.lucene.queries.function.ValueSource) AggValueSource(org.apache.solr.search.facet.AggValueSource) AggValueSource(org.apache.solr.search.facet.AggValueSource)

Example 19 with ValueSource

use of org.apache.lucene.queries.function.ValueSource 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 20 with ValueSource

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

ValueSource (org.apache.lucene.queries.function.ValueSource)54 Query (org.apache.lucene.search.Query)13 FunctionQuery (org.apache.lucene.queries.function.FunctionQuery)12 SolrException (org.apache.solr.common.SolrException)11 SchemaField (org.apache.solr.schema.SchemaField)11 FunctionValues (org.apache.lucene.queries.function.FunctionValues)10 ArrayList (java.util.ArrayList)8 QueryValueSource (org.apache.lucene.queries.function.valuesource.QueryValueSource)7 FieldType (org.apache.solr.schema.FieldType)6 BooleanQuery (org.apache.lucene.search.BooleanQuery)5 AggValueSource (org.apache.solr.search.facet.AggValueSource)5 IOException (java.io.IOException)4 Map (java.util.Map)4 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)4 VectorValueSource (org.apache.lucene.queries.function.valuesource.VectorValueSource)4 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)4 IndexReader (org.apache.lucene.index.IndexReader)3 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)3 BoostedQuery (org.apache.lucene.queries.function.BoostedQuery)3 TermGroupSelector (org.apache.lucene.search.grouping.TermGroupSelector)3