List of usage examples for org.apache.lucene.index StoredFieldVisitor intField
public void intField(FieldInfo fieldInfo, int value) throws IOException
From source file:lucene.security.index.SecureAtomicReader.java
License:Apache License
@Override public void document(int docID, final StoredFieldVisitor visitor) throws IOException { if (_accessControl.hasAccess(ReadType.DOCUMENT_FETCH_READ, docID)) { in.document(docID, visitor);/*from w ww. j av a 2 s . c o m*/ return; } if (_accessControl.hasAccess(ReadType.DOCUMENT_FETCH_DISCOVER, docID)) { // TODO add way to perform code when visitor runs here.... in.document(docID, new StoredFieldVisitor() { @Override public Status needsField(FieldInfo fieldInfo) throws IOException { if (_accessControl.canDiscoverField(fieldInfo.name)) { return visitor.needsField(fieldInfo); } else { return Status.NO; } } @Override public void binaryField(FieldInfo fieldInfo, byte[] value) throws IOException { visitor.binaryField(fieldInfo, value); } @Override public void stringField(FieldInfo fieldInfo, String value) throws IOException { visitor.stringField(fieldInfo, value); } @Override public void intField(FieldInfo fieldInfo, int value) throws IOException { visitor.intField(fieldInfo, value); } @Override public void longField(FieldInfo fieldInfo, long value) throws IOException { visitor.longField(fieldInfo, value); } @Override public void floatField(FieldInfo fieldInfo, float value) throws IOException { visitor.floatField(fieldInfo, value); } @Override public void doubleField(FieldInfo fieldInfo, double value) throws IOException { visitor.doubleField(fieldInfo, value); } }); return; } }
From source file:org.apache.solr.codecs.onsql.ONSQLStoredFieldsReader.java
License:Apache License
@Override public void visitDocument(int doc_id, StoredFieldVisitor visitor) throws IOException { // first we retrieve our custom primary key Key key_to_get = Key.createKey( Arrays.asList(this.shardid_key_part, this.segment_key_part, Base62Converter.fromBase10(doc_id))); Iterator<Key> segment_keys = kvstore.multiGetKeysIterator(Direction.FORWARD, 1, key_to_get, null, Depth.DESCENDANTS_ONLY);//from w w w. j av a2s .co m List<String> custom_pk = null; if (segment_keys.hasNext()) { Key segment_key = segment_keys.next(); custom_pk = new ArrayList<String>(segment_key.getMinorPath()); } if (custom_pk == null) throw new IllegalStateException("non-existing document id,search key used=" + key_to_get.toString()); // now let's get the main storage keys and values Iterator<KeyValueVersion> keyvals_iterator = kvstore.multiGetIterator(Direction.FORWARD, 10, // batchsize Key.createKey(custom_pk), // parentKey null, // subRange Depth.DESCENDANTS_ONLY); // depth while (keyvals_iterator.hasNext()) { KeyValueVersion store_entry = keyvals_iterator.next(); Key data_key = store_entry.getKey(); // we need to get two last pieces of minor key parts, these will be field number and type List<String> minor_part = data_key.getMinorPath(); String field_number = minor_part.get(0); // we have backrefs to segment keys present as a fields with negative field id, // which is started as _ sign with out Base62Converter, we skip them as they are key-only fields, // holding primary key for the segment as a minor key part, we skip them, // as they are nesessary only for segment deletion time if (field_number.startsWith("_")) continue; String field_type = minor_part.get(1); FieldInfo currentfield_info = this.fieldInfos.fieldInfo(Base62Converter.toBase10(field_number)); switch (visitor.needsField(currentfield_info)) { case YES: if (field_type.equals(TYPE_STRING)) visitor.stringField(currentfield_info, new String(store_entry.getValue().getValue(), StandardCharsets.UTF_8)); else if (field_type.equals(TYPE_BINARY)) visitor.binaryField(currentfield_info, store_entry.getValue().getValue()); else if (field_type.equals(TYPE_INT)) visitor.intField(currentfield_info, ByteBuffer.wrap(store_entry.getValue().getValue()).getInt()); else if (field_type.equals(TYPE_LONG)) visitor.longField(currentfield_info, ByteBuffer.wrap(store_entry.getValue().getValue()).getLong()); else if (field_type.equals(TYPE_FLOAT)) visitor.floatField(currentfield_info, ByteBuffer.wrap(store_entry.getValue().getValue()).getFloat()); else if (field_type.equals(TYPE_DOUBLE)) visitor.doubleField(currentfield_info, ByteBuffer.wrap(store_entry.getValue().getValue()).getDouble()); else throw new RuntimeException("unknown field type"); break; case NO: continue; case STOP: return; } } }
From source file:org.apache.solr.search.SolrDocumentFetcher.java
License:Apache License
/** Executes a stored field visitor against a hit from the document cache */ private void visitFromCached(Document document, StoredFieldVisitor visitor) throws IOException { for (IndexableField f : document) { final FieldInfo info = searcher.getFieldInfos().fieldInfo(f.name()); final StoredFieldVisitor.Status needsField = visitor.needsField(info); if (needsField == StoredFieldVisitor.Status.STOP) return; if (needsField == StoredFieldVisitor.Status.NO) continue; BytesRef binaryValue = f.binaryValue(); if (binaryValue != null) { visitor.binaryField(info, toByteArrayUnwrapIfPossible(binaryValue)); continue; }/* w w w. java 2s . c o m*/ Number numericValue = f.numericValue(); if (numericValue != null) { if (numericValue instanceof Double) { visitor.doubleField(info, numericValue.doubleValue()); } else if (numericValue instanceof Integer) { visitor.intField(info, numericValue.intValue()); } else if (numericValue instanceof Float) { visitor.floatField(info, numericValue.floatValue()); } else if (numericValue instanceof Long) { visitor.longField(info, numericValue.longValue()); } else { throw new AssertionError(); } continue; } // must be String if (f instanceof LargeLazyField) { // optimization to avoid premature string conversion visitor.stringField(info, toByteArrayUnwrapIfPossible(((LargeLazyField) f).readBytes())); } else { visitor.stringField(info, f.stringValue().getBytes(StandardCharsets.UTF_8)); } } }
From source file:org.apache.solr.search.SolrIndexSearcher.java
License:Apache License
/** Executes a stored field visitor against a hit from the document cache */ private void visitFromCached(Document document, StoredFieldVisitor visitor) throws IOException { for (IndexableField f : document) { FieldInfo info = fieldInfos.fieldInfo(f.name()); switch (visitor.needsField(info)) { case YES: if (f.binaryValue() != null) { BytesRef binaryValue = f.binaryValue(); byte copy[] = new byte[binaryValue.length]; System.arraycopy(binaryValue.bytes, binaryValue.offset, copy, 0, copy.length); visitor.binaryField(info, copy); } else if (f.numericValue() != null) { Number numericValue = f.numericValue(); if (numericValue instanceof Double) { visitor.doubleField(info, numericValue.doubleValue()); } else if (numericValue instanceof Integer) { visitor.intField(info, numericValue.intValue()); } else if (numericValue instanceof Float) { visitor.floatField(info, numericValue.floatValue()); } else if (numericValue instanceof Long) { visitor.longField(info, numericValue.longValue()); } else { throw new AssertionError(); }//from w ww. ja v a 2 s. c o m } else { visitor.stringField(info, f.stringValue()); } break; case NO: break; case STOP: return; } } }
From source file:org.elasticsearch.xpack.core.security.authz.accesscontrol.FieldSubsetReader.java
License:Open Source License
@Override public void document(final int docID, final StoredFieldVisitor visitor) throws IOException { super.document(docID, new StoredFieldVisitor() { @Override/* ww w. j a va 2 s .c o m*/ public void binaryField(FieldInfo fieldInfo, byte[] value) throws IOException { if (SourceFieldMapper.NAME.equals(fieldInfo.name)) { // for _source, parse, filter out the fields we care about, and serialize back downstream BytesReference bytes = new BytesArray(value); Tuple<XContentType, Map<String, Object>> result = XContentHelper.convertToMap(bytes, true); Map<String, Object> transformedSource = filter(result.v2(), filter, 0); XContentBuilder xContentBuilder = XContentBuilder.builder(result.v1().xContent()) .map(transformedSource); visitor.binaryField(fieldInfo, BytesReference.toBytes(BytesReference.bytes(xContentBuilder))); } else { visitor.binaryField(fieldInfo, value); } } @Override public void stringField(FieldInfo fieldInfo, byte[] value) throws IOException { visitor.stringField(fieldInfo, value); } @Override public void intField(FieldInfo fieldInfo, int value) throws IOException { visitor.intField(fieldInfo, value); } @Override public void longField(FieldInfo fieldInfo, long value) throws IOException { visitor.longField(fieldInfo, value); } @Override public void floatField(FieldInfo fieldInfo, float value) throws IOException { visitor.floatField(fieldInfo, value); } @Override public void doubleField(FieldInfo fieldInfo, double value) throws IOException { visitor.doubleField(fieldInfo, value); } @Override public Status needsField(FieldInfo fieldInfo) throws IOException { return hasField(fieldInfo.name) ? visitor.needsField(fieldInfo) : Status.NO; } }); }
From source file:tech.beshu.ror.es.security.DocumentFieldReader.java
License:Open Source License
@Override public void document(int docID, StoredFieldVisitor visitor) throws IOException { super.document(docID, new StoredFieldVisitor() { @Override/*from w w w . j a va 2 s. com*/ public Status needsField(FieldInfo fieldInfo) throws IOException { return policy.canKeep(fieldInfo.name) ? visitor.needsField(fieldInfo) : Status.NO; } @Override public int hashCode() { return visitor.hashCode(); } @Override public void stringField(FieldInfo fieldInfo, byte[] value) throws IOException { visitor.stringField(fieldInfo, value); } @Override public boolean equals(Object obj) { return visitor.equals(obj); } @Override public void doubleField(FieldInfo fieldInfo, double value) throws IOException { visitor.doubleField(fieldInfo, value); } @Override public void floatField(FieldInfo fieldInfo, float value) throws IOException { visitor.floatField(fieldInfo, value); } @Override public void intField(FieldInfo fieldInfo, int value) throws IOException { visitor.intField(fieldInfo, value); } @Override public void longField(FieldInfo fieldInfo, long value) throws IOException { visitor.longField(fieldInfo, value); } @Override public void binaryField(FieldInfo fieldInfo, byte[] value) throws IOException { if (!"_source".equals(fieldInfo.name)) { visitor.binaryField(fieldInfo, value); return; } Tuple<XContentType, Map<String, Object>> xContentTypeMapTuple = XContentHelper .convertToMap(new BytesArray(value), false, XContentType.JSON); Map<String, Object> map = xContentTypeMapTuple.v2(); Iterator<String> it = map.keySet().iterator(); while (it.hasNext()) { if (!policy.canKeep(it.next())) { it.remove(); } } final XContentBuilder xBuilder = XContentBuilder.builder(xContentTypeMapTuple.v1().xContent()) .map(map); ByteArrayOutputStream out = new ByteArrayOutputStream(); BytesReference.bytes(xBuilder).writeTo(out); visitor.binaryField(fieldInfo, out.toByteArray()); } }); }