com.elefana.indices.V5FieldMapper Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright 2018 Viridian Software Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.elefana.indices;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.joda.time.DateTime;
import com.elefana.api.indices.IndexTemplate;
public class V5FieldMapper extends FieldMapper {
private static final Map EMPTY_MAPPING = new HashMap();
public V5FieldMapper() {
super();
Map allMapping = new HashMap();
allMapping.put("enabled", false);
EMPTY_MAPPING.put("_all", allMapping);
EMPTY_MAPPING.put("properties", new HashMap());
}
@Override
public List getFieldNamesFromMapping(Map mappings) {
Map propertyMappings = (Map) mappings.get("properties");
if(propertyMappings == null) {
return new ArrayList();
}
return new ArrayList(propertyMappings.keySet());
}
@Override
public Map getEmptyMapping() {
return EMPTY_MAPPING;
}
@Override
public Map convertIndexTemplateToMappings(IndexTemplate indexTemplate, String type) {
final Map result = new HashMap();
if(!indexTemplate.getMappings().containsKey(type)) {
return result;
}
return (Map) indexTemplate.getMappings().get(type);
}
@Override
public void generateMappings(Map existingMapping, Map document) {
Map propertyMappings = (Map) existingMapping.get("properties");
if(propertyMappings == null) {
propertyMappings = new HashMap();
existingMapping.put("properties", propertyMappings);
}
for(String propertyName : document.keySet()) {
if(propertyMappings.containsKey(propertyName)) {
continue;
}
if(document.get(propertyName) instanceof String) {
String value = (String) document.get(propertyName);
try {
if(DEFAULT_DATE_TIME_FORMATTER.parser().parseMillis(value) > 0L) {
propertyMappings.put(propertyName, generateMappingType("date"));
}
continue;
} catch (Exception e) {}
if(value.contains(" ")) {
propertyMappings.put(propertyName, generateMappingType("text"));
} else {
propertyMappings.put(propertyName, generateMappingType("string"));
}
} else if(document.get(propertyName) instanceof Boolean) {
propertyMappings.put(propertyName, generateMappingType("boolean"));
} else if(document.get(propertyName) instanceof Byte) {
propertyMappings.put(propertyName, generateMappingType("long"));
} else if(document.get(propertyName) instanceof Short) {
propertyMappings.put(propertyName, generateMappingType("long"));
} else if(document.get(propertyName) instanceof Integer) {
propertyMappings.put(propertyName, generateMappingType("long"));
} else if(document.get(propertyName) instanceof Long) {
propertyMappings.put(propertyName, generateMappingType("long"));
} else if(document.get(propertyName) instanceof Double) {
propertyMappings.put(propertyName, generateMappingType("double"));
} else if(document.get(propertyName) instanceof Float) {
propertyMappings.put(propertyName, generateMappingType("double"));
} else if(document.get(propertyName) instanceof Date) {
propertyMappings.put(propertyName, generateMappingType("date"));
} else if(document.get(propertyName) instanceof DateTime) {
propertyMappings.put(propertyName, generateMappingType("date"));
} else if(document.get(propertyName) instanceof List) {
propertyMappings.put(propertyName, generateMappingType("nested"));
} else {
propertyMappings.put(propertyName, generateMappingType("object"));
}
}
}
private Map generateMappingType(String type) {
Map result = new HashMap();
result.put("type", type);
return result;
}
@Override
public void mergeMapping(Map existingMapping, String fieldName, Map newMapping) {
if(!existingMapping.containsKey("properties")) {
existingMapping.put("properties", new HashMap());
}
existingMapping = (Map) existingMapping.get("properties");
replaceIfPresent(existingMapping, newMapping, fieldName);
}
private void replaceIfPresent(Map existingMapping, Map newMapping, String propertyName) {
if(!newMapping.containsKey(propertyName)) {
return;
}
existingMapping.put(propertyName, newMapping.get(propertyName));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy