Search in sources :

Example 36 with BinaryDocValues

use of org.apache.lucene.index.BinaryDocValues in project lucene-solr by apache.

the class BytesRefFieldSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FieldInfo fieldInfo = readerContext.reader().getFieldInfos().fieldInfo(field);
    // TODO: do it cleaner?
    if (fieldInfo != null && fieldInfo.getDocValuesType() == DocValuesType.BINARY) {
        final BinaryDocValues binaryValues = DocValues.getBinary(readerContext.reader(), field);
        return new FunctionValues() {

            int lastDocID = -1;

            private BytesRef getValueForDoc(int doc) throws IOException {
                if (doc < lastDocID) {
                    throw new IllegalArgumentException("docs were sent out-of-order: lastDocID=" + lastDocID + " vs docID=" + doc);
                }
                lastDocID = doc;
                int curDocID = binaryValues.docID();
                if (doc > curDocID) {
                    curDocID = binaryValues.advance(doc);
                }
                if (doc == curDocID) {
                    return binaryValues.binaryValue();
                } else {
                    return null;
                }
            }

            @Override
            public boolean exists(int doc) throws IOException {
                return getValueForDoc(doc) != null;
            }

            @Override
            public boolean bytesVal(int doc, BytesRefBuilder target) throws IOException {
                BytesRef value = getValueForDoc(doc);
                if (value == null || value.length == 0) {
                    return false;
                } else {
                    target.copyBytes(value);
                    return true;
                }
            }

            public String strVal(int doc) throws IOException {
                final BytesRefBuilder bytes = new BytesRefBuilder();
                return bytesVal(doc, bytes) ? bytes.get().utf8ToString() : null;
            }

            @Override
            public Object objectVal(int doc) throws IOException {
                return strVal(doc);
            }

            @Override
            public String toString(int doc) throws IOException {
                return description() + '=' + strVal(doc);
            }

            @Override
            public ValueFiller getValueFiller() {
                return new ValueFiller() {

                    private final MutableValueStr mval = new MutableValueStr();

                    @Override
                    public MutableValue getValue() {
                        return mval;
                    }

                    @Override
                    public void fillValue(int doc) throws IOException {
                        BytesRef value = getValueForDoc(doc);
                        mval.exists = value != null;
                        mval.value.clear();
                        if (value != null) {
                            mval.value.copyBytes(value);
                        }
                    }
                };
            }
        };
    } else {
        return new DocTermsIndexDocValues(this, readerContext, field) {

            @Override
            protected String toTerm(String readableValue) {
                return readableValue;
            }

            @Override
            public Object objectVal(int doc) throws IOException {
                return strVal(doc);
            }

            @Override
            public String toString(int doc) throws IOException {
                return description() + '=' + strVal(doc);
            }
        };
    }
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DocTermsIndexDocValues(org.apache.lucene.queries.function.docvalues.DocTermsIndexDocValues) MutableValueStr(org.apache.lucene.util.mutable.MutableValueStr) FunctionValues(org.apache.lucene.queries.function.FunctionValues) FieldInfo(org.apache.lucene.index.FieldInfo) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) BytesRef(org.apache.lucene.util.BytesRef)

Example 37 with BinaryDocValues

use of org.apache.lucene.index.BinaryDocValues in project SearchServices by Alfresco.

the class DocValueDocTransformer method transform.

/* (non-Javadoc)
     * @see org.apache.solr.response.transform.DocTransformer#transform(org.apache.solr.common.SolrDocument, int)
     */
