
org.elasticsearch.search.lookup.LeafDocLookup Maven / Gradle / Ivy
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.search.lookup;
import org.apache.lucene.index.LeafReaderContext;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.common.logging.DeprecationCategory;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.script.field.DocValuesField;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
public class LeafDocLookup implements Map> {
private static final DeprecationLogger DEPRECATION_LOGGER = DeprecationLogger.getLogger(LeafDocLookup.class);
static final String TYPES_DEPRECATION_KEY = "type-field-doc-lookup";
static final String TYPES_DEPRECATION_MESSAGE = "[types removal] Looking up doc types [_type] in scripts is deprecated.";
private final Map localCacheScriptFieldData = new HashMap<>(4);
private final Map> localCacheFieldData = new HashMap<>(4);
private final Function fieldTypeLookup;
private final Function> fieldDataLookup;
private final LeafReaderContext reader;
private int docId = -1;
LeafDocLookup(
Function fieldTypeLookup,
Function> fieldDataLookup,
LeafReaderContext reader
) {
this.fieldTypeLookup = fieldTypeLookup;
this.fieldDataLookup = fieldDataLookup;
this.reader = reader;
}
public void setDocument(int docId) {
this.docId = docId;
}
public DocValuesField getScriptField(String fieldName) {
DocValuesField field = localCacheScriptFieldData.get(fieldName);
if (field == null) {
final MappedFieldType fieldType = fieldTypeLookup.apply(fieldName);
if (fieldType == null) {
throw new IllegalArgumentException("no field found for [" + fieldName + "] in mapping");
}
// Load the field data on behalf of the script. Otherwise, it would require
// additional permissions to deal with pagedbytes/ramusagestimator/etc.
field = AccessController.doPrivileged(new PrivilegedAction() {
@Override
public DocValuesField run() {
return fieldDataLookup.apply(fieldType).load(reader).getScriptField(fieldName);
}
});
localCacheScriptFieldData.put(fieldName, field);
}
try {
field.setNextDocId(docId);
} catch (IOException ioe) {
throw ExceptionsHelper.convertToElastic(ioe);
}
return field;
}
@Override
public ScriptDocValues> get(Object key) {
// deprecate _type
if ("_type".equals(key)) {
DEPRECATION_LOGGER.critical(DeprecationCategory.SCRIPTING, TYPES_DEPRECATION_KEY, TYPES_DEPRECATION_MESSAGE);
}
// assume its a string...
String fieldName = key.toString();
ScriptDocValues> scriptValues = localCacheFieldData.get(fieldName);
if (scriptValues == null) {
final MappedFieldType fieldType = fieldTypeLookup.apply(fieldName);
if (fieldType == null) {
throw new IllegalArgumentException("No field found for [" + fieldName + "] in mapping");
}
// load fielddata on behalf of the script: otherwise it would need additional permissions
// to deal with pagedbytes/ramusagestimator/etc
scriptValues = AccessController.doPrivileged(new PrivilegedAction>() {
@Override
public ScriptDocValues> run() {
return fieldDataLookup.apply(fieldType).load(reader).getScriptValues();
}
});
localCacheFieldData.put(fieldName, scriptValues);
}
try {
scriptValues.setNextDocId(docId);
} catch (IOException e) {
throw ExceptionsHelper.convertToElastic(e);
}
return scriptValues;
}
@Override
public boolean containsKey(Object key) {
// assume its a string...
String fieldName = key.toString();
ScriptDocValues> scriptValues = localCacheFieldData.get(fieldName);
return scriptValues != null || fieldTypeLookup.apply(fieldName) != null;
}
@Override
public int size() {
throw new UnsupportedOperationException();
}
@Override
public boolean isEmpty() {
throw new UnsupportedOperationException();
}
@Override
public boolean containsValue(Object value) {
throw new UnsupportedOperationException();
}
@Override
public ScriptDocValues> put(String key, ScriptDocValues> value) {
throw new UnsupportedOperationException();
}
@Override
public ScriptDocValues> remove(Object key) {
throw new UnsupportedOperationException();
}
@Override
public void putAll(Map extends String, ? extends ScriptDocValues>> m) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
@Override
public Set keySet() {
throw new UnsupportedOperationException();
}
@Override
public Collection> values() {
throw new UnsupportedOperationException();
}
@Override
public Set>> entrySet() {
throw new UnsupportedOperationException();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy