Search in sources :

Example 1 with TrieDateField

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

the class StatsCollectorSupplierFactory method buildFieldSource.

/**
   *  Builds a value source for a given field, making sure that the field fits a given source type.
   * @param schema the schema
   * @param expressionString The name of the field to build a Field Source from.
   * @param sourceType FIELD_TYPE for any type of field, NUMBER_TYPE for numeric fields, 
   * DATE_TYPE for date fields and STRING_TYPE for string fields.
   * @return a value source
   */
private static ValueSource buildFieldSource(IndexSchema schema, String expressionString, int sourceType) {
    SchemaField sf;
    try {
        sf = schema.getField(expressionString);
    } catch (SolrException e) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "The field " + expressionString + " does not exist.", e);
    }
    FieldType type = sf.getType();
    if (type instanceof TrieIntField) {
        if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
            return null;
        }
        return new IntFieldSource(expressionString) {

            public String description() {
                return field;
            }
        };
    } else if (type instanceof TrieLongField) {
        if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
            return null;
        }
        return new LongFieldSource(expressionString) {

            public String description() {
                return field;
            }
        };
    } else if (type instanceof TrieFloatField) {
        if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
            return null;
        }
        return new FloatFieldSource(expressionString) {

            public String description() {
                return field;
            }
        };
    } else if (type instanceof TrieDoubleField) {
        if (sourceType != NUMBER_TYPE && sourceType != FIELD_TYPE) {
            return null;
        }
        return new DoubleFieldSource(expressionString) {

            public String description() {
                return field;
            }
        };
    } else if (type instanceof TrieDateField) {
        if (sourceType != DATE_TYPE && sourceType != FIELD_TYPE) {
            return null;
        }
        return new DateFieldSource(expressionString) {

            public String description() {
                return field;
            }
        };
    } else if (type instanceof StrField) {
        if (sourceType != STRING_TYPE && sourceType != FIELD_TYPE) {
            return null;
        }
        return new BytesRefFieldSource(expressionString) {

            public String description() {
                return field;
            }
        };
    }
    throw new SolrException(ErrorCode.BAD_REQUEST, type.toString() + " is not a supported field type in Solr Analytics.");
}
Also used : TrieDoubleField(org.apache.solr.schema.TrieDoubleField) StrField(org.apache.solr.schema.StrField) LongFieldSource(org.apache.lucene.queries.function.valuesource.LongFieldSource) TrieIntField(org.apache.solr.schema.TrieIntField) TrieDateField(org.apache.solr.schema.TrieDateField) BytesRefFieldSource(org.apache.lucene.queries.function.valuesource.BytesRefFieldSource) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) DoubleFieldSource(org.apache.lucene.queries.function.valuesource.DoubleFieldSource) IntFieldSource(org.apache.lucene.queries.function.valuesource.IntFieldSource) TrieLongField(org.apache.solr.schema.TrieLongField) FloatFieldSource(org.apache.lucene.queries.function.valuesource.FloatFieldSource) DateFieldSource(org.apache.solr.analytics.util.valuesource.DateFieldSource) SolrException(org.apache.solr.common.SolrException) TrieFloatField(org.apache.solr.schema.TrieFloatField)

Example 2 with TrieDateField

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

use of org.apache.solr.schema.TrieDateField 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)

Example 4 with TrieDateField

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

the class SolrDocumentFetcher method decorateDocValueFields.

/**
   * This will fetch and add the docValues fields to a given SolrDocument/SolrInputDocument
   *
   * @param doc
   *          A SolrDocument or SolrInputDocument instance where docValues will be added
   * @param docid
   *          The lucene docid of the document to be populated
   * @param fields
   *          The list of docValues fields to be decorated
   */
