Example usage for org.apache.lucene.document FeatureField FeatureField

List of usage examples for org.apache.lucene.document FeatureField FeatureField

Introduction

In this page you can find the example usage for org.apache.lucene.document FeatureField FeatureField.

Prototype

public FeatureField(String fieldName, String featureName, float featureValue) 

Source Link

Document

Create a feature.

Usage

From source file:org.elasticsearch.index.mapper.FeatureFieldMapper.java

License:Apache License

@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
    float value;//from w  w w  .j  ava 2  s  . c om
    if (context.externalValueSet()) {
        Object v = context.externalValue();
        if (v instanceof Number) {
            value = ((Number) v).floatValue();
        } else {
            value = Float.parseFloat(v.toString());
        }
    } else if (context.parser().currentToken() == Token.VALUE_NULL) {
        // skip
        return;
    } else {
        value = context.parser().floatValue();
    }

    if (context.doc().getByKey(name()) != null) {
        throw new IllegalArgumentException(
                "[feature] fields do not support indexing multiple values for the same field [" + name()
                        + "] in the same document");
    }

    if (fieldType().positiveScoreImpact() == false) {
        value = 1 / value;
    }

    context.doc().addWithKey(name(), new FeatureField("_feature", name(), value));
}

From source file:org.elasticsearch.index.mapper.FeatureVectorFieldMapper.java

License:Apache License

@Override
public FieldMapper parse(ParseContext context) throws IOException {
    if (context.externalValueSet()) {
        throw new IllegalArgumentException("[feature_vector] fields can't be used in multi-fields");
    }//www  .  j av a  2  s. com

    if (context.parser().currentToken() != Token.START_OBJECT) {
        throw new IllegalArgumentException(
                "[feature_vector] fields must be json objects, expected a START_OBJECT but got: "
                        + context.parser().currentToken());
    }

    String feature = null;
    for (Token token = context.parser().nextToken(); token != Token.END_OBJECT; token = context.parser()
            .nextToken()) {
        if (token == Token.FIELD_NAME) {
            feature = context.parser().currentName();
        } else if (token == Token.VALUE_NULL) {
            // ignore feature, this is consistent with numeric fields
        } else if (token == Token.VALUE_NUMBER || token == Token.VALUE_STRING) {
            final String key = name() + "." + feature;
            float value = context.parser().floatValue(true);
            if (context.doc().getByKey(key) != null) {
                throw new IllegalArgumentException(
                        "[feature_vector] fields do not support indexing multiple values for the same "
                                + "feature [" + key + "] in the same document");
            }
            context.doc().addWithKey(key, new FeatureField(name(), feature, value));
        } else {
            throw new IllegalArgumentException(
                    "[feature_vector] fields take hashes that map a feature to a strictly positive "
                            + "float, but got unexpected token " + token);
        }
    }
    return null; // no mapping update
}