Search in sources :

Example 41 with ValueSource

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

the class FunctionQParser method parseValueSource.

protected ValueSource parseValueSource(int flags) throws SyntaxError {
    ValueSource valueSource;
    int ch = sp.peek();
    if (ch >= '0' && ch <= '9' || ch == '.' || ch == '+' || ch == '-') {
        Number num = sp.getNumber();
        if (num instanceof Long) {
            valueSource = new LongConstValueSource(num.longValue());
        } else if (num instanceof Double) {
            valueSource = new DoubleConstValueSource(num.doubleValue());
        } else {
            // shouldn't happen
            valueSource = new ConstValueSource(num.floatValue());
        }
    } else if (ch == '"' || ch == '\'') {
        valueSource = new LiteralValueSource(sp.getQuotedString());
    } else if (ch == '$') {
        sp.pos++;
        String param = sp.getId();
        String val = getParam(param);
        if (val == null) {
            throw new SyntaxError("Missing param " + param + " while parsing function '" + sp.val + "'");
        }
        QParser subParser = subQuery(val, "func");
        if (subParser instanceof FunctionQParser) {
            ((FunctionQParser) subParser).setParseMultipleSources(true);
        }
        Query subQuery = subParser.getQuery();
        if (subQuery instanceof FunctionQuery) {
            valueSource = ((FunctionQuery) subQuery).getValueSource();
        } else {
            valueSource = new QueryValueSource(subQuery, 0.0f);
        }
    /***
       // dereference *simple* argument (i.e., can't currently be a function)
       // In the future we could support full function dereferencing via a stack of ValueSource (or StringParser) objects
      ch = val.length()==0 ? '\0' : val.charAt(0);

      if (ch>='0' && ch<='9'  || ch=='.' || ch=='+' || ch=='-') {
        StrParser sp = new StrParser(val);
        Number num = sp.getNumber();
        if (num instanceof Long) {
          valueSource = new LongConstValueSource(num.longValue());
        } else if (num instanceof Double) {
          valueSource = new DoubleConstValueSource(num.doubleValue());
        } else {
          // shouldn't happen
          valueSource = new ConstValueSource(num.floatValue());
        }
      } else if (ch == '"' || ch == '\'') {
        StrParser sp = new StrParser(val);
        val = sp.getQuotedString();
        valueSource = new LiteralValueSource(val);
      } else {
        if (val.length()==0) {
          valueSource = new LiteralValueSource(val);
        } else {
          String id = val;
          SchemaField f = req.getSchema().getField(id);
          valueSource = f.getType().getValueSource(f, this);
        }
      }
       ***/
    } else {
        String id = sp.getId();
        if (sp.opt("(")) {
            // a function... look it up.
            ValueSourceParser argParser = req.getCore().getValueSourceParser(id);
            if (argParser == null) {
                throw new SyntaxError("Unknown function " + id + " in FunctionQuery(" + sp + ")");
            }
            valueSource = argParser.parse(this);
            sp.expect(")");
        } else {
            if ("true".equals(id)) {
                valueSource = new BoolConstValueSource(true);
            } else if ("false".equals(id)) {
                valueSource = new BoolConstValueSource(false);
            } else {
                SchemaField f = req.getSchema().getField(id);
                valueSource = f.getType().getValueSource(f, this);
            }
        }
    }
    if ((flags & FLAG_CONSUME_DELIMITER) != 0) {
        consumeArgumentDelimiter();
    }
    return valueSource;
}
Also used : Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) SchemaField(org.apache.solr.schema.SchemaField) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ValueSource(org.apache.lucene.queries.function.ValueSource) AggValueSource(org.apache.solr.search.facet.AggValueSource)

Example 42 with ValueSource

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

the class FunctionQParser method parse.

