Search in sources :

Example 26 with SchemaField

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

the class DirectUpdateHandler2 method getQuery.

private Query getQuery(DeleteUpdateCommand cmd) {
    Query q;
    try {
        // move this higher in the stack?
        QParser parser = QParser.getParser(cmd.getQuery(), cmd.req);
        q = parser.getQuery();
        q = QueryUtils.makeQueryable(q);
        // Make sure not to delete newer versions
        if (ulog != null && cmd.getVersion() != 0 && cmd.getVersion() != -Long.MAX_VALUE) {
            BooleanQuery.Builder bq = new BooleanQuery.Builder();
            bq.add(q, Occur.MUST);
            SchemaField sf = ulog.getVersionInfo().getVersionField();
            ValueSource vs = sf.getType().getValueSource(sf, null);
            ValueSourceRangeFilter filt = new ValueSourceRangeFilter(vs, Long.toString(Math.abs(cmd.getVersion())), null, true, true);
            FunctionRangeQuery range = new FunctionRangeQuery(filt);
            // formulated in the "MUST_NOT" sense so we can delete docs w/o a version (some tests depend on this...)
            bq.add(range, Occur.MUST_NOT);
            q = bq.build();
        }
        return q;
    } catch (SyntaxError e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
    }
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) FunctionRangeQuery(org.apache.solr.search.FunctionRangeQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FunctionRangeQuery(org.apache.solr.search.FunctionRangeQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ValueSourceRangeFilter(org.apache.solr.search.function.ValueSourceRangeFilter) SyntaxError(org.apache.solr.search.SyntaxError) ValueSource(org.apache.lucene.queries.function.ValueSource) QParser(org.apache.solr.search.QParser) SolrException(org.apache.solr.common.SolrException)

Example 27 with SchemaField

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

the class DocumentBuilder method toDocument.

/**
   * Convert a SolrInputDocument to a lucene Document.
   * 
   * This function should go elsewhere.  This builds the Document without an
   * extra Map<> checking for multiple values.  For more discussion, see:
   * http://www.nabble.com/Re%3A-svn-commit%3A-r547493---in--lucene-solr-trunk%3A-.--src-java-org-apache-solr-common--src-java-org-apache-solr-schema--src-java-org-apache-solr-update--src-test-org-apache-solr-common--tf3931539.html
   * 
   * TODO: /!\ NOTE /!\ This semantics of this function are still in flux.  
   * Something somewhere needs to be able to fill up a SolrDocument from
   * a lucene document - this is one place that may happen.  It may also be
   * moved to an independent function
   * 
   * @since solr 1.3
   * 
   * @param doc SolrInputDocument from which the document has to be built
   * @param schema Schema instance
   * @param forInPlaceUpdate Whether the output document would be used for an in-place update or not. When this is true,
   *        default fields values and copy fields targets are not populated.
   * @return Built Lucene document

   */
public static Document toDocument(SolrInputDocument doc, IndexSchema schema, boolean forInPlaceUpdate) {
    final SchemaField uniqueKeyField = schema.getUniqueKeyField();
    final String uniqueKeyFieldName = null == uniqueKeyField ? null : uniqueKeyField.getName();
    Document out = new Document();
    Set<String> usedFields = Sets.newHashSet();
    // Load fields from SolrDocument to Document
    for (SolrInputField field : doc) {
        String name = field.getName();
        SchemaField sfield = schema.getFieldOrNull(name);
        boolean used = false;
        // Make sure it has the correct number
        if (sfield != null && !sfield.multiValued() && field.getValueCount() > 1) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ERROR: " + getID(doc, schema) + "multiple values encountered for non multiValued field " + sfield.getName() + ": " + field.getValue());
        }
        List<CopyField> copyFields = schema.getCopyFieldsList(name);
        if (copyFields.size() == 0)
            copyFields = null;
        // load each field value
        boolean hasField = false;
        try {
            for (Object v : field) {
                if (v == null) {
                    continue;
                }
                hasField = true;
                if (sfield != null) {
                    used = true;
                    addField(out, sfield, v, name.equals(uniqueKeyFieldName) ? false : forInPlaceUpdate);
                    // record the field as having a value
                    usedFields.add(sfield.getName());
                }
                // This could happen whether it is explicit or not.
                if (copyFields != null) {
                    // and this is the uniqueKey field (because the uniqueKey can't change so no need to "update" the copyField).
                    if (!(forInPlaceUpdate && name.equals(uniqueKeyFieldName))) {
                        for (CopyField cf : copyFields) {
                            SchemaField destinationField = cf.getDestination();
                            final boolean destHasValues = usedFields.contains(destinationField.getName());
                            // check if the copy field is a multivalued or not
                            if (!destinationField.multiValued() && destHasValues) {
                                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ERROR: " + getID(doc, schema) + "multiple values encountered for non multiValued copy field " + destinationField.getName() + ": " + v);
                            }
                            used = true;
                            // Perhaps trim the length of a copy field
                            Object val = v;
                            if (val instanceof String && cf.getMaxChars() > 0) {
                                val = cf.getLimitedValue((String) val);
                            }
                            addField(out, destinationField, val, destinationField.getName().equals(uniqueKeyFieldName) ? false : forInPlaceUpdate);
                            // record the field as having a value
                            usedFields.add(destinationField.getName());
                        }
                    }
                }
            }
        } catch (SolrException ex) {
            throw ex;
        } catch (Exception ex) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ERROR: " + getID(doc, schema) + "Error adding field '" + field.getName() + "'='" + field.getValue() + "' msg=" + ex.getMessage(), ex);
        }
        // make sure the field was used somehow...
        if (!used && hasField) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "ERROR: " + getID(doc, schema) + "unknown field '" + name + "'");
        }
    }
    // during the full indexing initially.
    if (!forInPlaceUpdate) {
        for (SchemaField field : schema.getRequiredFields()) {
            if (out.getField(field.getName()) == null) {
                if (field.getDefaultValue() != null) {
                    addField(out, field, field.getDefaultValue(), false);
                } else {
                    String msg = getID(doc, schema) + "missing required field: " + field.getName();
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, msg);
                }
            }
        }
    }
    if (!forInPlaceUpdate) {
        moveLargestFieldLast(out);
    }
    return out;
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SolrInputField(org.apache.solr.common.SolrInputField) Document(org.apache.lucene.document.Document) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrException(org.apache.solr.common.SolrException) CopyField(org.apache.solr.schema.CopyField) SolrException(org.apache.solr.common.SolrException)

