Updating Search service to be ES 6.1.2 compliant
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / searchdbabstraction / util / DocumentSchemaUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.sa.searchdbabstraction.util;
22
23 import com.fasterxml.jackson.core.JsonParseException;
24 import com.fasterxml.jackson.databind.JsonMappingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26
27 import org.apache.commons.io.IOUtils;
28 import org.onap.aai.sa.rest.DocumentFieldSchema;
29 import org.onap.aai.sa.rest.DocumentSchema;
30
31 import java.io.File;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.concurrent.atomic.AtomicBoolean;
36
37 public class DocumentSchemaUtil {
38
39   private static String dynamicCustomMapping = null;
40   private static final String DYNAMIC_CUSTOM_TEMPALTE_FILE = System.getProperty("CONFIG_HOME") + File.separator 
41                   + "dynamic-custom-template.json";
42   
43   public static String generateDocumentMappings(String documentSchema)
44       throws JsonParseException, JsonMappingException, IOException {
45
46     // Unmarshal the json content into a document schema object.
47     ObjectMapper mapper = new ObjectMapper();
48     DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
49
50     return generateDocumentMappings(schema);
51   }
52
53   public static String generateDocumentMappings(DocumentSchema schema) throws IOException {
54           
55         // Adding dynamic template to add fielddata=true to dynamic fields of type "string"  
56         // in order to avoid aggregation queries breaking in ESv6.1.2
57         if(dynamicCustomMapping == null) {
58                 try {
59                         dynamicCustomMapping = IOUtils.toString(new FileInputStream(DYNAMIC_CUSTOM_TEMPALTE_FILE), "UTF-8").replaceAll("\\s+", "");
60                 } catch (IOException e) {
61                         throw new IOException("Dynamic Custom template configuration went wrong! Please check for the correct template file.", e);
62                 }
63         }
64
65     // Now, generate the Elastic Search mapping json and return it.
66     StringBuilder sb = new StringBuilder();
67     sb.append("{");
68     // Adding custom mapping which adds fielddata=true to dynamic fields of type "string"
69     sb.append(dynamicCustomMapping != null ? dynamicCustomMapping : "");
70     sb.append("\"properties\": {");
71
72     generateFieldMappings(schema.getFields(), sb);
73
74     sb.append("}");
75     sb.append("}");
76
77     return sb.toString();
78   }
79
80
81   private static void generateFieldMappings(List<DocumentFieldSchema> fields, StringBuilder sb) {
82
83     AtomicBoolean firstField = new AtomicBoolean(true);
84
85     for (DocumentFieldSchema field : fields) {
86
87       // If this isn't the first field in the list, prepend it with a ','
88       if (!firstField.compareAndSet(true, false)) {
89         sb.append(", ");
90       }
91
92       // Now, append the translated field contents.
93       generateFieldMapping(field, sb);
94     }
95   }
96
97   private static void generateFieldMapping(DocumentFieldSchema fieldSchema, StringBuilder sb) {
98
99     sb.append("\"").append(fieldSchema.getName()).append("\": {");
100
101     // The field type is mandatory.
102     sb.append("\"type\": \"").append(fieldSchema.getDataType()).append("\"");
103
104     // For date type fields we may optionally supply a format specifier.
105     if (fieldSchema.getDataType().equals("date")) {
106       if (fieldSchema.getFormat() != null) {
107         sb.append(", \"format\": \"").append(fieldSchema.getFormat()).append("\"");
108       }
109     }
110
111     // If the index field was specified, then append it.
112     if (fieldSchema.getSearchable() != null) {
113       sb.append(", \"index\": \"").append(fieldSchema.getSearchable()
114           ? "analyzed" : "not_analyzed").append("\"");
115     }
116
117     // If a search analyzer was specified, then append it.
118     if (fieldSchema.getSearchAnalyzer() != null) {
119       sb.append(", \"search_analyzer\": \"").append(fieldSchema.getSearchAnalyzer()).append("\"");
120     }
121
122     // If an indexing analyzer was specified, then append it.
123     if (fieldSchema.getIndexAnalyzer() != null) {
124       sb.append(", \"analyzer\": \"").append(fieldSchema.getIndexAnalyzer()).append("\"");
125     }
126
127
128     if (fieldSchema.getDataType().equals("nested")) {
129
130       sb.append(", \"properties\": {");
131       generateFieldMappings(fieldSchema.getSubFields(), sb);
132       sb.append("}");
133     }
134
135     sb.append("}");
136   }
137
138 }
139