@Override
public Query parse() throws SyntaxError {
    ValueSource vs = null;
    List<ValueSource> lst = null;
    for (; ; ) {
        ValueSource valsource = parseValueSource(FLAG_DEFAULT & ~FLAG_CONSUME_DELIMITER);
        sp.eatws();
        if (!parseMultipleSources) {
            vs = valsource;
            break;
        } else {
            if (lst != null) {
                lst.add(valsource);
            } else {
                vs = valsource;
            }
        }
        // check if there is a "," separator
        if (sp.peek() != ',')
            break;
        consumeArgumentDelimiter();
        if (lst == null) {
            lst = new ArrayList<>(2);
            lst.add(valsource);
        }
    }
    if (parseToEnd && sp.pos < sp.end) {
        throw new SyntaxError("Unexpected text after function: " + sp.val.substring(sp.pos, sp.end));
    }
    if (lst != null) {
        vs = new VectorValueSource(lst);
    }
    return new FunctionQuery(vs);
}
Also used : FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ValueSource(org.apache.lucene.queries.function.ValueSource) AggValueSource(org.apache.solr.search.facet.AggValueSource)

Example 43 with ValueSource

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

the class DocBasedVersionConstraintsProcessorFactory method inform.

@Override
public void inform(SolrCore core) {
    if (core.getUpdateHandler().getUpdateLog() == null) {
        throw new SolrException(SERVER_ERROR, "updateLog must be enabled.");
    }
    if (core.getLatestSchema().getUniqueKeyField() == null) {
        throw new SolrException(SERVER_ERROR, "schema must have uniqueKey defined.");
    }
    SchemaField userVersionField = core.getLatestSchema().getField(versionField);
    if (userVersionField == null || !userVersionField.stored() || userVersionField.multiValued()) {
        throw new SolrException(SERVER_ERROR, "field " + versionField + " must be defined in schema, be stored, and be single valued.");
    }
    try {
        ValueSource vs = userVersionField.getType().getValueSource(userVersionField, null);
        useFieldCache = true;
    } catch (Exception e) {
        log.warn("Can't use fieldcache/valuesource: " + e.getMessage());
    }
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) ValueSource(org.apache.lucene.queries.function.ValueSource) SolrException(org.apache.solr.common.SolrException) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException)

Example 44 with ValueSource

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

the class IndexFingerprint method getFingerprint.

public static IndexFingerprint getFingerprint(SolrIndexSearcher searcher, LeafReaderContext ctx, Long maxVersion) throws IOException {
    SchemaField versionField = VersionInfo.getAndCheckVersionField(searcher.getSchema());
    ValueSource vs = versionField.getType().getValueSource(versionField, null);
    Map funcContext = ValueSource.newContext(searcher);
    vs.createWeight(funcContext, searcher);
    IndexFingerprint f = new IndexFingerprint();
    f.maxVersionSpecified = maxVersion;
    f.maxDoc = ctx.reader().maxDoc();
    f.numDocs = ctx.reader().numDocs();
    int maxDoc = ctx.reader().maxDoc();
    Bits liveDocs = ctx.reader().getLiveDocs();
    FunctionValues fv = vs.getValues(funcContext, ctx);
    for (int doc = 0; doc < maxDoc; doc++) {
        if (liveDocs != null && !liveDocs.get(doc))
            continue;
        long v = fv.longVal(doc);
        f.maxVersionEncountered = Math.max(v, f.maxVersionEncountered);
        if (v <= f.maxVersionSpecified) {
            f.maxInHash = Math.max(v, f.maxInHash);
            f.versionsHash += Hash.fmix64(v);
            f.numVersions++;
        }
    }
    return f;
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) ValueSource(org.apache.lucene.queries.function.ValueSource) Bits(org.apache.lucene.util.Bits) FunctionValues(org.apache.lucene.queries.function.FunctionValues) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 45 with ValueSource

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

the class MultiBoolFunction method description.

@Override
public String description() {
    StringBuilder sb = new StringBuilder(name());
    sb.append('(');
    boolean first = true;
    for (ValueSource source : sources) {
        if (first) {
            first = false;
        } else {
            sb.append(',');
        }
        sb.append(source.description());
    }
    return sb.toString();
}
Also used : ValueSource(org.apache.lucene.queries.function.ValueSource)

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