List of usage examples for org.apache.lucene.index DocValues getBinary
public static BinaryDocValues getBinary(LeafReader reader, String field) throws IOException
From source file:net.semanticmetadata.lire.solr.LireValueSource.java
License:Open Source License
@Override /**/*w w w . ja v a2 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.analytics.function.field.StringField.java
License:Apache License
@Override public void doSetNextReader(LeafReaderContext context) throws IOException { docValues = DocValues.getBinary(context.reader(), fieldName); }
From source file:org.apache.solr.schema.TestPointFields.java
License:Apache License
private void doTestInternals(String field, String[] values) throws IOException { assertTrue(h.getCore().getLatestSchema().getField(field).getType() instanceof PointField); for (int i = 0; i < 10; i++) { assertU(adoc("id", String.valueOf(i), field, values[i])); }//w w w . j a v a 2 s. c o m assertU(commit()); IndexReader ir; RefCounted<SolrIndexSearcher> ref = null; SchemaField sf = h.getCore().getLatestSchema().getField(field); boolean ignoredField = !(sf.indexed() || sf.stored() || sf.hasDocValues()); try { ref = h.getCore().getSearcher(); SolrIndexSearcher searcher = ref.get(); ir = searcher.getIndexReader(); // our own SlowCompositeReader to check DocValues on disk w/o the UninvertingReader added by SolrIndexSearcher final LeafReader leafReaderForCheckingDVs = SlowCompositeReaderWrapper.wrap(searcher.getRawReader()); if (sf.indexed()) { assertEquals("Field " + field + " should have point values", 10, PointValues.size(ir, field)); } else { assertEquals("Field " + field + " should have no point values", 0, PointValues.size(ir, field)); } if (ignoredField) { assertTrue("Field " + field + " should not have docValues", DocValues.getSortedNumeric(leafReaderForCheckingDVs, field) .nextDoc() == DocIdSetIterator.NO_MORE_DOCS); assertTrue("Field " + field + " should not have docValues", DocValues .getNumeric(leafReaderForCheckingDVs, field).nextDoc() == DocIdSetIterator.NO_MORE_DOCS); assertTrue("Field " + field + " should not have docValues", DocValues .getSorted(leafReaderForCheckingDVs, field).nextDoc() == DocIdSetIterator.NO_MORE_DOCS); assertTrue("Field " + field + " should not have docValues", DocValues .getBinary(leafReaderForCheckingDVs, field).nextDoc() == DocIdSetIterator.NO_MORE_DOCS); } else { if (sf.hasDocValues()) { if (sf.multiValued()) { assertFalse("Field " + field + " should have docValues", DocValues.getSortedNumeric(leafReaderForCheckingDVs, field) .nextDoc() == DocIdSetIterator.NO_MORE_DOCS); } else { assertFalse("Field " + field + " should have docValues", DocValues.getNumeric(leafReaderForCheckingDVs, field) .nextDoc() == DocIdSetIterator.NO_MORE_DOCS); } } else { expectThrows(IllegalStateException.class, () -> DocValues.getSortedNumeric(leafReaderForCheckingDVs, field)); expectThrows(IllegalStateException.class, () -> DocValues.getNumeric(leafReaderForCheckingDVs, field)); } expectThrows(IllegalStateException.class, () -> DocValues.getSorted(leafReaderForCheckingDVs, field)); expectThrows(IllegalStateException.class, () -> DocValues.getBinary(leafReaderForCheckingDVs, field)); } for (LeafReaderContext leave : ir.leaves()) { LeafReader reader = leave.reader(); for (int i = 0; i < reader.numDocs(); i++) { Document doc = reader.document(i); if (sf.stored()) { assertNotNull("Field " + field + " not found. Doc: " + doc, doc.get(field)); } else { assertNull(doc.get(field)); } } } } finally { ref.decref(); } clearIndex(); assertU(commit()); }
From source file:org.elasticsearch.index.fielddata.plain.BytesBinaryDVIndexFieldData.java
License:Apache License
@Override public BytesBinaryDVAtomicFieldData load(LeafReaderContext context) { try {//from w ww. j a va 2 s . co m return new BytesBinaryDVAtomicFieldData(DocValues.getBinary(context.reader(), fieldNames.indexName())); } catch (IOException e) { throw new IllegalStateException("Cannot load doc values", e); } }