All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scaleset.search.mongo.SimpleSchemaMapper Maven / Gradle / Ivy

There is a newer version: 0.24.0
Show newest version
package com.scaleset.search.mongo;

import com.scaleset.utils.Coerce;
import org.apache.lucene.util.BytesRef;

import java.util.HashMap;
import java.util.Map;

public class SimpleSchemaMapper implements SchemaMapper {

    private String defaultField;

    private Map> simpleSchema = new HashMap<>();

    public SimpleSchemaMapper(String defaultField) {
        this.defaultField = defaultField;
    }

    @Override
    public String mapField(String field) {
        return field;
    }

    public SimpleSchemaMapper withMapping(String field, Class type) {
        simpleSchema.put(field, type);
        return this;
    }

    @Override
    public Object mapValue(String field, Object value) {
        Class type = simpleSchema.get(field);
        if (type == null) {
            type = String.class;
        }
        if (value instanceof BytesRef) {
            value = ((BytesRef) value).utf8ToString();
        }
        return Coerce.to(value, type);
    }

    @Override
    public String defaultField() {
        return defaultField;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy