List of usage examples for org.apache.lucene.util BytesRefBuilder copyBytes
public void copyBytes(BytesRefBuilder builder)
From source file:net.semanticmetadata.lire.solr.LireValueSource.java
License:Open Source License
@Override /**// w w w.j a v a 2 s . c o m * Check also {@link org.apache.lucene.queries.function.valuesource.BytesRefFieldSource} */ public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException { final FieldInfo fieldInfo = readerContext.reader().getFieldInfos().fieldInfo(field); if (fieldInfo != null && fieldInfo.getDocValuesType() == DocValuesType.BINARY) { final BinaryDocValues binaryValues = DocValues.getBinary(readerContext.reader(), field); final Bits docsWithField = DocValues.getDocsWithField(readerContext.reader(), field); return new FunctionValues() { @Override public boolean exists(int doc) { return docsWithField.get(doc); } @Override public boolean bytesVal(int doc, BytesRefBuilder target) { target.copyBytes(binaryValues.get(doc)); return target.length() > 0; } @Override public float floatVal(int doc) { return (float) doubleVal(doc); } public String strVal(int doc) { final BytesRefBuilder bytes = new BytesRefBuilder(); return bytesVal(doc, bytes) ? bytes.get().utf8ToString() : null; } /** * This method basically decides which type is delivered on request. It can be a String, * in this case it is the double form the distance function. * @param doc * @return the distance as Double, mapping to {@link FunctionValues#doubleVal(int)} */ @Override public Object objectVal(int doc) { return doubleVal(doc); } @Override public String toString(int doc) { return description() + '=' + strVal(doc); } @Override /** * This method has to be implemented to support sorting! */ public double doubleVal(int doc) { if (binaryValues.get(doc).length > 0) { tmpFeature.setByteArrayRepresentation(binaryValues.get(doc).bytes, binaryValues.get(doc).offset, binaryValues.get(doc).length); return tmpFeature.getDistance(feature); } else return maxDistance; // make sure max distance is returned for those without value } }; } else { // there is no DocVal to sort by. Therefore we need to set the function value to -1 and everything without DocVal gets ranked first? return new DocTermsIndexDocValues(this, readerContext, field) { @Override protected String toTerm(String readableValue) { return Double.toString(maxDistance); } @Override public Object objectVal(int doc) { return maxDistance; } @Override public String toString(int doc) { return description() + '=' + strVal(doc); } public double doubleVal(int doc) { return maxDistance; } }; } }
From source file:org.apache.solr.legacy.TestLegacyNumericUtils.java
License:Apache License
public void testLongConversionAndOrdering() throws Exception { // generate a series of encoded longs, each numerical one bigger than the one before BytesRefBuilder last = new BytesRefBuilder(); BytesRefBuilder act = new BytesRefBuilder(); for (long l = -100000L; l < 100000L; l++) { LegacyNumericUtils.longToPrefixCoded(l, 0, act); if (last != null) { // test if smaller assertTrue("actual bigger than last (BytesRef)", last.get().compareTo(act.get()) < 0); assertTrue("actual bigger than last (as String)", last.get().utf8ToString().compareTo(act.get().utf8ToString()) < 0); }/*w w w .j av a 2 s . c om*/ // test is back and forward conversion works assertEquals("forward and back conversion should generate same long", l, LegacyNumericUtils.prefixCodedToLong(act.get())); // next step last.copyBytes(act); } }
From source file:org.apache.solr.legacy.TestLegacyNumericUtils.java
License:Apache License
public void testIntConversionAndOrdering() throws Exception { // generate a series of encoded ints, each numerical one bigger than the one before BytesRefBuilder act = new BytesRefBuilder(); BytesRefBuilder last = new BytesRefBuilder(); for (int i = -100000; i < 100000; i++) { LegacyNumericUtils.intToPrefixCoded(i, 0, act); if (last != null) { // test if smaller assertTrue("actual bigger than last (BytesRef)", last.get().compareTo(act.get()) < 0); assertTrue("actual bigger than last (as String)", last.get().utf8ToString().compareTo(act.get().utf8ToString()) < 0); }/* w ww . j ava 2 s. c o m*/ // test is back and forward conversion works assertEquals("forward and back conversion should generate same int", i, LegacyNumericUtils.prefixCodedToInt(act.get())); // next step last.copyBytes(act.get()); } }