Search in sources :

Example 11 with FieldType

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

the class GroupConverter method fromMutable.

static Collection<SearchGroup<BytesRef>> fromMutable(SchemaField field, Collection<SearchGroup<MutableValue>> values) {
    if (values == null) {
        return null;
    }
    FieldType fieldType = field.getType();
    List<SearchGroup<BytesRef>> result = new ArrayList<>(values.size());
    for (SearchGroup<MutableValue> original : values) {
        SearchGroup<BytesRef> converted = new SearchGroup<BytesRef>();
        converted.sortValues = original.sortValues;
        if (original.groupValue.exists) {
            BytesRefBuilder binary = new BytesRefBuilder();
            fieldType.readableToIndexed(original.groupValue.toString(), binary);
            converted.groupValue = binary.get();
        } else {
            converted.groupValue = null;
        }
        result.add(converted);
    }
    return result;
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) SearchGroup(org.apache.lucene.search.grouping.SearchGroup) ArrayList(java.util.ArrayList) MutableValue(org.apache.lucene.util.mutable.MutableValue) BytesRef(org.apache.lucene.util.BytesRef) FieldType(org.apache.solr.schema.FieldType)

Example 12 with FieldType

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

the class GroupConverter method toMutable.

static Collection<SearchGroup<MutableValue>> toMutable(SchemaField field, Collection<SearchGroup<BytesRef>> values) {
    FieldType fieldType = field.getType();
    List<SearchGroup<MutableValue>> result = new ArrayList<>(values.size());
    for (SearchGroup<BytesRef> original : values) {
        SearchGroup<MutableValue> converted = new SearchGroup<MutableValue>();
        // ?
        converted.sortValues = original.sortValues;
        NumberType type = fieldType.getNumberType();
        final MutableValue v;
        switch(type) {
            case INTEGER:
                MutableValueInt mutableInt = new MutableValueInt();
                if (original.groupValue == null) {
                    mutableInt.value = 0;
                    mutableInt.exists = false;
                } else {
                    mutableInt.value = (Integer) fieldType.toObject(field, original.groupValue);
                }
                v = mutableInt;
                break;
            case FLOAT:
                MutableValueFloat mutableFloat = new MutableValueFloat();
                if (original.groupValue == null) {
                    mutableFloat.value = 0;
                    mutableFloat.exists = false;
                } else {
                    mutableFloat.value = (Float) fieldType.toObject(field, original.groupValue);
                }
                v = mutableFloat;
                break;
            case DOUBLE:
                MutableValueDouble mutableDouble = new MutableValueDouble();
                if (original.groupValue == null) {
                    mutableDouble.value = 0;
                    mutableDouble.exists = false;
                } else {
                    mutableDouble.value = (Double) fieldType.toObject(field, original.groupValue);
                }
                v = mutableDouble;
                break;
            case LONG:
                MutableValueLong mutableLong = new MutableValueLong();
                if (original.groupValue == null) {
                    mutableLong.value = 0;
                    mutableLong.exists = false;
                } else {
                    mutableLong.value = (Long) fieldType.toObject(field, original.groupValue);
                }
                v = mutableLong;
                break;
            case DATE:
                MutableValueDate mutableDate = new MutableValueDate();
                if (original.groupValue == null) {
                    mutableDate.value = 0;
                    mutableDate.exists = false;
                } else {
                    mutableDate.value = ((Date) fieldType.toObject(field, original.groupValue)).getTime();
                }
                v = mutableDate;
                break;
            default:
                throw new AssertionError();
        }
        converted.groupValue = v;
        result.add(converted);
    }
    return result;
}
Also used : SearchGroup(org.apache.lucene.search.grouping.SearchGroup) ArrayList(java.util.ArrayList) MutableValue(org.apache.lucene.util.mutable.MutableValue) MutableValueLong(org.apache.lucene.util.mutable.MutableValueLong) FieldType(org.apache.solr.schema.FieldType) MutableValueDate(org.apache.lucene.util.mutable.MutableValueDate) NumberType(org.apache.solr.schema.NumberType) MutableValueFloat(org.apache.lucene.util.mutable.MutableValueFloat) MutableValueDouble(org.apache.lucene.util.mutable.MutableValueDouble) MutableValueInt(org.apache.lucene.util.mutable.MutableValueInt) BytesRef(org.apache.lucene.util.BytesRef)

Example 13 with FieldType

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

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

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

the class FieldAnalysisRequestHandler method handleAnalysisRequest.

/**
   * Handles the resolved analysis request and returns the analysis breakdown response as a named list.
   *
   * @param request The request to handle.
   * @param schema  The index schema.
   *
   * @return The analysis breakdown as a named list.
   */
protected NamedList<NamedList> handleAnalysisRequest(FieldAnalysisRequest request, IndexSchema schema) {
    NamedList<NamedList> analysisResults = new SimpleOrderedMap<>();
    NamedList<NamedList> fieldTypeAnalysisResults = new SimpleOrderedMap<>();
    if (request.getFieldTypes() != null) {
        for (String fieldTypeName : request.getFieldTypes()) {
            FieldType fieldType = schema.getFieldTypes().get(fieldTypeName);
            fieldTypeAnalysisResults.add(fieldTypeName, analyzeValues(request, fieldType, null));
        }
    }
    NamedList<NamedList> fieldNameAnalysisResults = new SimpleOrderedMap<>();
    if (request.getFieldNames() != null) {
        for (String fieldName : request.getFieldNames()) {
            FieldType fieldType = schema.getFieldType(fieldName);
            fieldNameAnalysisResults.add(fieldName, analyzeValues(request, fieldType, fieldName));
        }
    }
    analysisResults.add("field_types", fieldTypeAnalysisResults);
    analysisResults.add("field_names", fieldNameAnalysisResults);
    return analysisResults;
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) FieldType(org.apache.solr.schema.FieldType)

Aggregations

FieldType (org.apache.solr.schema.FieldType)93 SchemaField (org.apache.solr.schema.SchemaField)37 SolrException (org.apache.solr.common.SolrException)29 ArrayList (java.util.ArrayList)23 BytesRef (org.apache.lucene.util.BytesRef)23 NamedList (org.apache.solr.common.util.NamedList)23 IOException (java.io.IOException)18 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)15 IndexSchema (org.apache.solr.schema.IndexSchema)14 Query (org.apache.lucene.search.Query)13 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)13 Analyzer (org.apache.lucene.analysis.Analyzer)12 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)10 CharsRefBuilder (org.apache.lucene.util.CharsRefBuilder)10 StrField (org.apache.solr.schema.StrField)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Map (java.util.Map)7 DocIterator (org.apache.solr.search.DocIterator)7 DocList (org.apache.solr.search.DocList)7