2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.sa.searchdbabstraction.util;
23 import com.fasterxml.jackson.core.JsonParseException;
24 import com.fasterxml.jackson.databind.JsonMappingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.util.List;
30 import java.util.concurrent.atomic.AtomicBoolean;
31 import org.apache.commons.io.IOUtils;
32 import org.onap.aai.sa.rest.DocumentFieldSchema;
33 import org.onap.aai.sa.rest.DocumentSchema;
35 public class DocumentSchemaUtil {
37 private static String dynamicCustomMapping = null;
38 private static final String DYNAMIC_CUSTOM_TEMPALTE_FILE = System.getProperty("CONFIG_HOME") + File.separator
39 + "dynamic-custom-template.json";
41 public static String generateDocumentMappings(String documentSchema)
42 throws JsonParseException, JsonMappingException, IOException {
44 // Unmarshal the json content into a document schema object.
45 ObjectMapper mapper = new ObjectMapper();
46 DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
48 return generateDocumentMappings(schema);
51 public static String generateDocumentMappings(DocumentSchema schema) throws IOException {
53 // Adding dynamic template to add fielddata=true to dynamic fields of type "string"
54 // in order to avoid aggregation queries breaking in ESv6.1.2
55 if(dynamicCustomMapping == null) {
57 dynamicCustomMapping = IOUtils.toString(new FileInputStream(DYNAMIC_CUSTOM_TEMPALTE_FILE), "UTF-8").replaceAll("\\s+", "");
58 } catch (IOException e) {
59 throw new IOException("Dynamic Custom template configuration went wrong! Please check for the correct template file.", e);
63 // Now, generate the Elastic Search mapping json and return it.
64 StringBuilder sb = new StringBuilder();
66 // Adding custom mapping which adds fielddata=true to dynamic fields of type "string"
67 sb.append(dynamicCustomMapping != null ? dynamicCustomMapping : "");
68 sb.append("\"properties\": {");
70 generateFieldMappings(schema.getFields(), sb);
79 private static void generateFieldMappings(List<DocumentFieldSchema> fields, StringBuilder sb) {
81 AtomicBoolean firstField = new AtomicBoolean(true);
83 for (DocumentFieldSchema field : fields) {
85 // If this isn't the first field in the list, prepend it with a ','
86 if (!firstField.compareAndSet(true, false)) {
90 // Now, append the translated field contents.
91 generateFieldMapping(field, sb);
95 private static void generateFieldMapping(DocumentFieldSchema fieldSchema, StringBuilder sb) {
97 sb.append("\"").append(fieldSchema.getName()).append("\": {");
99 // The field type is mandatory.
100 sb.append("\"type\": \"").append(fieldSchema.getDataType()).append("\"");
102 // For date type fields we may optionally supply a format specifier.
103 if (fieldSchema.getDataType().equals("date")) {
104 if (fieldSchema.getFormat() != null) {
105 sb.append(", \"format\": \"").append(fieldSchema.getFormat()).append("\"");
109 // If the index field was specified, then append it.
110 if (fieldSchema.getSearchable() != null) {
111 sb.append(", \"index\": \"").append(fieldSchema.getSearchable()
112 ? "analyzed" : "not_analyzed").append("\"");
115 // If a search analyzer was specified, then append it.
116 if (fieldSchema.getSearchAnalyzer() != null) {
117 sb.append(", \"search_analyzer\": \"").append(fieldSchema.getSearchAnalyzer()).append("\"");
120 // If an indexing analyzer was specified, then append it.
121 if (fieldSchema.getIndexAnalyzer() != null) {
122 sb.append(", \"analyzer\": \"").append(fieldSchema.getIndexAnalyzer()).append("\"");
126 if (fieldSchema.getDataType().equals("nested")) {
128 sb.append(", \"properties\": {");
129 generateFieldMappings(fieldSchema.getSubFields(), sb);