Example 28 with SchemaField

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

the class DocumentBuilder method getID.

private static String getID(SolrInputDocument doc, IndexSchema schema) {
    String id = "";
    SchemaField sf = schema.getUniqueKeyField();
    if (sf != null) {
        id = "[doc=" + doc.getFieldValue(sf.getName()) + "] ";
    }
    return id;
}
Also used : SchemaField(org.apache.solr.schema.SchemaField)

Example 29 with SchemaField

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

the class ExportWriter method getFieldWriters.

protected FieldWriter[] getFieldWriters(String[] fields, SolrIndexSearcher searcher) throws IOException {
    IndexSchema schema = searcher.getSchema();
    FieldWriter[] writers = new FieldWriter[fields.length];
    for (int i = 0; i < fields.length; i++) {
        String field = fields[i];
        SchemaField schemaField = null;
        try {
            schemaField = schema.getField(field);
        } catch (Exception e) {
            throw new IOException(e);
        }
        if (!schemaField.hasDocValues()) {
            throw new IOException(field + " must have DocValues to use this feature.");
        }
        boolean multiValued = schemaField.multiValued();
        FieldType fieldType = schemaField.getType();
        if (fieldType instanceof TrieIntField) {
            if (multiValued) {
                writers[i] = new MultiFieldWriter(field, fieldType, schemaField, true);
            } else {
                writers[i] = new IntFieldWriter(field);
            }
        } else if (fieldType instanceof TrieLongField) {
            if (multiValued) {
                writers[i] = new MultiFieldWriter(field, fieldType, schemaField, true);
            } else {
                writers[i] = new LongFieldWriter(field);
            }
        } else if (fieldType instanceof TrieFloatField) {
            if (multiValued) {
                writers[i] = new MultiFieldWriter(field, fieldType, schemaField, true);
            } else {
                writers[i] = new FloatFieldWriter(field);
            }
        } else if (fieldType instanceof TrieDoubleField) {
            if (multiValued) {
                writers[i] = new MultiFieldWriter(field, fieldType, schemaField, true);
            } else {
                writers[i] = new DoubleFieldWriter(field);
            }
        } else if (fieldType instanceof StrField) {
            if (multiValued) {
                writers[i] = new MultiFieldWriter(field, fieldType, schemaField, false);
            } else {
                writers[i] = new StringFieldWriter(field, fieldType);
            }
        } else if (fieldType instanceof TrieDateField) {
            if (multiValued) {
                writers[i] = new MultiFieldWriter(field, fieldType, schemaField, false);
            } else {
                writers[i] = new DateFieldWriter(field);
            }
        } else if (fieldType instanceof BoolField) {
            if (multiValued) {
                writers[i] = new MultiFieldWriter(field, fieldType, schemaField, true);
            } else {
                writers[i] = new BoolFieldWriter(field, fieldType);
            }
        } else {
            throw new IOException("Export fields must either be one of the following types: int,float,long,double,string,date,boolean");
        }
    }
    return writers;
}
Also used : StrField(org.apache.solr.schema.StrField) TrieIntField(org.apache.solr.schema.TrieIntField) TrieDateField(org.apache.solr.schema.TrieDateField) TrieLongField(org.apache.solr.schema.TrieLongField) TrieFloatField(org.apache.solr.schema.TrieFloatField) TrieDoubleField(org.apache.solr.schema.TrieDoubleField) BoolField(org.apache.solr.schema.BoolField) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) IndexSchema(org.apache.solr.schema.IndexSchema)

Example 30 with SchemaField

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

the class ExportWriter method getSortDoc.

