Search in sources :

Example 96 with SchemaField

use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.

the class CursorMark method parseSerializedTotem.

/**
   * Parses the serialized version of a CursorMark from a client 
   * (which must conform to the existing sortSpec) and populates this object.
   *
   * @see #getSerializedTotem
   */
public void parseSerializedTotem(final String serialized) {
    if (CURSOR_MARK_START.equals(serialized)) {
        values = null;
        return;
    }
    final SortField[] sortFields = sortSpec.getSort().getSort();
    final List<SchemaField> schemaFields = sortSpec.getSchemaFields();
    List<Object> pieces = null;
    try {
        final byte[] rawData = Base64.base64ToByteArray(serialized);
        ByteArrayInputStream in = new ByteArrayInputStream(rawData);
        try {
            pieces = (List<Object>) new JavaBinCodec().unmarshal(in);
            boolean b = false;
            for (Object o : pieces) {
                if (o instanceof BytesRefBuilder || o instanceof BytesRef || o instanceof String) {
                    b = true;
                    break;
                }
            }
            if (b) {
                in.reset();
                pieces = (List<Object>) new JavaBinCodec().unmarshal(in);
            }
        } finally {
            in.close();
        }
    } catch (Exception ex) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "Unable to parse '" + CURSOR_MARK_PARAM + "' after totem: " + "value must either be '" + CURSOR_MARK_START + "' or the " + "'" + CURSOR_MARK_NEXT + "' returned by a previous search: " + serialized, ex);
    }
    assert null != pieces : "pieces wasn't parsed?";
    if (sortFields.length != pieces.size()) {
        throw new SolrException(ErrorCode.BAD_REQUEST, CURSOR_MARK_PARAM + " does not work with current sort (wrong size): " + serialized);
    }
    this.values = new ArrayList<>(sortFields.length);
    final BytesRef tmpBytes = new BytesRef();
    for (int i = 0; i < sortFields.length; i++) {
        SortField curSort = sortFields[i];
        SchemaField curField = schemaFields.get(i);
        Object rawValue = pieces.get(i);
        if (null != curField) {
            FieldType curType = curField.getType();
            rawValue = curType.unmarshalSortValue(rawValue);
        }
        this.values.add(rawValue);
    }
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) SortField(org.apache.lucene.search.SortField) SolrException(org.apache.solr.common.SolrException) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) ByteArrayInputStream(java.io.ByteArrayInputStream) BytesRef(org.apache.lucene.util.BytesRef) SolrException(org.apache.solr.common.SolrException)

Example 97 with SchemaField

use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.

the class CursorMark method getSerializedTotem.

/**
   * Generates a Base64 encoded serialized representation of the sort values 
   * encapsulated by this object, for use in cursor requests.
   *
   * @see #parseSerializedTotem
   */
public String getSerializedTotem() {
    if (null == this.values) {
        return CURSOR_MARK_START;
    }
    final List<SchemaField> schemaFields = sortSpec.getSchemaFields();
    final ArrayList<Object> marshalledValues = new ArrayList<>(values.size() + 1);
    for (int i = 0; i < schemaFields.size(); i++) {
        SchemaField fld = schemaFields.get(i);
        Object safeValue = values.get(i);
        if (null != fld) {
            FieldType type = fld.getType();
            safeValue = type.marshalSortValue(safeValue);
        }
        marshalledValues.add(safeValue);
    }
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream(256);
        try {
            new JavaBinCodec().marshal(marshalledValues, out);
            byte[] rawData = out.toByteArray();
            return Base64.byteArrayToBase64(rawData, 0, rawData.length);
        } finally {
            out.close();
        }
    } catch (Exception ex) {
        throw new SolrException(ErrorCode.SERVER_ERROR, "Unable to format search after totem", ex);
    }
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SolrException(org.apache.solr.common.SolrException) SolrException(org.apache.solr.common.SolrException) FieldType(org.apache.solr.schema.FieldType) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec)

Example 98 with SchemaField

use of org.apache.solr.schema.SchemaField 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 99 with SchemaField

use of org.apache.solr.schema.SchemaField 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 100 with SchemaField

use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.

the class VersionInfo method getAndCheckVersionField.

/**
   * Gets and returns the {@link org.apache.solr.common.params.CommonParams#VERSION_FIELD} from the specified
   * schema, after verifying that it is indexed, stored, and single-valued.  
   * If any of these pre-conditions are not met, it throws a SolrException 
   * with a user suitable message indicating the problem.
   */
public static SchemaField getAndCheckVersionField(IndexSchema schema) throws SolrException {
    final String errPrefix = VERSION_FIELD + " field must exist in schema and be searchable (indexed or docValues) and retrievable(stored or docValues) and not multiValued";
    SchemaField sf = schema.getFieldOrNull(VERSION_FIELD);
    if (null == sf) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, errPrefix + " (" + VERSION_FIELD + " does not exist)");
    }
    if (!sf.indexed() && !sf.hasDocValues()) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, errPrefix + " (" + VERSION_FIELD + " not searchable");
    }
    if (!sf.stored() && !sf.hasDocValues()) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, errPrefix + " (" + VERSION_FIELD + " not retrievable");
    }
    if (sf.multiValued()) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, errPrefix + " (" + VERSION_FIELD + " is multiValued");
    }
    return sf;
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SolrException(org.apache.solr.common.SolrException)

Aggregations

SchemaField (org.apache.solr.schema.SchemaField)167 SolrException (org.apache.solr.common.SolrException)45 FieldType (org.apache.solr.schema.FieldType)37 ArrayList (java.util.ArrayList)36 IndexSchema (org.apache.solr.schema.IndexSchema)32 NamedList (org.apache.solr.common.util.NamedList)27 BytesRef (org.apache.lucene.util.BytesRef)20 IOException (java.io.IOException)17 Query (org.apache.lucene.search.Query)17 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)17 HashMap (java.util.HashMap)16 Document (org.apache.lucene.document.Document)16 SolrParams (org.apache.solr.common.params.SolrParams)16 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)16 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)15 IndexableField (org.apache.lucene.index.IndexableField)14 Map (java.util.Map)13 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)13 SolrDocument (org.apache.solr.common.SolrDocument)13 SolrInputDocument (org.apache.solr.common.SolrInputDocument)13