Example usage for org.apache.lucene.index StoredFieldVisitor doubleField

List of usage examples for org.apache.lucene.index StoredFieldVisitor doubleField

Introduction

In this page you can find the example usage for org.apache.lucene.index StoredFieldVisitor doubleField.

Prototype

public void doubleField(FieldInfo fieldInfo, double value) throws IOException 

Source Link

Document

Process a double numeric field.

Usage

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  a  v  a2  s . co  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);// ww w .  j av  a2 s.c  o 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;
        }/*from  w  w w  .j  a  v a 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();
                }/* w  w w. j ava 2  s.com*/
            } else {
                visitor.stringField(info, f.stringValue());
            }
            break;
        case NO:
            break;
        case STOP:
            return;
        }
    }
}

From source file:org.elasticsearch.search.lookup.LeafFieldsLookupTests.java

License:Apache License

@Before
public void setUp() throws Exception {
    super.setUp();

    MappedFieldType fieldType = mock(MappedFieldType.class);
    when(fieldType.name()).thenReturn("field");
    when(fieldType.valueForDisplay(anyObject())).then(returnsFirstArg());

    MapperService mapperService = mock(MapperService.class);
    when(mapperService.fullName("field")).thenReturn(fieldType);
    when(mapperService.fullName("alias")).thenReturn(fieldType);

    FieldInfo mockFieldInfo = new FieldInfo("field", 1, false, false, true, IndexOptions.NONE,
            DocValuesType.NONE, -1, Collections.emptyMap(), 0, 0, false);

    LeafReader leafReader = mock(LeafReader.class);
    doAnswer(invocation -> {//  w  w  w.  ja  v a 2s.  c  om
        Object[] args = invocation.getArguments();
        StoredFieldVisitor visitor = (StoredFieldVisitor) args[1];
        visitor.doubleField(mockFieldInfo, 2.718);
        return null;
    }).when(leafReader).document(anyInt(), any(StoredFieldVisitor.class));

    fieldsLookup = new LeafFieldsLookup(mapperService, new String[] { "type" }, leafReader);
}

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//from  w ww. j  a  va2s .  co  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/*  w w w.ja  va2  s.  c  om*/
        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());
        }
    });
}