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);
}
};
}
}
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);
}
}
}
}
}
}
Aggregations