org.elasticsearch.index.fielddata.ScriptDocValues Java Examples

The following examples show how to use org.elasticsearch.index.fielddata.ScriptDocValues. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FieldDataFieldsFetchSubPhase.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    for (FieldDataFieldsContext.FieldDataField field : context.getFetchSubPhaseContext(CONTEXT_FACTORY).fields()) {
        if (hitContext.hit().fieldsOrNull() == null) {
            hitContext.hit().fields(new HashMap<String, SearchHitField>(2));
        }
        SearchHitField hitField = hitContext.hit().fields().get(field.name());
        if (hitField == null) {
            hitField = new InternalSearchHitField(field.name(), new ArrayList<>(2));
            hitContext.hit().fields().put(field.name(), hitField);
        }
        MappedFieldType fieldType = context.mapperService().smartNameFieldType(field.name());
        if (fieldType != null) {
            AtomicFieldData data = context.fieldData().getForField(fieldType).load(hitContext.readerContext());
            ScriptDocValues values = data.getScriptValues();
            values.setNextDocId(hitContext.docId());
            hitField.values().addAll(values.getValues());
        }
    }
}
 
Example #2
Source File: LeafDocLookup.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public Object get(Object key) {
    // assume its a string...
    String fieldName = key.toString();
    ScriptDocValues scriptValues = localCacheFieldData.get(fieldName);
    if (scriptValues == null) {
        final MappedFieldType fieldType = mapperService.smartNameFieldType(fieldName, types);
        if (fieldType == null) {
            throw new IllegalArgumentException("No field found for [" + fieldName + "] in mapping with types " + Arrays.toString(types) + "");
        }
        // load fielddata on behalf of the script: otherwise it would need additional permissions
        // to deal with pagedbytes/ramusagestimator/etc
        scriptValues = AccessController.doPrivileged(new PrivilegedAction<ScriptDocValues>() {
            @Override
            public ScriptDocValues run() {
                return fieldDataService.getForField(fieldType).load(reader).getScriptValues();
            }
        });
        localCacheFieldData.put(fieldName, scriptValues);
    }
    scriptValues.setNextDocId(docId);
    return scriptValues;
}
 
Example #3
Source File: IsPrimeSearchScriptFactory.java    From elasticsearch-native-script-example with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object run() {
    // First we get field using doc lookup
    ScriptDocValues<Long> docValue = (ScriptDocValues<Long>) doc().get(fieldName);
    // Check if field exists
    if (docValue != null && !docValue.isEmpty()) {
        try {
            // Try to parse it as an integer
            BigInteger bigInteger = BigInteger.valueOf(((Longs) docValue).getValue());
            // Check if it's prime
            return bigInteger.isProbablePrime(certainty);
        } catch (NumberFormatException ex) {
            return false;
        }
    }
    return false;
}
 
Example #4
Source File: FeatureVectorScoringSearchScript.java    From elasticsearch-feature-vector-scoring with Apache License 2.0 5 votes vote down vote up
@Override
public Object run() {
    if (this.field == null || this.inputFeatureVector == null || this.inputFeatureVectorNorm == 0) {
        return this.baseConstant;
    }

    if (!doc().containsKey(this.field) || doc().get(this.field) == null) {
        return this.baseConstant;
    }

    String docFeatureVectorStr = ((ScriptDocValues.Strings) doc().get(this.field)).getValue();
    return calculateScore(docFeatureVectorStr);
}
 
Example #5
Source File: LeafDocLookup.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public boolean containsKey(Object key) {
    // assume its a string...
    String fieldName = key.toString();
    ScriptDocValues scriptValues = localCacheFieldData.get(fieldName);
    if (scriptValues == null) {
        MappedFieldType fieldType = mapperService.smartNameFieldType(fieldName, types);
        if (fieldType == null) {
            return false;
        }
    }
    return true;
}
 
Example #6
Source File: MapScriptFactory.java    From elasticsearch-native-script-example with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object run() {
    ArrayList<Long> transactions = (ArrayList<Long>) agg.get(InitScriptFactory.TRANSACTIONS_FIELD);
    ScriptDocValues.Longs amount = (ScriptDocValues.Longs) doc().get("amount");
    ScriptDocValues.Strings type = (ScriptDocValues.Strings) doc().get("type");
    if ("sale".equals(type.getValue())) {
        transactions.add(amount.getValue());
    } else {
        transactions.add(-amount.getValue());
    }
    return null;
}
 
Example #7
Source File: AbstractSearchScript.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Returns field data strings access for the provided field.
 */
protected ScriptDocValues.Strings docFieldStrings(String field) {
    return (ScriptDocValues.Strings) doc().get(field);
}
 
Example #8
Source File: AbstractSearchScript.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Returns field data double (floating point) access for the provided field.
 */
protected ScriptDocValues.Doubles docFieldDoubles(String field) {
    return (ScriptDocValues.Doubles) doc().get(field);
}
 
Example #9
Source File: AbstractSearchScript.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Returns field data long (integers) access for the provided field.
 */
protected ScriptDocValues.Longs docFieldLongs(String field) {
    return (ScriptDocValues.Longs) doc().get(field);
}
 
Example #10
Source File: AtomicDoubleFieldData.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public final ScriptDocValues getScriptValues() {
    return new ScriptDocValues.Doubles(getDoubleValues());
}
 
Example #11
Source File: AtomicLongFieldData.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public final ScriptDocValues getScriptValues() {
    return new ScriptDocValues.Longs(getLongValues());
}
 
Example #12
Source File: AbstractAtomicParentChildFieldData.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public final ScriptDocValues getScriptValues() {
    return new ScriptDocValues.Strings(getBytesValues());
}
 
Example #13
Source File: AbstractAtomicGeoPointFieldData.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public final ScriptDocValues.GeoPoints getScriptValues() {
    return new ScriptDocValues.GeoPoints(getGeoPointValues());
}
 
Example #14
Source File: AbstractAtomicOrdinalsFieldData.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public final ScriptDocValues getScriptValues() {
    return new ScriptDocValues.Strings(getBytesValues());
}
 
Example #15
Source File: BinaryDVAtomicFieldData.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Strings getScriptValues() {
    return new ScriptDocValues.Strings(getBytesValues());
}
 
Example #16
Source File: BytesBinaryDVAtomicFieldData.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ScriptDocValues getScriptValues() {
    throw new UnsupportedOperationException();
}