public void decorateDocValueFields(@SuppressWarnings("rawtypes") SolrDocumentBase doc, int docid, Set<String> fields) throws IOException {
    final List<LeafReaderContext> leafContexts = searcher.getLeafContexts();
    final int subIndex = ReaderUtil.subIndex(docid, leafContexts);
    final int localId = docid - leafContexts.get(subIndex).docBase;
    final LeafReader leafReader = leafContexts.get(subIndex).reader();
    for (String fieldName : fields) {
        final SchemaField schemaField = searcher.getSchema().getFieldOrNull(fieldName);
        if (schemaField == null || !schemaField.hasDocValues() || doc.containsKey(fieldName)) {
            log.warn("Couldn't decorate docValues for field: [{}], schemaField: [{}]", fieldName, schemaField);
            continue;
        }
        FieldInfo fi = searcher.getFieldInfos().fieldInfo(fieldName);
        if (fi == null) {
            // Searcher doesn't have info about this field, hence ignore it.
            continue;
        }
        final DocValuesType dvType = fi.getDocValuesType();
        switch(dvType) {
            case NUMERIC:
                final NumericDocValues ndv = leafReader.getNumericDocValues(fieldName);
                if (ndv == null) {
                    continue;
                }
                Long val;
                if (ndv.advanceExact(localId)) {
                    val = ndv.longValue();
                } else {
                    continue;
                }
                Object newVal = val;
                if (schemaField.getType().isPointField()) {
                    // TODO: Maybe merge PointField with TrieFields here
                    NumberType type = schemaField.getType().getNumberType();
                    switch(type) {
                        case INTEGER:
                            newVal = val.intValue();
                            break;
                        case LONG:
                            newVal = val.longValue();
                            break;
                        case FLOAT:
                            newVal = Float.intBitsToFloat(val.intValue());
                            break;
                        case DOUBLE:
                            newVal = Double.longBitsToDouble(val);
                            break;
                        case DATE:
                            newVal = new Date(val);
                            break;
                        default:
                            throw new AssertionError("Unexpected PointType: " + type);
                    }
                } else {
                    if (schemaField.getType() instanceof TrieIntField) {
                        newVal = val.intValue();
                    } else if (schemaField.getType() instanceof TrieFloatField) {
                        newVal = Float.intBitsToFloat(val.intValue());
                    } else if (schemaField.getType() instanceof TrieDoubleField) {
                        newVal = Double.longBitsToDouble(val);
                    } else if (schemaField.getType() instanceof TrieDateField) {
                        newVal = new Date(val);
                    } else if (schemaField.getType() instanceof EnumField) {
                        newVal = ((EnumField) schemaField.getType()).intValueToStringValue(val.intValue());
                    }
                }
                doc.addField(fieldName, newVal);
                break;
            case BINARY:
                BinaryDocValues bdv = leafReader.getBinaryDocValues(fieldName);
                if (bdv == null) {
                    continue;
                }
                BytesRef value;
                if (bdv.advanceExact(localId)) {
                    value = BytesRef.deepCopyOf(bdv.binaryValue());
                } else {
                    continue;
                }
                doc.addField(fieldName, value);
                break;
            case SORTED:
                SortedDocValues sdv = leafReader.getSortedDocValues(fieldName);
                if (sdv == null) {
                    continue;
                }
                if (sdv.advanceExact(localId)) {
                    final BytesRef bRef = sdv.binaryValue();
                    // Special handling for Boolean fields since they're stored as 'T' and 'F'.
                    if (schemaField.getType() instanceof BoolField) {
                        doc.addField(fieldName, schemaField.getType().toObject(schemaField, bRef));
                    } else {
                        doc.addField(fieldName, bRef.utf8ToString());
                    }
                }
                break;
            case SORTED_NUMERIC:
                final SortedNumericDocValues numericDv = leafReader.getSortedNumericDocValues(fieldName);
                NumberType type = schemaField.getType().getNumberType();
                if (numericDv != null) {
                    if (numericDv.advance(localId) == localId) {
                        final List<Object> outValues = new ArrayList<Object>(numericDv.docValueCount());
                        for (int i = 0; i < numericDv.docValueCount(); i++) {
                            long number = numericDv.nextValue();
                            switch(type) {
                                case INTEGER:
                                    outValues.add((int) number);
                                    break;
                                case LONG:
                                    outValues.add(number);
                                    break;
                                case FLOAT:
                                    outValues.add(NumericUtils.sortableIntToFloat((int) number));
                                    break;
                                case DOUBLE:
                                    outValues.add(NumericUtils.sortableLongToDouble(number));
                                    break;
                                case DATE:
                                    outValues.add(new Date(number));
                                    break;
                                default:
                                    throw new AssertionError("Unexpected PointType: " + type);
                            }
                        }
                        assert outValues.size() > 0;
                        doc.addField(fieldName, outValues);
                    }
                }
            case SORTED_SET:
                final SortedSetDocValues values = leafReader.getSortedSetDocValues(fieldName);
                if (values != null && values.getValueCount() > 0) {
                    if (values.advance(localId) == localId) {
                        final List<Object> outValues = new LinkedList<>();
                        for (long ord = values.nextOrd(); ord != SortedSetDocValues.NO_MORE_ORDS; ord = values.nextOrd()) {
                            value = values.lookupOrd(ord);
                            outValues.add(schemaField.getType().toObject(schemaField, value));
                        }
                        assert outValues.size() > 0;
                        doc.addField(fieldName, outValues);
                    }
                }
            case NONE:
                break;
        }
    }
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) TrieIntField(org.apache.solr.schema.TrieIntField) ArrayList(java.util.ArrayList) TrieDateField(org.apache.solr.schema.TrieDateField) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocValuesType(org.apache.lucene.index.DocValuesType) TrieFloatField(org.apache.solr.schema.TrieFloatField) BytesRef(org.apache.lucene.util.BytesRef) TrieDoubleField(org.apache.solr.schema.TrieDoubleField) EnumField(org.apache.solr.schema.EnumField) BoolField(org.apache.solr.schema.BoolField) LeafReader(org.apache.lucene.index.LeafReader) Date(java.util.Date) SortedDocValues(org.apache.lucene.index.SortedDocValues) LinkedList(java.util.LinkedList) SchemaField(org.apache.solr.schema.SchemaField) NumberType(org.apache.solr.schema.NumberType) SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) FieldInfo(org.apache.lucene.index.FieldInfo)

Example 5 with TrieDateField

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

the class SolrContentHandler method transformValue.

/**
   * Can be used to transform input values based on their {@link org.apache.solr.schema.SchemaField}
   * <p>
   * This implementation only formats dates using the {@link ExtractionDateUtil}.
   *
   * @param val    The value to transform
   * @param schFld The {@link org.apache.solr.schema.SchemaField}
   * @return The potentially new value.
   */
protected String transformValue(String val, SchemaField schFld) {
    String result = val;
    if (schFld != null && schFld.getType() instanceof TrieDateField) {
        //try to transform the date
        try {
            // may throw
            Date date = ExtractionDateUtil.parseDate(val, dateFormats);
            //ISO format
            result = date.toInstant().toString();
        } catch (Exception e) {
        // Let the specific fieldType handle errors
        // throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Invalid value: " + val + " for field: " + schFld, e);
        }
    }
    return result;
}
Also used : TrieDateField(org.apache.solr.schema.TrieDateField) Date(java.util.Date) SAXException(org.xml.sax.SAXException)

Aggregations

TrieDateField (org.apache.solr.schema.TrieDateField)6 SchemaField (org.apache.solr.schema.SchemaField)4 TrieDoubleField (org.apache.solr.schema.TrieDoubleField)4 TrieFloatField (org.apache.solr.schema.TrieFloatField)4 TrieIntField (org.apache.solr.schema.TrieIntField)4 Date (java.util.Date)3 BoolField (org.apache.solr.schema.BoolField)3 FieldType (org.apache.solr.schema.FieldType)3 StrField (org.apache.solr.schema.StrField)3 TrieLongField (org.apache.solr.schema.TrieLongField)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 LeafReader (org.apache.lucene.index.LeafReader)2 SortedDocValues (org.apache.lucene.index.SortedDocValues)2 SolrException (org.apache.solr.common.SolrException)2 IndexSchema (org.apache.solr.schema.IndexSchema)2 LinkedList (java.util.LinkedList)1 BinaryDocValues (org.apache.lucene.index.BinaryDocValues)1 DocValuesType (org.apache.lucene.index.DocValuesType)1 FieldInfo (org.apache.lucene.index.FieldInfo)1