private SortDoc getSortDoc(SolrIndexSearcher searcher, SortField[] sortFields) throws IOException {
    SortValue[] sortValues = new SortValue[sortFields.length];
    IndexSchema schema = searcher.getSchema();
    for (int i = 0; i < sortFields.length; ++i) {
        SortField sf = sortFields[i];
        String field = sf.getField();
        boolean reverse = sf.getReverse();
        SchemaField schemaField = schema.getField(field);
        FieldType ft = schemaField.getType();
        if (!schemaField.hasDocValues()) {
            throw new IOException(field + " must have DocValues to use this feature.");
        }
        if (ft instanceof TrieIntField) {
            if (reverse) {
                sortValues[i] = new IntValue(field, new IntDesc());
            } else {
                sortValues[i] = new IntValue(field, new IntAsc());
            }
        } else if (ft instanceof TrieFloatField) {
            if (reverse) {
                sortValues[i] = new FloatValue(field, new FloatDesc());
            } else {
                sortValues[i] = new FloatValue(field, new FloatAsc());
            }
        } else if (ft instanceof TrieDoubleField) {
            if (reverse) {
                sortValues[i] = new DoubleValue(field, new DoubleDesc());
            } else {
                sortValues[i] = new DoubleValue(field, new DoubleAsc());
            }
        } else if (ft instanceof TrieLongField) {
            if (reverse) {
                sortValues[i] = new LongValue(field, new LongDesc());
            } else {
                sortValues[i] = new LongValue(field, new LongAsc());
            }
        } else if (ft instanceof StrField) {
            LeafReader reader = searcher.getSlowAtomicReader();
            SortedDocValues vals = reader.getSortedDocValues(field);
            if (reverse) {
                sortValues[i] = new StringValue(vals, field, new IntDesc());
            } else {
                sortValues[i] = new StringValue(vals, field, new IntAsc());
            }
        } else if (ft instanceof TrieDateField) {
            if (reverse) {
                sortValues[i] = new LongValue(field, new LongDesc());
            } else {
                sortValues[i] = new LongValue(field, new LongAsc());
            }
        } else if (ft instanceof BoolField) {
            // This is a bit of a hack, but since the boolean field stores ByteRefs, just like Strings
            // _and_ since "F" happens to sort before "T" (thus false sorts "less" than true)
            // we can just use the existing StringValue here.
            LeafReader reader = searcher.getSlowAtomicReader();
            SortedDocValues vals = reader.getSortedDocValues(field);
            if (reverse) {
                sortValues[i] = new StringValue(vals, field, new IntDesc());
            } else {
                sortValues[i] = new StringValue(vals, field, new IntAsc());
            }
        } else {
            throw new IOException("Sort fields must be one of the following types: int,float,long,double,string,date,boolean");
        }
    }
    if (sortValues.length == 1) {
        return new SingleValueSortDoc(sortValues[0]);
    } else if (sortValues.length == 2) {
        return new DoubleValueSortDoc(sortValues[0], sortValues[1]);
    } else if (sortValues.length == 3) {
        return new TripleValueSortDoc(sortValues[0], sortValues[1], sortValues[2]);
    } else if (sortValues.length == 4) {
        return new QuadValueSortDoc(sortValues[0], sortValues[1], sortValues[2], sortValues[3]);
    } else {
        throw new IOException("A max of 4 sorts can be specified");
    }
}
Also used : StrField(org.apache.solr.schema.StrField) TrieIntField(org.apache.solr.schema.TrieIntField) SortField(org.apache.lucene.search.SortField) TrieDateField(org.apache.solr.schema.TrieDateField) TrieLongField(org.apache.solr.schema.TrieLongField) TrieFloatField(org.apache.solr.schema.TrieFloatField) TrieDoubleField(org.apache.solr.schema.TrieDoubleField) BoolField(org.apache.solr.schema.BoolField) LeafReader(org.apache.lucene.index.LeafReader) IOException(java.io.IOException) SortedDocValues(org.apache.lucene.index.SortedDocValues) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) IndexSchema(org.apache.solr.schema.IndexSchema)

Aggregations

SchemaField (org.apache.solr.schema.SchemaField)182 SolrException (org.apache.solr.common.SolrException)48 ArrayList (java.util.ArrayList)42 FieldType (org.apache.solr.schema.FieldType)41 IndexSchema (org.apache.solr.schema.IndexSchema)35 NamedList (org.apache.solr.common.util.NamedList)29 Query (org.apache.lucene.search.Query)23 IOException (java.io.IOException)22 BytesRef (org.apache.lucene.util.BytesRef)21 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)21 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)21 Document (org.apache.lucene.document.Document)20 SolrParams (org.apache.solr.common.params.SolrParams)19 IndexableField (org.apache.lucene.index.IndexableField)18 HashMap (java.util.HashMap)17 SolrInputDocument (org.apache.solr.common.SolrInputDocument)16 SolrDocument (org.apache.solr.common.SolrDocument)15 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)15 Map (java.util.Map)14 Term (org.apache.lucene.index.Term)14