@Override
public void transform(SolrDocument doc, int docid, float score) throws IOException {
    for (String fieldName : context.getSearcher().getFieldNames()) {
        SchemaField schemaField = context.getSearcher().getSchema().getFieldOrNull(fieldName);
        if (schemaField != null) {
            if (schemaField.hasDocValues()) {
                SortedDocValues sortedDocValues = context.getSearcher().getSlowAtomicReader().getSortedDocValues(fieldName);
                if (sortedDocValues != null) {
                    int ordinal = sortedDocValues.getOrd(docid);
                    if (ordinal > -1) {
                        doc.removeFields(fieldName);
                        String alfrescoFieldName = AlfrescoSolrDataModel.getInstance().getAlfrescoPropertyFromSchemaField(fieldName);
                        doc.removeFields(alfrescoFieldName);
                        doc.addField(alfrescoFieldName, schemaField.getType().toObject(schemaField, sortedDocValues.lookupOrd(ordinal)));
                    }
                }
                SortedSetDocValues sortedSetDocValues = context.getSearcher().getSlowAtomicReader().getSortedSetDocValues(fieldName);
                if (sortedSetDocValues != null) {
                    ArrayList<Object> newValues = new ArrayList<Object>();
                    sortedSetDocValues.setDocument(docid);
                    long ordinal;
                    while ((ordinal = sortedSetDocValues.nextOrd()) != SortedSetDocValues.NO_MORE_ORDS) {
                        newValues.add(schemaField.getType().toObject(schemaField, sortedSetDocValues.lookupOrd(ordinal)));
                    }
                    doc.removeFields(fieldName);
                    String alfrescoFieldName = AlfrescoSolrDataModel.getInstance().getAlfrescoPropertyFromSchemaField(fieldName);
                    doc.removeFields(alfrescoFieldName);
                    doc.addField(alfrescoFieldName, newValues);
                }
                BinaryDocValues binaryDocValues = context.getSearcher().getSlowAtomicReader().getBinaryDocValues(fieldName);
                if (binaryDocValues != null) {
                    doc.removeFields(fieldName);
                    String alfrescoFieldName = AlfrescoSolrDataModel.getInstance().getAlfrescoPropertyFromSchemaField(fieldName);
                    doc.removeFields(alfrescoFieldName);
                    doc.addField(alfrescoFieldName, schemaField.getType().toObject(schemaField, binaryDocValues.get(docid)));
                }
                if (schemaField.getType().getNumericType() != null) {
                    NumericDocValues numericDocValues = context.getSearcher().getSlowAtomicReader().getNumericDocValues(fieldName);
                    if (numericDocValues != null) {
                        doc.removeFields(fieldName);
                        String alfrescoFieldName = AlfrescoSolrDataModel.getInstance().getAlfrescoPropertyFromSchemaField(fieldName);
                        doc.removeFields(alfrescoFieldName);
                        switch(schemaField.getType().getNumericType()) {
                            case DOUBLE:
                                doc.addField(alfrescoFieldName, Double.longBitsToDouble(numericDocValues.get(docid)));
                                break;
                            case FLOAT:
                                doc.addField(alfrescoFieldName, Float.intBitsToFloat((int) numericDocValues.get(docid)));
                                break;
                            case INT:
                                doc.addField(alfrescoFieldName, (int) numericDocValues.get(docid));
                                break;
                            case LONG:
                                doc.addField(alfrescoFieldName, numericDocValues.get(docid));
                                break;
                        }
                    }
                    SortedNumericDocValues sortedNumericDocValues = context.getSearcher().getSlowAtomicReader().getSortedNumericDocValues(fieldName);
                    if (sortedNumericDocValues != null) {
                        sortedNumericDocValues.setDocument(docid);
                        doc.removeFields(fieldName);
                        String alfrescoFieldName = AlfrescoSolrDataModel.getInstance().getAlfrescoPropertyFromSchemaField(fieldName);
                        doc.removeFields(alfrescoFieldName);
                        ArrayList<Object> newValues = new ArrayList<Object>(sortedNumericDocValues.count());
                        if (sortedNumericDocValues.count() > 0) {
                            for (int i = 0; i < sortedNumericDocValues.count(); i++) {
                                switch(schemaField.getType().getNumericType()) {
                                    case DOUBLE:
                                        newValues.add(NumericUtils.sortableLongToDouble(sortedNumericDocValues.valueAt(i)));
                                        break;
                                    case FLOAT:
                                        newValues.add(NumericUtils.sortableIntToFloat((int) sortedNumericDocValues.valueAt(i)));
                                        break;
                                    case INT:
                                        newValues.add((int) sortedNumericDocValues.valueAt(i));
                                        break;
                                    case LONG:
                                        newValues.add(sortedNumericDocValues.valueAt(i));
                                        break;
                                }
                            }
                        }
                        doc.addField(alfrescoFieldName, newValues);
                    }
                }
            }
        }
    }
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) NumericDocValues(org.apache.lucene.index.NumericDocValues) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) ArrayList(java.util.ArrayList) SortedDocValues(org.apache.lucene.index.SortedDocValues) BinaryDocValues(org.apache.lucene.index.BinaryDocValues)

Aggregations

BinaryDocValues (org.apache.lucene.index.BinaryDocValues)37 BytesRef (org.apache.lucene.util.BytesRef)29 Document (org.apache.lucene.document.Document)13 LeafReader (org.apache.lucene.index.LeafReader)12 SortedDocValues (org.apache.lucene.index.SortedDocValues)12 NumericDocValues (org.apache.lucene.index.NumericDocValues)11 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)11 Directory (org.apache.lucene.store.Directory)10 ArrayList (java.util.ArrayList)9 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)9 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)9 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)7 DirectoryReader (org.apache.lucene.index.DirectoryReader)7 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)7 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)6 Bits (org.apache.lucene.util.Bits)6 IOException (java.io.IOException)5 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)5 IndexReader (org.apache.lucene.index.IndexReader)5 SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)5